Mercurial > hg
diff 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 |
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)