Skip to content

Commit 305b5de

Browse files
committed
Only test the limited C API on one C and one C++ version
Cleanup test selection in setup.py.
1 parent 4fb3ec6 commit 305b5de

2 files changed

Lines changed: 75 additions & 77 deletions

File tree

‎tests/setup.py‎

Lines changed: 67 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@
2121
else:
2222
TEST_LIMITED_C_API = False
2323

24-
# C++ is only supported on Python 3.6 and newer
25-
TEST_CXX = (sys.version_info >= (3, 6))
26-
2724
SRC_DIR = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
28-
25+
LIMITED_SUFFIX = "_limited"
2926
# Windows uses MSVC compiler
3027
MSVC = (os.name == "nt")
3128

@@ -64,43 +61,51 @@
6461
CFLAGS = list(COMMON_FLAGS)
6562
CXXFLAGS = list(COMMON_FLAGS)
6663

64+
65+
# C extensions
66+
C_EXTENSION_PREFIX = 'test_pythoncapi_compat_cext_'
6767
if not MSVC:
68-
C_VERSIONS = ('c99', 'c11')
68+
C_TESTS = ('c99', 'c11')
6969
else:
7070
# MSVC doesn't support /std:c99 flag
71-
C_VERSIONS = ('c11',)
72-
C_EXTENSION_PREFIX = 'test_pythoncapi_compat_cext_'
73-
C_VERSIONS = [(C_EXTENSION_PREFIX + std, std) for std in C_VERSIONS]
71+
C_TESTS = ('c11',)
72+
C_TESTS = [(C_EXTENSION_PREFIX + std, std, False) for std in C_TESTS]
73+
if TEST_LIMITED_C_API:
74+
C_TESTS.append((C_EXTENSION_PREFIX + LIMITED_SUFFIX, None, True))
7475

75-
LIMITED_SUFFIX = "_limited"
76+
77+
# C++ extensions
78+
CXX_EXTENSION_PREFIX = 'test_pythoncapi_compat_cppext'
79+
def cxx_extension_name(std):
80+
if not std:
81+
return CXX_EXTENSION_PREFIX
82+
83+
if std.startswith("c++"):
84+
std = std[3:]
85+
else:
86+
raise ValueError(f"invalid options: {std!r}")
87+
return CXX_EXTENSION_PREFIX + std
7688

7789
if not MSVC:
78-
CXX_VERSIONS = [
90+
CXX_TESTS = [
7991
'c++03',
8092
'c++11',
8193
'c++14',
8294
'c++17',
8395
'c++20',
8496
]
97+
CXX_DEFAULT_STD = 'c++11'
8598
else:
8699
# MSVC doesn't support /std:c++11
87-
CXX_VERSIONS = [
100+
CXX_TESTS = [
88101
None,
89102
'c++14',
90103
]
91-
CXX_EXTENSION_PREFIX = 'test_pythoncapi_compat_cppext'
92-
93-
def cxx_extension_name(std):
94-
if not std:
95-
return CXX_EXTENSION_PREFIX
96-
97-
if std.startswith("c++"):
98-
std = std[3:]
99-
else:
100-
raise ValueError(f"invalid options: {std!r}")
101-
return CXX_EXTENSION_PREFIX + std
102-
103-
CXX_VERSIONS = [(cxx_extension_name(options), options) for options in CXX_VERSIONS]
104+
CXX_DEFAULT_STD = 'c++14'
105+
CXX_TESTS = [(cxx_extension_name(options), options, False)
106+
for options in CXX_TESTS]
107+
if TEST_LIMITED_C_API:
108+
CXX_TESTS.append((CXX_EXTENSION_PREFIX + LIMITED_SUFFIX, CXX_DEFAULT_STD, True))
104109

105110
DEBUG_FLAGS = ('-O0', '-ggdb')
106111

@@ -113,6 +118,7 @@ def main():
113118

114119
cflags = list(CFLAGS)
115120
cxxflags = list(CXXFLAGS)
121+
limited_flag = f'-DPy_LIMITED_API={sys.hexversion:#x}'
116122

117123
# gh-105776: When "gcc -std=11" is used as the C++ compiler, -std=c11
118124
# option emits a C++ compiler warning. Remove "-std11" option from the
@@ -128,54 +134,47 @@ def main():
128134
# CC env var overrides sysconfig CC variable in setuptools
129135
os.environ['CC'] = cmd
130136

131-
if DEBUG:
132-
cflags.extend(DEBUG_FLAGS)
133-
cxxflags.extend(DEBUG_FLAGS)
134-
135-
if TEST_LIMITED_C_API:
136-
limited = f'-DPy_LIMITED_API={sys.hexversion:#x}'
137-
cflags.append(limited)
138-
cxxflags.append(limited)
137+
def add_common_flags(flags, limited):
138+
if limited:
139+
flags.append(limited_flag)
140+
if DEBUG:
141+
flags.extend(DEBUG_FLAGS)
139142

140143
# C extension
141144
extensions = []
142145
sources = ['test_pythoncapi_compat_cext.c']
143-
for name, std in C_VERSIONS:
144-
if not MSVC:
145-
flags = cflags + [f'-std={std}']
146-
else:
147-
flags = cflags + [f'/std:{std}']
148-
149-
def add_extension(name):
150-
ext_flags = [*flags, f'-DMODULE_NAME={name}']
151-
ext = Extension(name, sources=sources, extra_compile_args=ext_flags)
152-
extensions.append(ext)
153-
154-
add_extension(name)
155-
if TEST_LIMITED_C_API:
156-
add_extension(name + LIMITED_SUFFIX)
157-
158-
if TEST_CXX:
159-
# C++ extension
160-
sources = ['test_pythoncapi_compat_cppext.cpp']
161-
for name, std in CXX_VERSIONS:
162-
flags = list(cxxflags)
163-
if std is not None:
164-
if MSVC:
165-
std_flags = [f'/std:{std}', '/Zc:__cplusplus']
166-
else:
167-
std_flags = [f'-std={std}']
168-
flags.extend(std_flags)
169-
170-
def add_extension(name):
171-
ext_flags = [*flags, f'-DMODULE_NAME={name}']
172-
ext = Extension(name, sources=sources,
173-
extra_compile_args=ext_flags, language='c++')
174-
extensions.append(ext)
175-
176-
add_extension(name)
177-
if TEST_LIMITED_C_API:
178-
add_extension(name + LIMITED_SUFFIX)
146+
for name, std, limited in C_TESTS:
147+
flags = [*cflags, f'-DMODULE_NAME={name}']
148+
if std:
149+
if not MSVC:
150+
flags.append(f'-std={std}')
151+
else:
152+
flags.append(f'/std:{std}')
153+
add_common_flags(flags, limited)
154+
155+
ext = Extension(
156+
name,
157+
sources=sources,
158+
extra_compile_args=flags)
159+
extensions.append(ext)
160+
161+
# C++ extension
162+
sources = ['test_pythoncapi_compat_cppext.cpp']
163+
for name, std, limited in CXX_TESTS:
164+
flags = [*cxxflags, f'-DMODULE_NAME={name}']
165+
if std:
166+
if MSVC:
167+
flags.extend([f'/std:{std}', '/Zc:__cplusplus'])
168+
else:
169+
flags.append(f'-std={std}')
170+
add_common_flags(flags, limited)
171+
172+
ext = Extension(
173+
name,
174+
sources=sources,
175+
extra_compile_args=flags,
176+
language='c++')
177+
extensions.append(ext)
179178

180179
setup(name="test_pythoncapi_compat", ext_modules=extensions)
181180

‎tests/test_pythoncapi_compat.py‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,13 @@ def python_version():
153153

154154

155155
def run_tests(module_name, std):
156-
lang = std.upper()
156+
lang = std.upper() if std else None
157157
if VERBOSE:
158158
print("")
159159

160-
title = "Test %s (%s)" % (module_name, lang)
160+
title = f"Test {module_name}"
161+
if lang:
162+
titlte = f"{title} ({lang})"
161163
display_title(title)
162164

163165
testmod = import_tests(module_name)
@@ -196,10 +198,9 @@ def test_func():
196198
if VERBOSE:
197199
print()
198200

199-
msg = "%s %s tests succeeded!" % (len(tests), lang)
200-
msg = "%s: %s" % (python_version(), msg)
201+
msg = f"{python_version()}, {module_name}: {len(tests)} tests succeeded!"
201202
if check_refleak:
202-
msg = "%s (no reference leak detected)" % msg
203+
msg += " (no reference leak detected)"
203204
print(msg)
204205

205206

@@ -220,11 +221,9 @@ def main():
220221

221222
build_ext()
222223

223-
tests = setup.C_VERSIONS + setup.CXX_VERSIONS
224-
for module_name, std in tests:
224+
tests = setup.C_TESTS + setup.CXX_TESTS
225+
for module_name, std, limited in tests:
225226
run_tests(module_name, std)
226-
if setup.TEST_LIMITED_C_API:
227-
run_tests(module_name + "_limited", std)
228227

229228

230229
if __name__ == "__main__":

0 commit comments

Comments
 (0)