[PATCH 3 of 5] tests: perform an ast parse with Python 3

Gregory Szorc gregory.szorc at gmail.com
Sat Mar 12 17:11:34 EST 2016


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1457816230 28800
#      Sat Mar 12 12:57:10 2016 -0800
# Node ID 14590a7efe1391034e61c43151037c6e1f2b8e4d
# Parent  8f46c2b82dee58117e861f1c374c8c229a8fd12d
tests: perform an ast parse with Python 3

Previously, test-check-py3-compat.t parsed Python files with Python 2
and looked for known patterns that are incompatible with Python 3.

Now that we have a mechanism for invoking Python 3 interpreters from
tests, we can expand check-py3-compat.py and its corresponding .t
test to perform an additional AST parse using Python 3.

As the test output shows, we identify a number of new parse failures
on Python 3. There are some redundant warnings for missing parentheses
for the print function. Given the recent influx of patches around
fixing these, the redundancy shouldn't last for too long.

diff --git a/contrib/check-py3-compat.py b/contrib/check-py3-compat.py
--- a/contrib/check-py3-compat.py
+++ b/contrib/check-py3-compat.py
@@ -7,18 +7,18 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
 from __future__ import absolute_import, print_function
 
 import ast
 import sys
 
-def check_compat(f):
-    """Check Python 3 compatibility for a file."""
+def check_compat_py2(f):
+    """Check Python 3 compatibility for a file with Python 2"""
     with open(f, 'rb') as fh:
         content = fh.read()
     root = ast.parse(content)
 
     # Ignore empty files.
     if not root.body:
         return
 
@@ -31,13 +31,29 @@ def check_compat(f):
         elif isinstance(node, ast.Print):
             haveprint = True
 
     if 'absolute_import' not in futures:
         print('%s not using absolute_import' % f)
     if haveprint and 'print_function' not in futures:
         print('%s requires print_function' % f)
 
+def check_compat_py3(f):
+    """Check Python 3 compatibility of a file with Python 3."""
+    with open(f, 'rb') as fh:
+        content = fh.read()
+
+    try:
+        ast.parse(content)
+    except SyntaxError as e:
+        print('%s: invalid syntax: %s' % (f, e))
+        return
+
 if __name__ == '__main__':
+    if sys.version_info[0] == 2:
+        fn = check_compat_py2
+    else:
+        fn = check_compat_py3
+
     for f in sys.argv[1:]:
-        check_compat(f)
+        fn(f)
 
     sys.exit(0)
diff --git a/tests/test-check-py3-compat.t b/tests/test-check-py3-compat.t
--- a/tests/test-check-py3-compat.t
+++ b/tests/test-check-py3-compat.t
@@ -123,8 +123,51 @@
   tests/test-ui-config.py requires print_function
   tests/test-ui-verbosity.py not using absolute_import
   tests/test-ui-verbosity.py requires print_function
   tests/test-url.py not using absolute_import
   tests/test-url.py requires print_function
   tests/test-walkrepo.py requires print_function
   tests/test-wireproto.py requires print_function
   tests/tinyproxy.py requires print_function
+
+#if py3exe
+  $ hg files 'set:(**.py)' | sed 's|\\|/|g' | xargs $PYTHON3 contrib/check-py3-compat.py
+  contrib/check-code.py: invalid syntax: invalid syntax (<unknown>, line 446)
+  contrib/import-checker.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 569)
+  contrib/revsetbenchmarks.py: invalid syntax: invalid syntax (<unknown>, line 186)
+  doc/hgmanpage.py: invalid syntax: invalid syntax (<unknown>, line 286)
+  hgext/color.py: invalid syntax: invalid syntax (<unknown>, line 551)
+  mercurial/archival.py: invalid syntax: invalid syntax (<unknown>, line 234)
+  mercurial/bundle2.py: invalid syntax: invalid syntax (<unknown>, line 977)
+  mercurial/commands.py: invalid syntax: invalid syntax (<unknown>, line 3324)
+  tests/filterpyflakes.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 61)
+  tests/generate-working-copy-states.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 69)
+  tests/get-with-headers.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 44)
+  tests/readlink.py: invalid syntax: invalid syntax (<unknown>, line 7)
+  tests/seq.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 23)
+  tests/silenttestrunner.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 11)
+  tests/test-ancestor.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 187)
+  tests/test-batching.py: invalid syntax: invalid syntax (<unknown>, line 34)
+  tests/test-bdiff.py: invalid syntax: invalid syntax (<unknown>, line 10)
+  tests/test-context.py: invalid syntax: invalid syntax (<unknown>, line 21)
+  tests/test-demandimport.py: invalid syntax: invalid syntax (<unknown>, line 26)
+  tests/test-duplicateoptions.py: invalid syntax: invalid syntax (<unknown>, line 34)
+  tests/test-filecache.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 23)
+  tests/test-filelog.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 33)
+  tests/test-hg-parseurl.py: invalid syntax: invalid syntax (<unknown>, line 4)
+  tests/test-hgweb-auth.py: invalid syntax: invalid syntax (<unknown>, line 24)
+  tests/test-hybridencode.py: invalid syntax: invalid syntax (<unknown>, line 5)
+  tests/test-lrucachedict.py: invalid syntax: invalid syntax (<unknown>, line 6)
+  tests/test-minirst.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 6)
+  tests/test-parseindex2.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 173)
+  tests/test-propertycache.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 50)
+  tests/test-revlog-ancestry.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 49)
+  tests/test-status-inprocess.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 8)
+  tests/test-trusted.py: invalid syntax: invalid syntax (<unknown>, line 60)
+  tests/test-ui-color.py: invalid syntax: invalid syntax (<unknown>, line 11)
+  tests/test-ui-config.py: invalid syntax: invalid syntax (<unknown>, line 32)
+  tests/test-ui-verbosity.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 9)
+  tests/test-walkrepo.py: invalid syntax: invalid syntax (<unknown>, line 37)
+  tests/test-wireproto.py: invalid syntax: invalid syntax (<unknown>, line 55)
+  tests/tinyproxy.py: invalid syntax: Missing parentheses in call to 'print' (<unknown>, line 53)
+
+#endif


More information about the Mercurial-devel mailing list