Mercurial > hg
comparison contrib/check-py3-compat.py @ 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 | ae522fb493d4 |
children | d69172ddfdca |
comparison
equal
deleted
inserted
replaced
28582:cdbc25306696 | 28583:260ce2eed951 |
---|---|
10 from __future__ import absolute_import, print_function | 10 from __future__ import absolute_import, print_function |
11 | 11 |
12 import ast | 12 import ast |
13 import sys | 13 import sys |
14 | 14 |
15 def check_compat(f): | 15 def check_compat_py2(f): |
16 """Check Python 3 compatibility for a file.""" | 16 """Check Python 3 compatibility for a file with Python 2""" |
17 with open(f, 'rb') as fh: | 17 with open(f, 'rb') as fh: |
18 content = fh.read() | 18 content = fh.read() |
19 root = ast.parse(content) | 19 root = ast.parse(content) |
20 | 20 |
21 # Ignore empty files. | 21 # Ignore empty files. |
34 if 'absolute_import' not in futures: | 34 if 'absolute_import' not in futures: |
35 print('%s not using absolute_import' % f) | 35 print('%s not using absolute_import' % f) |
36 if haveprint and 'print_function' not in futures: | 36 if haveprint and 'print_function' not in futures: |
37 print('%s requires print_function' % f) | 37 print('%s requires print_function' % f) |
38 | 38 |
39 def check_compat_py3(f): | |
40 """Check Python 3 compatibility of a file with Python 3.""" | |
41 with open(f, 'rb') as fh: | |
42 content = fh.read() | |
43 | |
44 try: | |
45 ast.parse(content) | |
46 except SyntaxError as e: | |
47 print('%s: invalid syntax: %s' % (f, e)) | |
48 return | |
49 | |
39 if __name__ == '__main__': | 50 if __name__ == '__main__': |
51 if sys.version_info[0] == 2: | |
52 fn = check_compat_py2 | |
53 else: | |
54 fn = check_compat_py3 | |
55 | |
40 for f in sys.argv[1:]: | 56 for f in sys.argv[1:]: |
41 check_compat(f) | 57 fn(f) |
42 | 58 |
43 sys.exit(0) | 59 sys.exit(0) |