Skip to content

Commit 6290c7b

Browse files
authored
Tests C11, C++17 and C++20 (#167)
1 parent 5fc6653 commit 6290c7b

File tree

3 files changed

+74
-48
lines changed

3 files changed

+74
-48
lines changed

tests/setup.py

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@
4040
'-Wformat-nonliteral',
4141
'-Wformat-security',
4242
))
43-
CFLAGS = COMMON_FLAGS + [
44-
# Use C99 for pythoncapi_compat.c which initializes PyModuleDef with a
45-
# mixture of designated and non-designated initializers
46-
'-std=c99',
47-
]
43+
CFLAGS = COMMON_FLAGS
4844
else:
4945
# C compiler flags for MSVC
5046
COMMON_FLAGS.extend((
@@ -54,6 +50,27 @@
5450
CFLAGS = list(COMMON_FLAGS)
5551
CXXFLAGS = list(COMMON_FLAGS)
5652

53+
if not MSVC:
54+
C_VERSIONS = ('c99', 'c11')
55+
else:
56+
# MSVC doesn't support /std:c99 flag
57+
C_VERSIONS = ('c11',)
58+
59+
if not MSVC:
60+
CXX_VERSIONS = [
61+
('test_pythoncapi_compat_cpp03ext', ['-std=c++03']),
62+
('test_pythoncapi_compat_cpp11ext', ['-std=c++11']),
63+
('test_pythoncapi_compat_cpp14ext', ['-std=c++14']),
64+
('test_pythoncapi_compat_cpp17ext', ['-std=c++17']),
65+
('test_pythoncapi_compat_cpp20ext', ['-std=c++20']),
66+
]
67+
else:
68+
# MSVC doesn't support /std:c++11
69+
CXX_VERSIONS = [
70+
('test_pythoncapi_compat_cppext', None),
71+
('test_pythoncapi_compat_cpp14ext', ['/std:c++14', '/Zc:__cplusplus']),
72+
]
73+
5774

5875
def main():
5976
# gh-105776: When "gcc -std=11" is used as the C++ compiler, -std=c11
@@ -75,27 +92,21 @@ def main():
7592
os.environ['CC'] = cmd
7693

7794
# C extension
78-
c_ext = Extension(
79-
'test_pythoncapi_compat_cext',
80-
sources=['test_pythoncapi_compat_cext.c'],
81-
extra_compile_args=CFLAGS)
82-
extensions = [c_ext]
95+
extensions = []
96+
for std in C_VERSIONS:
97+
if not MSVC:
98+
cflags = CFLAGS + ['-std=%s' % std]
99+
else:
100+
cflags = CFLAGS + ['/std:%s' % std]
101+
c_ext = Extension(
102+
'test_pythoncapi_compat_cext_%s' % std,
103+
sources=['test_pythoncapi_compat_cext.c'],
104+
extra_compile_args=cflags)
105+
extensions.append(c_ext)
83106

84107
if TEST_CXX:
85108
# C++ extension
86-
87-
# MSVC has /std flag but doesn't support /std:c++11
88-
if not MSVC:
89-
versions = [
90-
('test_pythoncapi_compat_cpp03ext', ['-std=c++03']),
91-
('test_pythoncapi_compat_cpp11ext', ['-std=c++11']),
92-
]
93-
else:
94-
versions = [
95-
('test_pythoncapi_compat_cppext', None),
96-
('test_pythoncapi_compat_cpp14ext', ['/std:c++14', '/Zc:__cplusplus']),
97-
]
98-
for name, std_flags in versions:
109+
for name, std_flags in CXX_VERSIONS:
99110
flags = list(CXXFLAGS)
100111
if std_flags is not None:
101112
flags.extend(std_flags)

tests/test_pythoncapi_compat.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,29 @@
3232
# Windows uses MSVC compiler
3333
MSVC = (os.name == "nt")
3434

35-
TESTS = [
36-
("test_pythoncapi_compat_cext", "C"),
37-
]
35+
# C++ is only supported on Python 3.6 and newer
36+
TEST_CXX = (sys.version_info >= (3, 6))
37+
3838
if not MSVC:
39-
TESTS.extend((
39+
C_TESTS = [
40+
("test_pythoncapi_compat_cext_c99", "C99"),
41+
("test_pythoncapi_compat_cext_c11", "C11"),
42+
]
43+
CXX_TESTS = [
4044
("test_pythoncapi_compat_cpp03ext", "C++03"),
4145
("test_pythoncapi_compat_cpp11ext", "C++11"),
42-
))
46+
("test_pythoncapi_compat_cpp14ext", "C++14"),
47+
("test_pythoncapi_compat_cpp17ext", "C++17"),
48+
("test_pythoncapi_compat_cpp20ext", "C++20"),
49+
]
4350
else:
44-
TESTS.extend((
51+
C_TESTS = [
52+
("test_pythoncapi_compat_cext_c11", "C11"),
53+
]
54+
CXX_TESTS = [
4555
("test_pythoncapi_compat_cppext", "C++"),
4656
("test_pythoncapi_compat_cpp14ext", "C++14"),
47-
))
57+
]
4858

4959

5060
VERBOSE = False
@@ -84,9 +94,12 @@ def import_tests(module_name):
8494

8595
if not pythonpath:
8696
raise Exception("Failed to find the build directory")
87-
sys.path.append(pythonpath)
88-
89-
return __import__(module_name)
97+
old_sys_path = list(sys.path)
98+
try:
99+
sys.path.append(pythonpath)
100+
return __import__(module_name)
101+
finally:
102+
sys.path[:] = old_sys_path
90103

91104

92105
def _run_tests(tests, verbose):
@@ -155,18 +168,7 @@ def run_tests(module_name, lang):
155168
title = "Test %s (%s)" % (module_name, lang)
156169
display_title(title)
157170

158-
try:
159-
testmod = import_tests(module_name)
160-
except ImportError:
161-
# The C extension must always be available
162-
if lang == "C":
163-
raise
164-
165-
if VERBOSE:
166-
print("%s: skip %s, missing %s extension"
167-
% (python_version(), lang, module_name))
168-
print()
169-
return
171+
testmod = import_tests(module_name)
170172

171173
if VERBOSE:
172174
empty_line = False
@@ -226,7 +228,10 @@ def main():
226228

227229
build_ext()
228230

229-
for module_name, lang in TESTS:
231+
tests = list(C_TESTS)
232+
if TEST_CXX:
233+
tests += CXX_TESTS
234+
for module_name, lang in tests:
230235
run_tests(module_name, lang)
231236

232237

tests/test_pythoncapi_compat_cext.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,24 @@
1616
# define PYTHON3 1
1717
#endif
1818

19-
#if defined(__cplusplus) && __cplusplus >= 201402
19+
#if defined(__cplusplus) && __cplusplus >= 202002L
20+
# define MODULE_NAME test_pythoncapi_compat_cpp20ext
21+
#elif defined(__cplusplus) && __cplusplus >= 201703L
22+
# define MODULE_NAME test_pythoncapi_compat_cpp17ext
23+
#elif defined(__cplusplus) && __cplusplus >= 201402L
2024
# define MODULE_NAME test_pythoncapi_compat_cpp14ext
21-
#elif defined(__cplusplus) && __cplusplus >= 201103
25+
#elif defined(__cplusplus) && __cplusplus >= 201103L
2226
# define MODULE_NAME test_pythoncapi_compat_cpp11ext
2327
#elif defined(__cplusplus) && !defined(_MSC_VER)
2428
# define MODULE_NAME test_pythoncapi_compat_cpp03ext
2529
#elif defined(__cplusplus)
2630
# define MODULE_NAME test_pythoncapi_compat_cppext
31+
#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
32+
# define MODULE_NAME test_pythoncapi_compat_cext_c23
33+
#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
34+
# define MODULE_NAME test_pythoncapi_compat_cext_c11
35+
#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
36+
# define MODULE_NAME test_pythoncapi_compat_cext_c99
2737
#else
2838
# define MODULE_NAME test_pythoncapi_compat_cext
2939
#endif

0 commit comments

Comments
 (0)