Patchwork [2,of,4,V2] tests: perform an ast parse with Python 3

login
register
mail settings
Submitter Gregory Szorc
Date March 18, 2016, 11:19 p.m.
Message ID <fa7fd1c3c288e28ae7b4.1458343193@gps-mbp.local>
Download mbox | patch
Permalink /patch/13942/
State Accepted
Headers show

Comments

Gregory Szorc - March 18, 2016, 11:19 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1458342912 25200
#      Fri Mar 18 16:15:12 2016 -0700
# Node ID fa7fd1c3c288e28ae7b4eb174f05218433767ba9
# Parent  049a0221014adeef44ab5dd1c1daf34caf9e0090
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.

Patch

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
@@ -11,10 +11,10 @@  from __future__ import absolute_import, 
 
 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)
 
@@ -35,9 +35,25 @@  def check_compat(f):
         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
@@ -122,4 +122,47 @@ 
   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: (unicode error) 'unicodeescape' codec can't decode bytes in position 18-19: malformed \N character escape (<unknown>, line 106)
+  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