Debian Codegraph @

subprojects/pythoncapi-compat/tests/test_pythoncapi_compat.py

raw
1 #!/usr/bin/python3
2 """
3 Run the test suite.
4
5 Usage::
6
7 python3 run_tests.py
8 python3 run_tests.py -v # verbose mode
9 """
10 from __future__ import absolute_import
11 from __future__ import print_function
12 import gc
13 import os.path
14 import shutil
15 import subprocess
16 import sys
17 try:
18 import faulthandler
19 except ImportError:
20 # Python 2
21 faulthandler = None
22
23 # test.utils
24 from utils import run_command, command_stdout
25
26
27 # Windows uses MSVC compiler
28 MSVC = (os.name == "nt")
29
30 TESTS = [
31 ("test_pythoncapi_compat_cext", "C"),
32 ]
33 if not MSVC:
34 TESTS.extend((
35 ("test_pythoncapi_compat_cpp03ext", "C++03"),
36 ("test_pythoncapi_compat_cpp11ext", "C++11"),
37 ))
38 else:
39 TESTS.extend((
40 ("test_pythoncapi_compat_cppext", "C++"),
41 ("test_pythoncapi_compat_cpp14ext", "C++14"),
42 ))
43
44
45 VERBOSE = False
46
47
48 def display_title(title):
49 if not VERBOSE:
50 return
51
52 ver = sys.version_info
53 title = "Python %s.%s: %s" % (ver.major, ver.minor, title)
54
55 print(title)
56 print("=" * len(title))
57 print()
58 sys.stdout.flush()
59
60
61 def build_ext():
62 display_title("Build test extensions")
63 if os.path.exists("build"):
64 shutil.rmtree("build")
65 cmd = [sys.executable, "setup.py", "build"]
66 if VERBOSE:
67 run_command(cmd)
68 print()
69 else:
70 exitcode, stdout = command_stdout(cmd, stderr=subprocess.STDOUT)
71 if exitcode:
72 print(stdout.rstrip())
73 sys.exit(exitcode)
74
75
76 def import_tests(module_name):
77 pythonpath = None
78 for name in os.listdir("build"):
79 if name.startswith('lib.'):
80 pythonpath = os.path.join("build", name)
81
82 if not pythonpath:
83 raise Exception("Failed to find the build directory")
84 sys.path.append(pythonpath)
85
86 return __import__(module_name)
87
88
89 def _run_tests(tests, verbose):
90 for name, test_func in tests:
91 if verbose:
92 print("%s()" % name)
93 sys.stdout.flush()
94 test_func()
95
96
97 _HAS_CLEAR_TYPE_CACHE = hasattr(sys, '_clear_type_cache')
98
99 def _refleak_cleanup():
100 if _HAS_CLEAR_TYPE_CACHE:
101 sys._clear_type_cache()
102 gc.collect()
103
104
105 def _check_refleak(test_func, verbose):
106 nrun = 6
107 for i in range(1, nrun + 1):
108 if verbose:
109 if i > 1:
110 print()
111 print("Run %s/%s:" % (i, nrun))
112 sys.stdout.flush()
113
114 init_refcnt = sys.gettotalrefcount()
115 test_func()
116 _refleak_cleanup()
117 diff = sys.gettotalrefcount() - init_refcnt
118
119 if i > 3 and diff:
120 raise AssertionError("refcnt leak, diff: %s" % diff)
121
122
123 def python_version():
124 ver = sys.version_info
125 build = 'debug' if hasattr(sys, 'gettotalrefcount') else 'release'
126 if hasattr(sys, 'implementation'):
127 python_impl = sys.implementation.name
128 if python_impl == 'cpython':
129 python_impl = 'CPython'
130 elif python_impl == 'pypy':
131 python_impl = 'PyPy'
132 else:
133 if "PyPy" in sys.version:
134 python_impl = "PyPy"
135 else:
136 python_impl = 'Python'
137 return "%s %s.%s (%s build)" % (python_impl, ver.major, ver.minor, build)
138
139
140 def run_tests(module_name, lang):
141 title = "Test %s (%s)" % (module_name, lang)
142 display_title(title)
143
144 try:
145 testmod = import_tests(module_name)
146 except ImportError:
147 # The C extension must always be available
148 if lang == "C":
149 raise
150
151 if VERBOSE:
152 print("%s: skip %s, missing %s extension"
153 % (python_version(), lang, module_name))
154 print()
155 return
156
157 if VERBOSE:
158 empty_line = False
159 for attr in ('__cplusplus', 'PY_VERSION', 'PY_VERSION_HEX',
160 'PYPY_VERSION', 'PYPY_VERSION_NUM'):
161 try:
162 value = getattr(testmod, attr)
163 except AttributeError:
164 pass
165 else:
166 if attr in ("PY_VERSION_HEX", "PYPY_VERSION_NUM"):
167 value = "0x%x" % value
168 print("%s: %s" % (attr, value))
169 empty_line = True
170
171 if empty_line:
172 print()
173
174 check_refleak = hasattr(sys, 'gettotalrefcount')
175
176 tests = [(name, getattr(testmod, name))
177 for name in dir(testmod)
178 if name.startswith("test")]
179
180 def test_func():
181 _run_tests(tests, VERBOSE)
182
183 if check_refleak:
184 _check_refleak(test_func, VERBOSE)
185 else:
186 test_func()
187
188 if VERBOSE:
189 print()
190
191 msg = "%s %s tests succeeded!" % (len(tests), lang)
192 msg = "%s: %s" % (python_version(), msg)
193 if check_refleak:
194 msg = "%s (no reference leak detected)" % msg
195 print(msg)
196
197
198 def main():
199 global VERBOSE
200 VERBOSE = ("-v" in sys.argv[1:] or "--verbose" in sys.argv[1:])
201
202 if (3, 13) <= sys.version_info <= (3, 13, 0, 'alpha', 4):
203 print("SKIP Python 3.13 alpha 1..4: not supported!")
204 return
205
206 if faulthandler is not None:
207 faulthandler.enable()
208
209 src_dir = os.path.dirname(__file__)
210 if src_dir:
211 os.chdir(src_dir)
212
213 build_ext()
214
215 for module_name, lang in TESTS:
216 run_tests(module_name, lang)
217
218
219 if __name__ == "__main__":
220 main()