Skip to content

Commit 487911d

Browse files
committed
gh-127902: Backport test_cext from the main branch
1 parent c77bfd7 commit 487911d

File tree

4 files changed

+306
-0
lines changed

4 files changed

+306
-0
lines changed

Lib/test/test_cext/__init__.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# gh-116869: Build a basic C test extension to check that the Python C API
2+
# does not emit C compiler warnings.
3+
#
4+
# The Python C API must be compatible with building
5+
# with the -Werror=declaration-after-statement compiler flag.
6+
7+
import os.path
8+
import shlex
9+
import shutil
10+
import subprocess
11+
import unittest
12+
from test import support
13+
14+
15+
SOURCE = os.path.join(os.path.dirname(__file__), 'extension.c')
16+
SETUP = os.path.join(os.path.dirname(__file__), 'setup.py')
17+
18+
19+
# With MSVC on a debug build, the linker fails with: cannot open file
20+
# 'python311.lib', it should look 'python311_d.lib'.
21+
@unittest.skipIf(support.MS_WINDOWS and support.Py_DEBUG,
22+
'test fails on Windows debug build')
23+
# Building and running an extension in clang sanitizing mode is not
24+
# straightforward
25+
@support.skip_if_sanitizer('test does not work with analyzing builds',
26+
address=True, memory=True, ub=True, thread=True)
27+
# the test uses venv+pip: skip if it's not available
28+
@support.requires_venv_with_pip()
29+
@support.requires_subprocess()
30+
@support.requires_resource('cpu')
31+
class TestExt(unittest.TestCase):
32+
# Default build with no options
33+
def test_build(self):
34+
self.check_build('_test_cext')
35+
36+
def test_build_c11(self):
37+
self.check_build('_test_c11_cext', std='c11')
38+
39+
@unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support /std:c99")
40+
def test_build_c99(self):
41+
self.check_build('_test_c99_cext', std='c99')
42+
43+
def test_build_limited(self):
44+
self.check_build('_test_limited_cext', limited=True)
45+
46+
def test_build_limited_c11(self):
47+
self.check_build('_test_limited_c11_cext', limited=True, std='c11')
48+
49+
def check_build(self, extension_name, std=None, limited=False):
50+
venv_dir = 'env'
51+
with support.setup_venv_with_pip_setuptools_wheel(venv_dir) as python_exe:
52+
self._check_build(extension_name, python_exe,
53+
std=std, limited=limited)
54+
55+
def _check_build(self, extension_name, python_exe, std, limited):
56+
pkg_dir = 'pkg'
57+
os.mkdir(pkg_dir)
58+
shutil.copy(SETUP, os.path.join(pkg_dir, os.path.basename(SETUP)))
59+
shutil.copy(SOURCE, os.path.join(pkg_dir, os.path.basename(SOURCE)))
60+
61+
def run_cmd(operation, cmd):
62+
env = os.environ.copy()
63+
if std:
64+
env['CPYTHON_TEST_STD'] = std
65+
if limited:
66+
env['CPYTHON_TEST_LIMITED'] = '1'
67+
env['CPYTHON_TEST_EXT_NAME'] = extension_name
68+
if support.verbose:
69+
print('Run:', ' '.join(map(shlex.quote, cmd)))
70+
subprocess.run(cmd, check=True, env=env)
71+
else:
72+
proc = subprocess.run(cmd,
73+
env=env,
74+
stdout=subprocess.PIPE,
75+
stderr=subprocess.STDOUT,
76+
text=True)
77+
if proc.returncode:
78+
print('Run:', ' '.join(map(shlex.quote, cmd)))
79+
print(proc.stdout, end='')
80+
self.fail(
81+
f"{operation} failed with exit code {proc.returncode}")
82+
83+
# Build and install the C extension
84+
cmd = [python_exe, '-X', 'dev',
85+
'-m', 'pip', 'install', '--no-build-isolation',
86+
os.path.abspath(pkg_dir)]
87+
if support.verbose:
88+
cmd.append('-v')
89+
run_cmd('Install', cmd)
90+
91+
# Do a reference run. Until we test that running python
92+
# doesn't leak references (gh-94755), run it so one can manually check
93+
# -X showrefcount results against this baseline.
94+
cmd = [python_exe,
95+
'-X', 'dev',
96+
'-X', 'showrefcount',
97+
'-c', 'pass']
98+
run_cmd('Reference run', cmd)
99+
100+
# Import the C extension
101+
cmd = [python_exe,
102+
'-X', 'dev',
103+
'-X', 'showrefcount',
104+
'-c', f"import {extension_name}"]
105+
run_cmd('Import', cmd)
106+
107+
108+
if __name__ == "__main__":
109+
unittest.main()

Lib/test/test_cext/extension.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// gh-116869: Basic C test extension to check that the Python C API
2+
// does not emit C compiler warnings.
3+
4+
// Always enable assertions
5+
#undef NDEBUG
6+
7+
#include "Python.h"
8+
9+
#ifndef MODULE_NAME
10+
# error "MODULE_NAME macro must be defined"
11+
#endif
12+
13+
#define _STR(NAME) #NAME
14+
#define STR(NAME) _STR(NAME)
15+
16+
PyDoc_STRVAR(_testcext_add_doc,
17+
"add(x, y)\n"
18+
"\n"
19+
"Return the sum of two integers: x + y.");
20+
21+
static PyObject *
22+
_testcext_add(PyObject *Py_UNUSED(module), PyObject *args)
23+
{
24+
long i, j, res;
25+
if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) {
26+
return NULL;
27+
}
28+
res = i + j;
29+
return PyLong_FromLong(res);
30+
}
31+
32+
33+
static PyMethodDef _testcext_methods[] = {
34+
{"add", _testcext_add, METH_VARARGS, _testcext_add_doc},
35+
{NULL, NULL, 0, NULL} // sentinel
36+
};
37+
38+
39+
static int
40+
_testcext_exec(
41+
#ifdef __STDC_VERSION__
42+
PyObject *module
43+
#else
44+
PyObject *Py_UNUSED(module)
45+
#endif
46+
)
47+
{
48+
#ifdef __STDC_VERSION__
49+
if (PyModule_AddIntMacro(module, __STDC_VERSION__) < 0) {
50+
return -1;
51+
}
52+
#endif
53+
54+
// test Py_BUILD_ASSERT() and Py_BUILD_ASSERT_EXPR()
55+
Py_BUILD_ASSERT(sizeof(int) == sizeof(unsigned int));
56+
assert(Py_BUILD_ASSERT_EXPR(sizeof(int) == sizeof(unsigned int)) == 0);
57+
58+
return 0;
59+
}
60+
61+
static PyModuleDef_Slot _testcext_slots[] = {
62+
{Py_mod_exec, (void*)_testcext_exec},
63+
{0, NULL}
64+
};
65+
66+
67+
PyDoc_STRVAR(_testcext_doc, "C test extension.");
68+
69+
static struct PyModuleDef _testcext_module = {
70+
PyModuleDef_HEAD_INIT, // m_base
71+
STR(MODULE_NAME), // m_name
72+
_testcext_doc, // m_doc
73+
0, // m_size
74+
_testcext_methods, // m_methods
75+
_testcext_slots, // m_slots
76+
NULL, // m_traverse
77+
NULL, // m_clear
78+
NULL, // m_free
79+
};
80+
81+
82+
#define _FUNC_NAME(NAME) PyInit_ ## NAME
83+
#define FUNC_NAME(NAME) _FUNC_NAME(NAME)
84+
85+
PyMODINIT_FUNC
86+
FUNC_NAME(MODULE_NAME)(void)
87+
{
88+
return PyModuleDef_Init(&_testcext_module);
89+
}

Lib/test/test_cext/setup.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# gh-91321: Build a basic C test extension to check that the Python C API is
2+
# compatible with C and does not emit C compiler warnings.
3+
import os
4+
import platform
5+
import shlex
6+
import sys
7+
import sysconfig
8+
from test import support
9+
10+
from setuptools import setup, Extension
11+
12+
13+
SOURCE = 'extension.c'
14+
15+
if not support.MS_WINDOWS:
16+
# C compiler flags for GCC and clang
17+
CFLAGS = [
18+
# The purpose of test_cext extension is to check that building a C
19+
# extension using the Python C API does not emit C compiler warnings.
20+
'-Werror',
21+
22+
# gh-120593: Check the 'const' qualifier
23+
'-Wcast-qual',
24+
25+
# gh-116869: The Python C API must be compatible with building
26+
# with the -Werror=declaration-after-statement compiler flag.
27+
'-Werror=declaration-after-statement',
28+
]
29+
else:
30+
# MSVC compiler flags
31+
CFLAGS = [
32+
# Display warnings level 1 to 4
33+
'/W4',
34+
# Treat all compiler warnings as compiler errors
35+
'/WX',
36+
]
37+
38+
39+
def main():
40+
std = os.environ.get("CPYTHON_TEST_STD", "")
41+
module_name = os.environ["CPYTHON_TEST_EXT_NAME"]
42+
limited = bool(os.environ.get("CPYTHON_TEST_LIMITED", ""))
43+
44+
cflags = list(CFLAGS)
45+
cflags.append(f'-DMODULE_NAME={module_name}')
46+
47+
# Add -std=STD or /std:STD (MSVC) compiler flag
48+
if std:
49+
if support.MS_WINDOWS:
50+
cflags.append(f'/std:{std}')
51+
else:
52+
cflags.append(f'-std={std}')
53+
54+
# Remove existing -std or /std options from CC command line.
55+
# Python adds -std=c11 option.
56+
cmd = (sysconfig.get_config_var('CC') or '')
57+
if cmd is not None:
58+
if support.MS_WINDOWS:
59+
std_prefix = '/std'
60+
else:
61+
std_prefix = '-std'
62+
cmd = shlex.split(cmd)
63+
cmd = [arg for arg in cmd if not arg.startswith(std_prefix)]
64+
cmd = shlex.join(cmd)
65+
# CC env var overrides sysconfig CC variable in setuptools
66+
os.environ['CC'] = cmd
67+
68+
# Define Py_LIMITED_API macro
69+
if limited:
70+
version = sys.hexversion
71+
cflags.append(f'-DPy_LIMITED_API={version:#x}')
72+
73+
# On Windows, add PCbuild\amd64\ to include and library directories
74+
include_dirs = []
75+
library_dirs = []
76+
if support.MS_WINDOWS:
77+
srcdir = sysconfig.get_config_var('srcdir')
78+
machine = platform.uname().machine
79+
pcbuild = os.path.join(srcdir, 'PCbuild', machine)
80+
if os.path.exists(pcbuild):
81+
# pyconfig.h is generated in PCbuild\amd64\
82+
include_dirs.append(pcbuild)
83+
# python313.lib is generated in PCbuild\amd64\
84+
library_dirs.append(pcbuild)
85+
print(f"Add PCbuild directory: {pcbuild}")
86+
87+
# Display information to help debugging
88+
for env_name in ('CC', 'CFLAGS'):
89+
if env_name in os.environ:
90+
print(f"{env_name} env var: {os.environ[env_name]!r}")
91+
else:
92+
print(f"{env_name} env var: <missing>")
93+
print(f"extra_compile_args: {cflags!r}")
94+
95+
ext = Extension(
96+
module_name,
97+
sources=[SOURCE],
98+
extra_compile_args=cflags,
99+
include_dirs=include_dirs,
100+
library_dirs=library_dirs)
101+
setup(name=f'internal_{module_name}',
102+
version='0.0',
103+
ext_modules=[ext])
104+
105+
106+
if __name__ == "__main__":
107+
main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Backport test_cext from the main branch. Patch by Victor Stinner.

0 commit comments

Comments
 (0)