[PATCH 2 of 6] import-checker: track filenames for SyntaxErrors

timeless timeless at mozdev.org
Tue Apr 12 17:54:00 EDT 2016


# HG changeset patch
# User timeless <timeless at mozdev.org>
# Date 1460467246 0
#      Tue Apr 12 13:20:46 2016 +0000
# Node ID 7dd9938d18883e20195853b332945c8b7e9d1a0d
# Parent  df592defa539bf8a1f338a81bfbbb73f6e7d9935
import-checker: track filenames for SyntaxErrors

diff --git a/contrib/import-checker.py b/contrib/import-checker.py
--- a/contrib/import-checker.py
+++ b/contrib/import-checker.py
@@ -225,7 +225,7 @@
 
 stdlib_modules = set(list_stdlib_modules())
 
-def imported_modules(source, modulename, localmods, ignore_nested=False):
+def imported_modules(source, modulename, f, localmods, ignore_nested=False):
     """Given the source of a file as a string, yield the names
     imported by that file.
 
@@ -239,6 +239,7 @@
     Returns:
       A list of absolute module names imported by the given source.
 
+    >>> f = 'foo/xxx.py'
     >>> modulename = 'foo.xxx'
     >>> localmods = {'foo.__init__': True,
     ...              'foo.foo1': True, 'foo.foo2': True,
@@ -247,43 +248,43 @@
     >>> # standard library (= not locally defined ones)
     >>> sorted(imported_modules(
     ...        'from stdlib1 import foo, bar; import stdlib2',
-    ...        modulename, localmods))
+    ...        modulename, f, localmods))
     []
     >>> # relative importing
     >>> sorted(imported_modules(
     ...        'import foo1; from bar import bar1',
-    ...        modulename, localmods))
+    ...        modulename, f, localmods))
     ['foo.bar.bar1', 'foo.foo1']
     >>> sorted(imported_modules(
     ...        'from bar.bar1 import name1, name2, name3',
-    ...        modulename, localmods))
+    ...        modulename, f, localmods))
     ['foo.bar.bar1']
     >>> # absolute importing
     >>> sorted(imported_modules(
     ...        'from baz import baz1, name1',
-    ...        modulename, localmods))
+    ...        modulename, f, localmods))
     ['baz.__init__', 'baz.baz1']
     >>> # mixed importing, even though it shouldn't be recommended
     >>> sorted(imported_modules(
     ...        'import stdlib, foo1, baz',
-    ...        modulename, localmods))
+    ...        modulename, f, localmods))
     ['baz.__init__', 'foo.foo1']
     >>> # ignore_nested
     >>> sorted(imported_modules(
     ... '''import foo
     ... def wat():
     ...     import bar
-    ... ''', modulename, localmods))
+    ... ''', modulename, f, localmods))
     ['foo.__init__', 'foo.bar.__init__']
     >>> sorted(imported_modules(
     ... '''import foo
     ... def wat():
     ...     import bar
-    ... ''', modulename, localmods, ignore_nested=True))
+    ... ''', modulename, f, localmods, ignore_nested=True))
     ['foo.__init__']
     """
     fromlocal = fromlocalfunc(modulename, localmods)
-    for node in ast.walk(ast.parse(source)):
+    for node in ast.walk(ast.parse(source, f)):
         if ignore_nested and getattr(node, 'col_offset', 0) > 0:
             continue
         if isinstance(node, ast.Import):
@@ -586,13 +587,18 @@
         modname = dotted_name_of_path(source_path, trimpure=True)
         localmods[modname] = source_path
     for modname, source_path in sorted(localmods.items()):
-        for src, modname2 in sources(source_path, modname):
-            used_imports[modname2] = sorted(
-                imported_modules(src, modname2, localmods, ignore_nested=True))
-            for error, lineno in verify_import_convention(modname2, src,
-                                                          localmods):
-                any_errors = True
-                print('%s:%d: %s' % (source_path, lineno, error))
+        for src, modname2, t, line in sources(source_path, modname):
+            try:
+                used_imports[modname2] = sorted(
+                    imported_modules(src, modname2, t, localmods,
+                                     ignore_nested=True))
+                for error, lineno in verify_import_convention(modname2, src,
+                                                              localmods):
+                    any_errors = True
+                    print('%s:%d: %s' % (source_path, lineno + line, error))
+            except SyntaxError as e:
+                print('SyntaxError %s:%d: %s' %
+                      (source_path, e.lineno + line, e))
     cycles = find_cycles(used_imports)
     if cycles:
         firstmods = set()


More information about the Mercurial-devel mailing list