Skip to content

gh-135906: Test the internal C API in test_cext #136247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-135906: Test the internal C API in test_cext
  • Loading branch information
vstinner committed Jul 3, 2025
commit 87eb7e1a03c007e52065f6bba363a3fcad6b32cc
27 changes: 18 additions & 9 deletions Lib/test/test_cext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
@support.requires_venv_with_pip()
@support.requires_subprocess()
@support.requires_resource('cpu')
class TestExt(unittest.TestCase):
class BaseTests:
TEST_INTERNAL_C_API = False

# Default build with no options
def test_build(self):
self.check_build('_test_cext')
Expand All @@ -43,14 +45,6 @@ def test_build_c99(self):
# Please ask the C API WG before adding a new C11-only feature.
self.check_build('_test_c99_cext', std='c99')

@support.requires_gil_enabled('incompatible with Free Threading')
def test_build_limited(self):
self.check_build('_test_limited_cext', limited=True)

@support.requires_gil_enabled('broken for now with Free Threading')
def test_build_limited_c11(self):
self.check_build('_test_limited_c11_cext', limited=True, std='c11')

def check_build(self, extension_name, std=None, limited=False):
venv_dir = 'env'
with support.setup_venv_with_pip_setuptools(venv_dir) as python_exe:
Expand All @@ -70,6 +64,7 @@ def run_cmd(operation, cmd):
if limited:
env['CPYTHON_TEST_LIMITED'] = '1'
env['CPYTHON_TEST_EXT_NAME'] = extension_name
env['TEST_INTERNAL_C_API'] = str(int(self.TEST_INTERNAL_C_API))
if support.verbose:
print('Run:', ' '.join(map(shlex.quote, cmd)))
subprocess.run(cmd, check=True, env=env)
Expand Down Expand Up @@ -110,5 +105,19 @@ def run_cmd(operation, cmd):
run_cmd('Import', cmd)


class TestPublicCAPI(BaseTests, unittest.TestCase):
@support.requires_gil_enabled('incompatible with Free Threading')
def test_build_limited(self):
self.check_build('_test_limited_cext', limited=True)

@support.requires_gil_enabled('broken for now with Free Threading')
def test_build_limited_c11(self):
self.check_build('_test_limited_c11_cext', limited=True, std='c11')


class TestInteralCAPI(BaseTests, unittest.TestCase):
TEST_INTERNAL_C_API = True


if __name__ == "__main__":
unittest.main()
20 changes: 20 additions & 0 deletions Lib/test/test_cext/extension.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
// gh-116869: Basic C test extension to check that the Python C API
// does not emit C compiler warnings.
//
// Test also the internal C API if the TEST_INTERNAL_C_API macro is defined.

// Always enable assertions
#undef NDEBUG

#ifdef TEST_INTERNAL_C_API
# define Py_BUILD_CORE 1
#endif

#include "Python.h"

#ifdef TEST_INTERNAL_C_API
// gh-135906: Check for compiler warnings in the internal C API.
// - Cython uses pycore_frame.h.
// - greenlet uses pycore_frame.h, pycore_interpframe_structs.h and
// pycore_interpframe.h.
# include "internal/pycore_frame.h"
# include "internal/pycore_gc.h"
# include "internal/pycore_interp.h"
# include "internal/pycore_interpframe.h"
# include "internal/pycore_interpframe_structs.h"
# include "internal/pycore_object.h"
# include "internal/pycore_pystate.h"
#endif

#ifndef MODULE_NAME
# error "MODULE_NAME macro must be defined"
#endif
Expand Down
22 changes: 18 additions & 4 deletions Lib/test/test_cext/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@

if not support.MS_WINDOWS:
# C compiler flags for GCC and clang
CFLAGS = [
BASE_CFLAGS = [
# The purpose of test_cext extension is to check that building a C
# extension using the Python C API does not emit C compiler warnings.
'-Werror',
]

# C compiler flags for GCC and clang
PUBLIC_CFLAGS = [
*BASE_CFLAGS,

# gh-120593: Check the 'const' qualifier
'-Wcast-qual',
Expand All @@ -26,27 +31,33 @@
'-pedantic-errors',
]
if not support.Py_GIL_DISABLED:
CFLAGS.append(
PUBLIC_CFLAGS.append(
# gh-116869: The Python C API must be compatible with building
# with the -Werror=declaration-after-statement compiler flag.
'-Werror=declaration-after-statement',
)
else:
# MSVC compiler flags
CFLAGS = [
BASE_CFLAGS = [
# Display warnings level 1 to 4
'/W4',
# Treat all compiler warnings as compiler errors
'/WX',
]
PUBLIC_CFLAGS = [*BASE_CFLAGS]
INTERNAL_CFLAGS = [*BASE_CFLAGS]


def main():
std = os.environ.get("CPYTHON_TEST_STD", "")
module_name = os.environ["CPYTHON_TEST_EXT_NAME"]
limited = bool(os.environ.get("CPYTHON_TEST_LIMITED", ""))
internal = bool(int(os.environ.get("TEST_INTERNAL_C_API", "0")))

cflags = list(CFLAGS)
if not internal:
cflags = list(PUBLIC_CFLAGS)
else:
cflags = list(INTERNAL_CFLAGS)
cflags.append(f'-DMODULE_NAME={module_name}')

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

if internal:
cflags.append('-DTEST_INTERNAL_C_API=1')

# On Windows, add PCbuild\amd64\ to include and library directories
include_dirs = []
library_dirs = []
Expand Down
Loading