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

login
register
mail settings
Submitter timeless@mozdev.org
Date April 12, 2016, 9:54 p.m.
Message ID <7dd9938d18883e201958.1460498040@waste.org>
Download mbox | patch
Permalink /patch/14562/
State Superseded
Headers show

Comments

timeless@mozdev.org - April 12, 2016, 9:54 p.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1460467246 0
#      Tue Apr 12 13:20:46 2016 +0000
# Node ID 7dd9938d18883e20195853b332945c8b7e9d1a0d
# Parent  df592defa539bf8a1f338a81bfbbb73f6e7d9935
import-checker: track filenames for SyntaxErrors
Simon Farnsworth - April 13, 2016, 10:50 a.m.
On 12/04/2016 22:54, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@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):

Where are "t" and "line" coming from? I don't see a change to sources in 
this patch, and the previous version didn't have t or line.

> +            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()
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://urldefense.proofpoint.com/v2/url?u=https-3A__www.mercurial-2Dscm.org_mailman_listinfo_mercurial-2Ddevel&d=CwIGaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=mEgSWILcY4c4W3zjApBQLA&m=9MhHVGl_MNyWpNn-JZ4rQAVubRdStFr3p7gdff2bgGA&s=tDSQuGQ120umqYYSCzv_dhXp1ozYyhzNSsi4E-lm6qs&e=
>

Patch

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()