Skip to content

Commit 2abaf12

Browse files
authored
[3.14] gh-144490: Test the internal C API in test_cppext (#144547)
Backport changes from the main branch.
1 parent 3fc48c1 commit 2abaf12

File tree

3 files changed

+54
-25
lines changed

3 files changed

+54
-25
lines changed

Lib/test/test_cppext/__init__.py

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,12 @@
2424
@support.requires_venv_with_pip()
2525
@support.requires_subprocess()
2626
@support.requires_resource('cpu')
27-
class TestCPPExt(unittest.TestCase):
27+
class BaseTests:
28+
TEST_INTERNAL_C_API = False
29+
2830
def test_build(self):
2931
self.check_build('_testcppext')
3032

31-
def test_build_cpp03(self):
32-
# In public docs, we say C API is compatible with C++11. However,
33-
# in practice we do maintain C++03 compatibility in public headers.
34-
# Please ask the C API WG before adding a new C++11-only feature.
35-
self.check_build('_testcpp03ext', std='c++03')
36-
37-
@support.requires_gil_enabled('incompatible with Free Threading')
38-
def test_build_limited_cpp03(self):
39-
self.check_build('_test_limited_cpp03ext', std='c++03', limited=True)
40-
41-
@unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support /std:c++11")
42-
def test_build_cpp11(self):
43-
self.check_build('_testcpp11ext', std='c++11')
44-
45-
# Only test C++14 on MSVC.
46-
# On s390x RHEL7, GCC 4.8.5 doesn't support C++14.
47-
@unittest.skipIf(not support.MS_WINDOWS, "need Windows")
48-
def test_build_cpp14(self):
49-
self.check_build('_testcpp14ext', std='c++14')
50-
51-
@support.requires_gil_enabled('incompatible with Free Threading')
52-
def test_build_limited(self):
53-
self.check_build('_testcppext_limited', limited=True)
54-
5533
def check_build(self, extension_name, std=None, limited=False):
5634
venv_dir = 'env'
5735
with support.setup_venv_with_pip_setuptools(venv_dir) as python_exe:
@@ -71,6 +49,7 @@ def run_cmd(operation, cmd):
7149
if limited:
7250
env['CPYTHON_TEST_LIMITED'] = '1'
7351
env['CPYTHON_TEST_EXT_NAME'] = extension_name
52+
env['TEST_INTERNAL_C_API'] = str(int(self.TEST_INTERNAL_C_API))
7453
if support.verbose:
7554
print('Run:', ' '.join(map(shlex.quote, cmd)))
7655
subprocess.run(cmd, check=True, env=env)
@@ -111,5 +90,35 @@ def run_cmd(operation, cmd):
11190
run_cmd('Import', cmd)
11291

11392

93+
class TestPublicCAPI(BaseTests, unittest.TestCase):
94+
@support.requires_gil_enabled('incompatible with Free Threading')
95+
def test_build_limited_cpp03(self):
96+
self.check_build('_test_limited_cpp03ext', std='c++03', limited=True)
97+
98+
@support.requires_gil_enabled('incompatible with Free Threading')
99+
def test_build_limited(self):
100+
self.check_build('_testcppext_limited', limited=True)
101+
102+
def test_build_cpp03(self):
103+
# In public docs, we say C API is compatible with C++11. However,
104+
# in practice we do maintain C++03 compatibility in public headers.
105+
# Please ask the C API WG before adding a new C++11-only feature.
106+
self.check_build('_testcpp03ext', std='c++03')
107+
108+
@unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support /std:c++11")
109+
def test_build_cpp11(self):
110+
self.check_build('_testcpp11ext', std='c++11')
111+
112+
# Only test C++14 on MSVC.
113+
# On s390x RHEL7, GCC 4.8.5 doesn't support C++14.
114+
@unittest.skipIf(not support.MS_WINDOWS, "need Windows")
115+
def test_build_cpp14(self):
116+
self.check_build('_testcpp14ext', std='c++14')
117+
118+
119+
class TestInteralCAPI(BaseTests, unittest.TestCase):
120+
TEST_INTERNAL_C_API = True
121+
122+
114123
if __name__ == "__main__":
115124
unittest.main()

Lib/test/test_cppext/extension.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,24 @@
66
// Always enable assertions
77
#undef NDEBUG
88

9+
#ifdef TEST_INTERNAL_C_API
10+
# define Py_BUILD_CORE_MODULE 1
11+
#endif
12+
913
#include "Python.h"
1014

15+
#ifdef TEST_INTERNAL_C_API
16+
// gh-135906: Check for compiler warnings in the internal C API
17+
# include "internal/pycore_frame.h"
18+
// mimalloc emits many compiler warnings when Python is built in debug
19+
// mode (when MI_DEBUG is not zero).
20+
// mimalloc emits compiler warnings when Python is built on Windows.
21+
# if !defined(Py_DEBUG) && !defined(MS_WINDOWS)
22+
# include "internal/pycore_backoff.h"
23+
# include "internal/pycore_cell.h"
24+
# endif
25+
#endif
26+
1127
#ifndef MODULE_NAME
1228
# error "MODULE_NAME macro must be defined"
1329
#endif

Lib/test/test_cppext/setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def main():
4747
std = os.environ.get("CPYTHON_TEST_CPP_STD", "")
4848
module_name = os.environ["CPYTHON_TEST_EXT_NAME"]
4949
limited = bool(os.environ.get("CPYTHON_TEST_LIMITED", ""))
50+
internal = bool(int(os.environ.get("TEST_INTERNAL_C_API", "0")))
5051

5152
cppflags = list(CPPFLAGS)
5253
cppflags.append(f'-DMODULE_NAME={module_name}')
@@ -82,6 +83,9 @@ def main():
8283
version = sys.hexversion
8384
cppflags.append(f'-DPy_LIMITED_API={version:#x}')
8485

86+
if internal:
87+
cppflags.append('-DTEST_INTERNAL_C_API=1')
88+
8589
# On Windows, add PCbuild\amd64\ to include and library directories
8690
include_dirs = []
8791
library_dirs = []

0 commit comments

Comments
 (0)