changeset 28583:260ce2eed951

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.
author Gregory Szorc <gregory.szorc@gmail.com>
date Fri, 18 Mar 2016 16:15:12 -0700
parents cdbc25306696
children d69172ddfdca
files contrib/check-py3-compat.py tests/test-check-py3-compat.t
diffstat 2 files changed, 62 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/check-py3-compat.py	Fri Mar 18 16:17:56 2016 -0700
+++ b/contrib/check-py3-compat.py	Fri Mar 18 16:15:12 2016 -0700
@@ -12,8 +12,8 @@
 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)
@@ -36,8 +36,24 @@
     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)
--- a/tests/test-check-py3-compat.t	Fri Mar 18 16:17:56 2016 -0700
+++ b/tests/test-check-py3-compat.t	Fri Mar 18 16:15:12 2016 -0700
@@ -118,3 +118,46 @@
   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