From b700c2f8a0e6b8d61c3abea8d41438f66cd55025 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 6 Dec 2015 17:18:09 +0200 Subject: Make pylint work with new astroid exceptions, AstroidImportError and AstroidSyntaxError. --- pylint/checkers/imports.py | 22 ++++++++++------------ pylint/checkers/variables.py | 4 ++-- pylint/lint.py | 12 +++++------- pylint/test/functional/syntax_error.txt | 2 +- pylint/test/functional/unknown_encoding_py29.txt | 2 +- 5 files changed, 19 insertions(+), 23 deletions(-) diff --git a/pylint/checkers/imports.py b/pylint/checkers/imports.py index 0e16d18..5863130 100644 --- a/pylint/checkers/imports.py +++ b/pylint/checkers/imports.py @@ -298,7 +298,7 @@ given file (report RP0402 must not be disabled)'} for name in names: self._check_deprecated_module(node, name) - importedmodnode = self.get_imported_module(node, name) + importedmodnode = self._get_imported_module(node, name) if isinstance(node.scope(), astroid.Module): self._check_position(node) self._record_import(node, importedmodnode) @@ -320,7 +320,7 @@ given file (report RP0402 must not be disabled)'} self._check_reimport(node, basename=basename, level=node.level) modnode = node.root() - importedmodnode = self.get_imported_module(node, basename) + importedmodnode = self._get_imported_module(node, basename) if isinstance(node.scope(), astroid.Module): self._check_position(node) self._record_import(node, importedmodnode) @@ -458,22 +458,20 @@ given file (report RP0402 must not be disabled)'} '"%s"' % local_imports[0][0].as_string())) return std_imports, extern_imports, local_imports - def get_imported_module(self, importnode, modname): + def _get_imported_module(self, importnode, modname): try: return importnode.do_import_module(modname) - except astroid.InferenceError as ex: - dotted_modname = _get_import_name(importnode, modname) - if str(ex) != modname: - args = '%r (%s)' % (dotted_modname, ex) - else: - args = repr(dotted_modname) - + except astroid.AstroidBuildingException as exc: for submodule in _qualified_names(modname): if submodule in self._ignored_modules: return None - if not node_ignores_exception(importnode, ImportError): - self.add_message("import-error", args=args, node=importnode) + if node_ignores_exception(importnode, ImportError): + return None + + dotted_modname = _get_import_name(importnode, modname) + self.add_message("import-error", args=repr(dotted_modname), + node=importnode) def _check_relative_import(self, modnode, importnode, importedmodnode, importedasname): diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 7629a7b..b7a114d 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -44,7 +44,7 @@ def _is_from_future_import(stmt, name): """Check if the name is a future import from another module.""" try: module = stmt.do_import_module(stmt.modname) - except astroid.InferenceError: + except astroid.AstroidBuildingException: return for local_node in module.locals.get(name, []): @@ -1014,7 +1014,7 @@ builtins. Remember that you should avoid to define new builtins when possible.' name_parts = node.modname.split('.') try: module = node.do_import_module(name_parts[0]) - except Exception: + except astroid.AstroidBuildingException: return module = self._check_module_attrs(node, module, name_parts[1:]) if not module: diff --git a/pylint/lint.py b/pylint/lint.py index 7d08a76..03fcbb1 100644 --- a/pylint/lint.py +++ b/pylint/lint.py @@ -903,14 +903,12 @@ class PyLinter(config.OptionsManagerMixIn, """return a ast(roid) representation for a module""" try: return MANAGER.ast_from_file(filepath, modname, source=True) + except astroid.AstroidSyntaxError as ex: + self.add_message('syntax-error', + line=getattr(ex.error, 'lineno', 0), + args=str(ex.error)) except astroid.AstroidBuildingException as ex: - if isinstance(ex.args[0], SyntaxError): - ex = ex.args[0] - self.add_message('syntax-error', - line=ex.lineno or 0, - args=ex.msg) - else: - self.add_message('parse-error', args=ex) + self.add_message('parse-error', args=ex) except Exception as ex: # pylint: disable=broad-except import traceback traceback.print_exc() diff --git a/pylint/test/functional/syntax_error.txt b/pylint/test/functional/syntax_error.txt index b57bc72..76abba8 100644 --- a/pylint/test/functional/syntax_error.txt +++ b/pylint/test/functional/syntax_error.txt @@ -1 +1 @@ -syntax-error:1::invalid syntax \ No newline at end of file +syntax-error:1::invalid syntax (, line 1) \ No newline at end of file diff --git a/pylint/test/functional/unknown_encoding_py29.txt b/pylint/test/functional/unknown_encoding_py29.txt index 588b3bf..20e7757 100644 --- a/pylint/test/functional/unknown_encoding_py29.txt +++ b/pylint/test/functional/unknown_encoding_py29.txt @@ -1 +1 @@ -syntax-error:1::"unknown encoding: IBO-8859-1" +syntax-error:1::"unknown encoding: IBO-8859-1 (, line 0)" -- cgit v1.2.1