[PATCH 6 of 8] import-checker: handle ast.parse SyntaxErrors

timeless timeless at mozdev.org
Wed Mar 30 05:24:06 EDT 2016


# HG changeset patch
# User timeless <timeless at mozdev.org>
# Date 1459325470 0
#      Wed Mar 30 08:11:10 2016 +0000
# Node ID d8be8db8119d2d716ad9ddaaaa077cf278f6273c
# Parent  54b962661f75e4a55c213c0eeb30f5d6024f7dbe
import-checker: handle ast.parse SyntaxErrors

diff --git a/contrib/import-checker.py b/contrib/import-checker.py
--- a/contrib/import-checker.py
+++ b/contrib/import-checker.py
@@ -233,6 +233,13 @@
 
 stdlib_modules = set(list_stdlib_modules())
 
+def parse(source):
+    """Wrapper around ast.parse()"""
+    try:
+        return ast.parse(source)
+    except SyntaxError:
+        return ast.parse("'SyntaxError'")
+
 def imported_modules(source, modulename, localmods, ignore_nested=False):
     """Given the source of a file as a string, yield the names
     imported by that file.
@@ -291,7 +298,7 @@
     ['foo.__init__']
     """
     fromlocal = fromlocalfunc(modulename, localmods)
-    for node in ast.walk(ast.parse(source)):
+    for node in ast.walk(parse(source)):
         if ignore_nested and getattr(node, 'col_offset', 0) > 0:
             continue
         if isinstance(node, ast.Import):
@@ -338,7 +345,7 @@
     The legacy convention only looks for mixed imports. The modern convention
     is much more thorough.
     """
-    root = ast.parse(source)
+    root = parse(source)
     absolute = usingabsolute(root)
 
     if absolute:


More information about the Mercurial-devel mailing list