From e4ecd3d2805abfbd6f6b457939d4026fa19446ce Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 6 Dec 2015 17:11:50 +0200 Subject: AstroidBuildingException is now AstroidBuildingError. The first name will exist until astroid 2.0. --- ChangeLog | 3 +++ astroid/brain/brain_gi.py | 6 +++--- astroid/brain/brain_six.py | 4 ++-- astroid/builder.py | 6 +++--- astroid/exceptions.py | 9 +++++---- astroid/inference.py | 4 ++-- astroid/manager.py | 18 +++++++++--------- astroid/scoped_nodes.py | 4 ++-- astroid/tests/unittest_builder.py | 2 +- astroid/tests/unittest_manager.py | 14 +++++++------- astroid/tests/unittest_nodes.py | 2 +- 11 files changed, 38 insertions(+), 34 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5003418..55a4602 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,9 @@ Change log for the astroid package (used to be astng) -- + * AstroidBuildingException is now AstroidBuildingError. The first + name will exist until astroid 2.0. + * Add two new exceptions, AstroidImportError and AstroidSyntaxError. They are subclasses of AstroidBuildingException and are raised when a module can't be imported from various reasons. diff --git a/astroid/brain/brain_gi.py b/astroid/brain/brain_gi.py index 0860207..d2c133d 100644 --- a/astroid/brain/brain_gi.py +++ b/astroid/brain/brain_gi.py @@ -9,7 +9,7 @@ import sys import re import warnings -from astroid import MANAGER, AstroidBuildingException, nodes +from astroid import MANAGER, AstroidBuildingError, nodes from astroid.builder import AstroidBuilder @@ -114,7 +114,7 @@ def _gi_build_stub(parent): def _import_gi_module(modname): # we only consider gi.repository submodules if not modname.startswith('gi.repository.'): - raise AstroidBuildingException(modname=modname) + raise AstroidBuildingError(modname=modname) # build astroid representation unless we already tried so if modname not in _inspected_modules: modnames = [modname] @@ -155,7 +155,7 @@ def _import_gi_module(modname): else: astng = _inspected_modules[modname] if astng is None: - raise AstroidBuildingException(modname=modname) + raise AstroidBuildingError(modname=modname) return astng def _looks_like_require_version(node): diff --git a/astroid/brain/brain_six.py b/astroid/brain/brain_six.py index 3b2b945..1c0ddf6 100644 --- a/astroid/brain/brain_six.py +++ b/astroid/brain/brain_six.py @@ -23,7 +23,7 @@ from textwrap import dedent from astroid import MANAGER, register_module_extender from astroid.builder import AstroidBuilder -from astroid.exceptions import AstroidBuildingException, InferenceError +from astroid.exceptions import AstroidBuildingError, InferenceError from astroid import nodes @@ -254,7 +254,7 @@ def six_moves_transform(): def _six_fail_hook(modname): if modname != 'six.moves': - raise AstroidBuildingException(modname=modname) + raise AstroidBuildingError(modname=modname) module = AstroidBuilder(MANAGER).string_build(_IMPORTS) module.name = 'six.moves' return module diff --git a/astroid/builder.py b/astroid/builder.py index 089cdd0..21b227d 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -118,7 +118,7 @@ class AstroidBuilder(raw_building.InspectBuilder): try: stream, encoding, data = open_source_file(path) except IOError as exc: - util.reraise(exceptions.AstroidBuildingException( + util.reraise(exceptions.AstroidBuildingError( 'Unable to load file {path}:\n{error}', modname=modname, path=path, error=exc)) except (SyntaxError, LookupError) as exc: @@ -127,7 +127,7 @@ class AstroidBuilder(raw_building.InspectBuilder): '{error}', modname=modname, path=path, error=exc)) except UnicodeError: # wrong encoding # detect_encoding returns utf-8 if no encoding specified - util.reraise(exceptions.AstroidBuildingException( + util.reraise(exceptions.AstroidBuildingError( 'Wrong ({encoding}) or no encoding specified for {filename}.', encoding=encoding, filename=filename)) with stream: @@ -202,7 +202,7 @@ class AstroidBuilder(raw_building.InspectBuilder): if name == '*': try: imported = node.do_import_module() - except exceptions.AstroidBuildingException: + except exceptions.AstroidBuildingError: continue for name in imported.wildcard_import_names(): node.parent.set_local(name, node) diff --git a/astroid/exceptions.py b/astroid/exceptions.py index 3437a1f..993e4b1 100644 --- a/astroid/exceptions.py +++ b/astroid/exceptions.py @@ -42,7 +42,7 @@ class AstroidError(Exception): return self.message.format(**vars(self)) -class AstroidBuildingException(AstroidError): +class AstroidBuildingError(AstroidError): """exception class when we are unable to build an astroid representation Standard attributes: @@ -51,14 +51,14 @@ class AstroidBuildingException(AstroidError): """ def __init__(self, message='Failed to import module {modname}.', **kws): - super(AstroidBuildingException, self).__init__(message, **kws) + super(AstroidBuildingError, self).__init__(message, **kws) -class AstroidImportError(AstroidBuildingException): +class AstroidImportError(AstroidBuildingError): """Exception class used when a module can't be imported by astroid.""" -class AstroidSyntaxError(AstroidBuildingException): +class AstroidSyntaxError(AstroidBuildingError): """Exception class used when a module can't be parsed.""" @@ -187,3 +187,4 @@ BinaryOperationError = util.BadBinaryOperationMessage SuperArgumentTypeError = SuperError UnresolvableName = NameInferenceError NotFoundError = AttributeInferenceError +AstroidBuildingException = AstroidBuildingError \ No newline at end of file diff --git a/astroid/inference.py b/astroid/inference.py index 7b2aa6b..5bbc34f 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -136,7 +136,7 @@ def infer_import(self, context=None, asname=True): yield self.do_import_module(self.real_name(name)) else: yield self.do_import_module(name) - except exceptions.AstroidBuildingException as exc: + except exceptions.AstroidBuildingError as exc: util.reraise(exceptions.InferenceError(node=self, error=exc, context=context)) @@ -161,7 +161,7 @@ def infer_import_from(self, context=None, asname=True): try: module = self.do_import_module() - except exceptions.AstroidBuildingException as exc: + except exceptions.AstroidBuildingError as exc: util.reraise(exceptions.InferenceError(node=self, error=exc, context=context)) diff --git a/astroid/manager.py b/astroid/manager.py index f2aa9d2..c332d0f 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -89,7 +89,7 @@ class AstroidManager(object): return AstroidBuilder(self).file_build(filepath, modname) elif fallback and modname: return self.ast_from_module_name(modname) - raise exceptions.AstroidBuildingException( + raise exceptions.AstroidBuildingError( 'Unable to build an AST for {path}.', path=filepath) def _build_stub_module(self, modname): @@ -140,11 +140,11 @@ class AstroidManager(object): "Can't find a file for module {modname}.", modname=modname) return self.ast_from_file(filepath, modname, fallback=False) - except exceptions.AstroidBuildingException as e: + except exceptions.AstroidBuildingError as e: for hook in self._failed_import_hooks: try: return hook(modname) - except exceptions.AstroidBuildingException: + except exceptions.AstroidBuildingError: pass raise e finally: @@ -187,8 +187,8 @@ class AstroidManager(object): modname=modname, error=ex) traceback = sys.exc_info()[2] self._mod_file_cache[(modname, contextfile)] = value - if isinstance(value, exceptions.AstroidBuildingException): - six.reraise(exceptions.AstroidBuildingException, + if isinstance(value, exceptions.AstroidBuildingError): + six.reraise(exceptions.AstroidBuildingError, value, traceback) return value @@ -213,7 +213,7 @@ class AstroidManager(object): try: modname = klass.__module__ except AttributeError: - util.reraise(exceptions.AstroidBuildingException( + util.reraise(exceptions.AstroidBuildingError( 'Unable to get module for class {class_name}.', cls=klass, class_repr=safe_repr(klass), modname=modname)) modastroid = self.ast_from_module_name(modname) @@ -228,7 +228,7 @@ class AstroidManager(object): try: modname = klass.__module__ except AttributeError: - util.reraise(exceptions.AstroidBuildingException( + util.reraise(exceptions.AstroidBuildingError( 'Unable to get module for {class_repr}.', cls=klass, class_repr=safe_repr(klass))) except Exception as ex: # pylint: disable=broad-except @@ -238,7 +238,7 @@ class AstroidManager(object): try: name = klass.__name__ except AttributeError: - util.reraise(exceptions.AstroidBuildingException( + util.reraise(exceptions.AstroidBuildingError( 'Unable to get name for {class_repr}:\n', cls=klass, class_repr=safe_repr(klass))) except Exception as ex: # pylint: disable=broad-except @@ -260,7 +260,7 @@ class AstroidManager(object): `hook` must be a function that accepts a single argument `modname` which contains the name of the module or package that could not be imported. If `hook` can resolve the import, must return a node of type `astroid.Module`, - otherwise, it must raise `AstroidBuildingException`. + otherwise, it must raise `AstroidBuildingError`. """ self._failed_import_hooks.append(hook) diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index a5680ad..de4b394 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -359,7 +359,7 @@ class Module(LocalsDictNodeNG): elif self.package: try: result = [self.import_module(name, relative_only=True)] - except (exceptions.AstroidBuildingException, SyntaxError): + except (exceptions.AstroidBuildingError, SyntaxError): util.reraise(exceptions.AttributeInferenceError(target=self, attribute=name, context=context)) @@ -422,7 +422,7 @@ class Module(LocalsDictNodeNG): absmodname = self.relative_to_absolute_name(modname, level) try: return MANAGER.ast_from_module_name(absmodname) - except exceptions.AstroidBuildingException: + except exceptions.AstroidBuildingError: # we only want to import a sub module or package of this module, # skip here if relative_only: diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index cb5bba9..eb99061 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -267,7 +267,7 @@ class BuilderTest(unittest.TestCase): resources.build_file('data/noendingnewline.py') def test_missing_file(self): - with self.assertRaises(exceptions.AstroidBuildingException): + with self.assertRaises(exceptions.AstroidBuildingError): resources.build_file('data/inexistant.py') def test_inspect_build0(self): diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index 6fc0776..37e3ae0 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -68,7 +68,7 @@ class AstroidManagerTest(resources.SysPathSetup, self.assertIn('unittest', self.manager.astroid_cache) def test_ast_from_file_name_astro_builder_exception(self): - self.assertRaises(exceptions.AstroidBuildingException, + self.assertRaises(exceptions.AstroidBuildingError, self.manager.ast_from_file, 'unhandledName') def test_do_not_expose_main(self): @@ -88,7 +88,7 @@ class AstroidManagerTest(resources.SysPathSetup, self.assertEqual(astroid.pure_python, False) def test_ast_from_module_name_astro_builder_exception(self): - self.assertRaises(exceptions.AstroidBuildingException, + self.assertRaises(exceptions.AstroidBuildingError, self.manager.ast_from_module_name, 'unhandledModule') @@ -140,7 +140,7 @@ class AstroidManagerTest(resources.SysPathSetup, def test_file_from_module_name_astro_building_exception(self): """check if the method launch a exception with a wrong module name""" - self.assertRaises(exceptions.AstroidBuildingException, + self.assertRaises(exceptions.AstroidBuildingError, self.manager.file_from_module_name, 'unhandledModule', None) def test_ast_from_module(self): @@ -179,7 +179,7 @@ class AstroidManagerTest(resources.SysPathSetup, def test_ast_from_class_attr_error(self): """give a wrong class at the ast_from_class method""" - self.assertRaises(exceptions.AstroidBuildingException, + self.assertRaises(exceptions.AstroidBuildingError, self.manager.ast_from_class, None) def testFailedImportHooks(self): @@ -187,13 +187,13 @@ class AstroidManagerTest(resources.SysPathSetup, if modname == 'foo.bar': return unittest else: - raise exceptions.AstroidBuildingException() + raise exceptions.AstroidBuildingError() - with self.assertRaises(exceptions.AstroidBuildingException): + with self.assertRaises(exceptions.AstroidBuildingError): self.manager.ast_from_module_name('foo.bar') self.manager.register_failed_import_hook(hook) self.assertEqual(unittest, self.manager.ast_from_module_name('foo.bar')) - with self.assertRaises(exceptions.AstroidBuildingException): + with self.assertRaises(exceptions.AstroidBuildingError): self.manager.ast_from_module_name('foo.bar.baz') del self.manager._failed_import_hooks[0] diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index e2e8338..8abd85e 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -458,7 +458,7 @@ class NameNodeTest(unittest.TestCase): del True """ if sys.version_info >= (3, 0): - with self.assertRaises(exceptions.AstroidBuildingException): + with self.assertRaises(exceptions.AstroidBuildingError): builder.parse(code) else: ast = builder.parse(code) -- cgit v1.2.1