2323from utils import run_command , command_stdout
2424
2525
26- # C++ is only supported on Python 3.6 and newer
27- TEST_CPP = (sys .version_info >= (3 , 6 ))
26+ TESTS = [
27+ ("test_pythoncapi_compat_cext" , "C" ),
28+ ("test_pythoncapi_compat_cppext" , "C++" ),
29+ ("test_pythoncapi_compat_cpp03ext" , "C++03" ),
30+ ("test_pythoncapi_compat_cpp11ext" , "C++11" ),
31+ ]
32+
2833VERBOSE = False
2934
3035
@@ -42,15 +47,10 @@ def display_title(title):
4247
4348
4449def build_ext ():
45- if TEST_CPP :
46- display_title ("Build the C and C++ extensions" )
47- else :
48- display_title ("Build the C extension" )
50+ display_title ("Build test extensions" )
4951 if os .path .exists ("build" ):
5052 shutil .rmtree ("build" )
5153 cmd = [sys .executable , "setup.py" , "build" ]
52- if TEST_CPP :
53- cmd .append ('--build-cppext' )
5454 if VERBOSE :
5555 run_command (cmd )
5656 print ()
@@ -98,15 +98,42 @@ def _check_refleak(test_func, verbose):
9898 raise AssertionError ("refcnt leak, diff: %s" % diff )
9999
100100
101- def run_tests (module_name ):
102- if "cppext" in module_name :
103- lang = "C++"
101+ def python_version ():
102+ ver = sys .version_info
103+ build = 'debug' if hasattr (sys , 'gettotalrefcount' ) else 'release'
104+ if hasattr (sys , 'implementation' ):
105+ python_impl = sys .implementation .name
106+ if python_impl == 'cpython' :
107+ python_impl = 'CPython'
108+ elif python_impl == 'pypy' :
109+ python_impl = 'PyPy'
104110 else :
105- lang = "C"
111+ if "PyPy" in sys .version :
112+ python_impl = "PyPy"
113+ else :
114+ python_impl = 'Python'
115+ return "%s %s.%s (%s build)" % (python_impl , ver .major , ver .minor , build )
116+
117+
118+ def run_tests (module_name , lang ):
106119 title = "Test %s (%s)" % (module_name , lang )
107120 display_title (title )
108121
109- testmod = import_tests (module_name )
122+ try :
123+ testmod = import_tests (module_name )
124+ except ImportError :
125+ # The C extension must always be available
126+ if lang == "C" :
127+ raise
128+
129+ if VERBOSE :
130+ print ("%s: skip %s, missing %s extension"
131+ % (python_version (), lang , module_name ))
132+ print ()
133+ return
134+
135+ if VERBOSE and hasattr (testmod , "__cplusplus" ):
136+ print ("__cplusplus: %s" % testmod .__cplusplus )
110137
111138 check_refleak = hasattr (sys , 'gettotalrefcount' )
112139
@@ -125,21 +152,8 @@ def test_func():
125152 if VERBOSE :
126153 print ()
127154
128- ver = sys .version_info
129- build = 'debug' if hasattr (sys , 'gettotalrefcount' ) else 'release'
130155 msg = "%s %s tests succeeded!" % (len (tests ), lang )
131- if hasattr (sys , 'implementation' ):
132- python_impl = sys .implementation .name
133- if python_impl == 'cpython' :
134- python_impl = 'CPython'
135- elif python_impl == 'pypy' :
136- python_impl = 'PyPy'
137- else :
138- if "PyPy" in sys .version :
139- python_impl = "PyPy"
140- else :
141- python_impl = 'Python'
142- msg = "%s %s.%s (%s build): %s" % (python_impl , ver .major , ver .minor , build , msg )
156+ msg = "%s: %s" % (python_version (), msg )
143157 if check_refleak :
144158 msg = "%s (no reference leak detected)" % msg
145159 print (msg )
@@ -150,9 +164,8 @@ def main():
150164 VERBOSE = ("-v" in sys .argv [1 :] or "--verbose" in sys .argv [1 :])
151165
152166 # Implementing PyFrame_GetLocals() and PyCode_GetCode() require the
153- # internal C API in Python 3.11 alpha versions. Skip also Python 3.11b1
154- # which has issues with C++ casts: _Py_CAST() macro.
155- if 0x30b0000 <= sys .hexversion <= 0x30b00b1 :
167+ # internal C API in Python 3.11 alpha versions.
168+ if 0x30b0000 <= sys .hexversion < 0x30b00b1 :
156169 version = sys .version .split ()[0 ]
157170 print ("SKIP TESTS: Python %s is not supported" % version )
158171 return
@@ -166,9 +179,8 @@ def main():
166179
167180 build_ext ()
168181
169- run_tests ("test_pythoncapi_compat_cext" )
170- if TEST_CPP :
171- run_tests ("test_pythoncapi_compat_cppext" )
182+ for module_name , lang in TESTS :
183+ run_tests (module_name , lang )
172184
173185
174186if __name__ == "__main__" :
0 commit comments