Skip to content

Commit 87eb7e1

Browse files
committed
gh-135906: Test the internal C API in test_cext
1 parent da79ac9 commit 87eb7e1

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

Lib/test/test_cext/__init__.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
@support.requires_venv_with_pip()
2929
@support.requires_subprocess()
3030
@support.requires_resource('cpu')
31-
class TestExt(unittest.TestCase):
31+
class BaseTests:
32+
TEST_INTERNAL_C_API = False
33+
3234
# Default build with no options
3335
def test_build(self):
3436
self.check_build('_test_cext')
@@ -43,14 +45,6 @@ def test_build_c99(self):
4345
# Please ask the C API WG before adding a new C11-only feature.
4446
self.check_build('_test_c99_cext', std='c99')
4547

46-
@support.requires_gil_enabled('incompatible with Free Threading')
47-
def test_build_limited(self):
48-
self.check_build('_test_limited_cext', limited=True)
49-
50-
@support.requires_gil_enabled('broken for now with Free Threading')
51-
def test_build_limited_c11(self):
52-
self.check_build('_test_limited_c11_cext', limited=True, std='c11')
53-
5448
def check_build(self, extension_name, std=None, limited=False):
5549
venv_dir = 'env'
5650
with support.setup_venv_with_pip_setuptools(venv_dir) as python_exe:
@@ -70,6 +64,7 @@ def run_cmd(operation, cmd):
7064
if limited:
7165
env['CPYTHON_TEST_LIMITED'] = '1'
7266
env['CPYTHON_TEST_EXT_NAME'] = extension_name
67+
env['TEST_INTERNAL_C_API'] = str(int(self.TEST_INTERNAL_C_API))
7368
if support.verbose:
7469
print('Run:', ' '.join(map(shlex.quote, cmd)))
7570
subprocess.run(cmd, check=True, env=env)
@@ -110,5 +105,19 @@ def run_cmd(operation, cmd):
110105
run_cmd('Import', cmd)
111106

112107

108+
class TestPublicCAPI(BaseTests, unittest.TestCase):
109+
@support.requires_gil_enabled('incompatible with Free Threading')
110+
def test_build_limited(self):
111+
self.check_build('_test_limited_cext', limited=True)
112+
113+
@support.requires_gil_enabled('broken for now with Free Threading')
114+
def test_build_limited_c11(self):
115+
self.check_build('_test_limited_c11_cext', limited=True, std='c11')
116+
117+
118+
class TestInteralCAPI(BaseTests, unittest.TestCase):
119+
TEST_INTERNAL_C_API = True
120+
121+
113122
if __name__ == "__main__":
114123
unittest.main()

Lib/test/test_cext/extension.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
// gh-116869: Basic C test extension to check that the Python C API
22
// does not emit C compiler warnings.
3+
//
4+
// Test also the internal C API if the TEST_INTERNAL_C_API macro is defined.
35

46
// Always enable assertions
57
#undef NDEBUG
68

9+
#ifdef TEST_INTERNAL_C_API
10+
# define Py_BUILD_CORE 1
11+
#endif
12+
713
#include "Python.h"
814

15+
#ifdef TEST_INTERNAL_C_API
16+
// gh-135906: Check for compiler warnings in the internal C API.
17+
// - Cython uses pycore_frame.h.
18+
// - greenlet uses pycore_frame.h, pycore_interpframe_structs.h and
19+
// pycore_interpframe.h.
20+
# include "internal/pycore_frame.h"
21+
# include "internal/pycore_gc.h"
22+
# include "internal/pycore_interp.h"
23+
# include "internal/pycore_interpframe.h"
24+
# include "internal/pycore_interpframe_structs.h"
25+
# include "internal/pycore_object.h"
26+
# include "internal/pycore_pystate.h"
27+
#endif
28+
929
#ifndef MODULE_NAME
1030
# error "MODULE_NAME macro must be defined"
1131
#endif

Lib/test/test_cext/setup.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414

1515
if not support.MS_WINDOWS:
1616
# C compiler flags for GCC and clang
17-
CFLAGS = [
17+
BASE_CFLAGS = [
1818
# The purpose of test_cext extension is to check that building a C
1919
# extension using the Python C API does not emit C compiler warnings.
2020
'-Werror',
21+
]
22+
23+
# C compiler flags for GCC and clang
24+
PUBLIC_CFLAGS = [
25+
*BASE_CFLAGS,
2126

2227
# gh-120593: Check the 'const' qualifier
2328
'-Wcast-qual',
@@ -26,27 +31,33 @@
2631
'-pedantic-errors',
2732
]
2833
if not support.Py_GIL_DISABLED:
29-
CFLAGS.append(
34+
PUBLIC_CFLAGS.append(
3035
# gh-116869: The Python C API must be compatible with building
3136
# with the -Werror=declaration-after-statement compiler flag.
3237
'-Werror=declaration-after-statement',
3338
)
3439
else:
3540
# MSVC compiler flags
36-
CFLAGS = [
41+
BASE_CFLAGS = [
3742
# Display warnings level 1 to 4
3843
'/W4',
3944
# Treat all compiler warnings as compiler errors
4045
'/WX',
4146
]
47+
PUBLIC_CFLAGS = [*BASE_CFLAGS]
48+
INTERNAL_CFLAGS = [*BASE_CFLAGS]
4249

4350

4451
def main():
4552
std = os.environ.get("CPYTHON_TEST_STD", "")
4653
module_name = os.environ["CPYTHON_TEST_EXT_NAME"]
4754
limited = bool(os.environ.get("CPYTHON_TEST_LIMITED", ""))
55+
internal = bool(int(os.environ.get("TEST_INTERNAL_C_API", "0")))
4856

49-
cflags = list(CFLAGS)
57+
if not internal:
58+
cflags = list(PUBLIC_CFLAGS)
59+
else:
60+
cflags = list(INTERNAL_CFLAGS)
5061
cflags.append(f'-DMODULE_NAME={module_name}')
5162

5263
# Add -std=STD or /std:STD (MSVC) compiler flag
@@ -75,6 +86,9 @@ def main():
7586
version = sys.hexversion
7687
cflags.append(f'-DPy_LIMITED_API={version:#x}')
7788

89+
if internal:
90+
cflags.append('-DTEST_INTERNAL_C_API=1')
91+
7892
# On Windows, add PCbuild\amd64\ to include and library directories
7993
include_dirs = []
8094
library_dirs = []

0 commit comments

Comments
 (0)