/
check_py27_compat.py
87 lines (74 loc) · 2.27 KB
/
check_py27_compat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
Some of the files in this repo are used also by PyPy tests, which run on
python2.7.
This script tries to import all of them: it does not check any behavior, just
that they are importable and thus are not using any py3-specific syntax.
This script assumes that pathlib and pytest are installed (because the modules
try to import them).
"""
from __future__ import print_function
import sys
import traceback
import py
ROOT = py.path.local(__file__).join('..', '..')
TEST_DIRS = [ROOT / 'test', ROOT / 'test' / 'debug']
# PyPy does NOT import these files using py2
PY3_ONLY = ['test_support.py', 'test_handles_invalid.py', 'test_handles_leak.py']
def try_import(name):
try:
if isinstance(name, py.path.local):
print('Trying to import %s... ' % ROOT.bestrelpath(name), end='')
name.pyimport()
else:
print('Trying to import %s... ' % name, end='')
__import__(name)
except:
print('ERROR!')
print()
traceback.print_exc(file=sys.stdout)
print()
return False
else:
print('OK')
return True
def try_import_hpy_devel():
"""
To import hpy.devel we need to create an empty hpy/__init__.py, because
python2.7 does not support namespace packages.
Return the number of failed imports.
"""
failed = 0
init_py = ROOT.join('hpy', '__init__.py')
assert init_py.check(exists=False)
try:
init_py.write('') # create an empty __init__.py
if not try_import('hpy.devel'):
failed += 1
finally:
init_py.remove()
return failed
def try_import_tests(dirs):
failed = 0
for d in dirs:
for t in d.listdir('test_*.py'):
if t.basename in PY3_ONLY:
continue
if not try_import(t):
failed += 1
return failed
def main():
if sys.version_info[:2] != (2, 7):
print('ERROR: this script should be run on top of python 2.7')
sys.exit(1)
sys.path.insert(0, str(ROOT))
failed = 0
failed += try_import_hpy_devel()
failed += try_import_tests(TEST_DIRS)
print()
if failed == 0:
print('Everything ok!')
else:
print('%d failed imports :(' % failed)
sys.exit(1)
if __name__ == '__main__':
main()