Debian Codegraph @

Lib/test/test_cext/__init__.py

raw
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 SOURCES = [
16 os.path.join(os.path.dirname(__file__), 'extension.c'),
17 ]
18 SETUP = os.path.join(os.path.dirname(__file__), 'setup.py')
19
20
21 # With MSVC on a debug build, the linker fails with: cannot open file
22 # 'python311.lib', it should look 'python311_d.lib'.
23 @unittest.skipIf(support.MS_WINDOWS and support.Py_DEBUG,
24 'test fails on Windows debug build')
25 # Building and running an extension in clang sanitizing mode is not
26 # straightforward
27 @support.skip_if_sanitizer('test does not work with analyzing builds',
28 address=True, memory=True, ub=True, thread=True)
29 # the test uses venv+pip: skip if it's not available
30 @support.requires_venv_with_pip()
31 @support.requires_subprocess()
32 @support.requires_resource('cpu')
33 class BaseTests:
34 TEST_INTERNAL_C_API = False
35
36 # Default build with no options
37 def test_build(self):
38 self.check_build('_test_cext')
39
40 def check_build(self, extension_name, std=None, limited=False):
41 venv_dir = 'env'
42 with support.setup_venv_with_pip_setuptools(venv_dir) as python_exe:
43 self._check_build(extension_name, python_exe,
44 std=std, limited=limited)
45
46 def _check_build(self, extension_name, python_exe, std, limited):
47 pkg_dir = 'pkg'
48 os.mkdir(pkg_dir)
49 shutil.copy(SETUP, os.path.join(pkg_dir, os.path.basename(SETUP)))
50 for source in SOURCES:
51 dest = os.path.join(pkg_dir, os.path.basename(source))
52 shutil.copy(source, dest)
53
54 def run_cmd(operation, cmd):
55 env = os.environ.copy()
56 if std:
57 env['CPYTHON_TEST_STD'] = std
58 if limited:
59 env['CPYTHON_TEST_LIMITED'] = '1'
60 env['CPYTHON_TEST_EXT_NAME'] = extension_name
61 env['TEST_INTERNAL_C_API'] = str(int(self.TEST_INTERNAL_C_API))
62 if support.verbose:
63 print('Run:', ' '.join(map(shlex.quote, cmd)))
64 subprocess.run(cmd, check=True, env=env)
65 else:
66 proc = subprocess.run(cmd,
67 env=env,
68 stdout=subprocess.PIPE,
69 stderr=subprocess.STDOUT,
70 text=True)
71 if proc.returncode:
72 print('Run:', ' '.join(map(shlex.quote, cmd)))
73 print(proc.stdout, end='')
74 self.fail(
75 f"{operation} failed with exit code {proc.returncode}")
76
77 # Build and install the C extension
78 cmd = [python_exe, '-X', 'dev',
79 '-m', 'pip', 'install', '--no-build-isolation',
80 os.path.abspath(pkg_dir)]
81 if support.verbose:
82 cmd.append('-v')
83 run_cmd('Install', cmd)
84
85 # Do a reference run. Until we test that running python
86 # doesn't leak references (gh-94755), run it so one can manually check
87 # -X showrefcount results against this baseline.
88 cmd = [python_exe,
89 '-X', 'dev',
90 '-X', 'showrefcount',
91 '-c', 'pass']
92 run_cmd('Reference run', cmd)
93
94 # Import the C extension
95 cmd = [python_exe,
96 '-X', 'dev',
97 '-X', 'showrefcount',
98 '-c', f"import {extension_name}"]
99 run_cmd('Import', cmd)
100
101
102 class TestPublicCAPI(BaseTests, unittest.TestCase):
103 @support.requires_gil_enabled('incompatible with Free Threading')
104 def test_build_limited(self):
105 self.check_build('_test_limited_cext', limited=True)
106
107 @support.requires_gil_enabled('broken for now with Free Threading')
108 def test_build_limited_c11(self):
109 self.check_build('_test_limited_c11_cext', limited=True, std='c11')
110
111 def test_build_c11(self):
112 self.check_build('_test_c11_cext', std='c11')
113
114 @unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support /std:c99")
115 def test_build_c99(self):
116 # In public docs, we say C API is compatible with C11. However,
117 # in practice we do maintain C99 compatibility in public headers.
118 # Please ask the C API WG before adding a new C11-only feature.
119 self.check_build('_test_c99_cext', std='c99')
120
121
122 class TestInteralCAPI(BaseTests, unittest.TestCase):
123 TEST_INTERNAL_C_API = True
124
125
126 if __name__ == "__main__":
127 unittest.main()