Debian Codegraph @

subprojects/pythoncapi-compat/runtests.py

raw
1 #!/usr/bin/python3 -u
2 """
3 Run the test suite on multiple Python versions.
4
5 Usage::
6
7 python3 runtests.py
8 python3 runtests.py --verbose
9 python3 runtests.py --current --verbose
10 """
11 from __future__ import absolute_import
12 from __future__ import print_function
13 import argparse
14 import os.path
15 import shutil
16 import sys
17 try:
18 from shutil import which
19 except ImportError:
20 # Python 2
21 from distutils.spawn import find_executable as which
22
23
24 from tests.utils import run_command
25
26
27 TEST_DIR = os.path.join(os.path.dirname(__file__), 'tests')
28 TEST_COMPAT = os.path.join(TEST_DIR, "test_pythoncapi_compat.py")
29 TEST_UPGRADE = os.path.join(TEST_DIR, "test_upgrade_pythoncapi.py")
30
31 PYTHONS = (
32 # CPython
33 "python3-debug",
34 "python3",
35 "python2.7",
36 "python3.6",
37 "python3.7",
38 "python3.8",
39 "python3.9",
40 "python3.10",
41 "python3.11",
42 "python3.12",
43 "python3.13",
44
45 # PyPy
46 "pypy",
47 "pypy2",
48 "pypy2.7",
49 "pypy3",
50 "pypy3.6",
51 "pypy3.7",
52 "pypy3.8",
53 "pypy3.9",
54 "pypy3.10",
55 "pypy3.11",
56 )
57
58
59 def run_tests_exe(executable, verbose, tested):
60 tested_key = os.path.realpath(executable)
61 if tested_key in tested:
62 return
63
64 # Don't use realpath() for the executed command to support virtual
65 # environments
66 cmd = [executable, TEST_COMPAT]
67 if verbose:
68 cmd.append('-v')
69 run_command(cmd)
70 tested.add(tested_key)
71
72
73 def run_tests(python, verbose, tested):
74 executable = which(python)
75 if not executable:
76 print("Ignore missing Python executable: %s" % python)
77 return
78 run_tests_exe(executable, verbose, tested)
79
80
81 def parse_args():
82 parser = argparse.ArgumentParser()
83 parser.add_argument('-v', '--verbose', action="store_true",
84 help='Verbose mode')
85 parser.add_argument('-c', '--current', action="store_true",
86 help="Only test the current Python executable "
87 "(don't test multiple Python versions)")
88 return parser.parse_args()
89
90
91 def main():
92 args = parse_args()
93
94 path = os.path.join(TEST_DIR, 'build')
95 if os.path.exists(path):
96 shutil.rmtree(path)
97
98 # upgrade_pythoncapi.py requires Python 3.6 or newer
99 if sys.version_info >= (3, 6):
100 print("Run %s" % TEST_UPGRADE)
101 cmd = [sys.executable, TEST_UPGRADE]
102 if args.verbose:
103 cmd.append('-v')
104 run_command(cmd)
105 else:
106 print("Don't test upgrade_pythoncapi.py: it requires Python 3.6")
107 print()
108
109 tested = set()
110 if not args.current:
111 for python in PYTHONS:
112 run_tests(python, args.verbose, tested)
113 run_tests_exe(sys.executable, args.verbose, tested)
114
115 print()
116 print("Tested: %s Python executables" % len(tested))
117 else:
118 run_tests_exe(sys.executable, args.verbose, tested)
119
120
121 if __name__ == "__main__":
122 main()