| 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 |
@support.requires_gil_enabled('incompatible with Free Threading') |
| 44 |
def test_build_limited(self): |
| 45 |
self.check_build('_test_limited_cext', limited=True) |
| 46 |
|
| 47 |
@support.requires_gil_enabled('broken for now with Free Threading') |
| 48 |
def test_build_limited_c11(self): |
| 49 |
self.check_build('_test_limited_c11_cext', limited=True, std='c11') |
| 50 |
|
| 51 |
def check_build(self, extension_name, std=None, limited=False): |
| 52 |
venv_dir = 'env' |
| 53 |
with support.setup_venv_with_pip_setuptools(venv_dir) as python_exe: |
| 54 |
self._check_build(extension_name, python_exe, |
| 55 |
std=std, limited=limited) |
| 56 |
|
| 57 |
def _check_build(self, extension_name, python_exe, std, limited): |
| 58 |
pkg_dir = 'pkg' |
| 59 |
os.mkdir(pkg_dir) |
| 60 |
shutil.copy(SETUP, os.path.join(pkg_dir, os.path.basename(SETUP))) |
| 61 |
shutil.copy(SOURCE, os.path.join(pkg_dir, os.path.basename(SOURCE))) |
| 62 |
|
| 63 |
def run_cmd(operation, cmd): |
| 64 |
env = os.environ.copy() |
| 65 |
if std: |
| 66 |
env['CPYTHON_TEST_STD'] = std |
| 67 |
if limited: |
| 68 |
env['CPYTHON_TEST_LIMITED'] = '1' |
| 69 |
env['CPYTHON_TEST_EXT_NAME'] = extension_name |
| 70 |
if support.verbose: |
| 71 |
print('Run:', ' '.join(map(shlex.quote, cmd))) |
| 72 |
subprocess.run(cmd, check=True, env=env) |
| 73 |
else: |
| 74 |
proc = subprocess.run(cmd, |
| 75 |
env=env, |
| 76 |
stdout=subprocess.PIPE, |
| 77 |
stderr=subprocess.STDOUT, |
| 78 |
text=True) |
| 79 |
if proc.returncode: |
| 80 |
print('Run:', ' '.join(map(shlex.quote, cmd))) |
| 81 |
print(proc.stdout, end='') |
| 82 |
self.fail( |
| 83 |
f"{operation} failed with exit code {proc.returncode}") |
| 84 |
|
| 85 |
# Build and install the C extension |
| 86 |
cmd = [python_exe, '-X', 'dev', |
| 87 |
'-m', 'pip', 'install', '--no-build-isolation', |
| 88 |
os.path.abspath(pkg_dir)] |
| 89 |
if support.verbose: |
| 90 |
cmd.append('-v') |
| 91 |
run_cmd('Install', cmd) |
| 92 |
|
| 93 |
# Do a reference run. Until we test that running python |
| 94 |
# doesn't leak references (gh-94755), run it so one can manually check |
| 95 |
# -X showrefcount results against this baseline. |
| 96 |
cmd = [python_exe, |
| 97 |
'-X', 'dev', |
| 98 |
'-X', 'showrefcount', |
| 99 |
'-c', 'pass'] |
| 100 |
run_cmd('Reference run', cmd) |
| 101 |
|
| 102 |
# Import the C extension |
| 103 |
cmd = [python_exe, |
| 104 |
'-X', 'dev', |
| 105 |
'-X', 'showrefcount', |
| 106 |
'-c', f"import {extension_name}"] |
| 107 |
run_cmd('Import', cmd) |
| 108 |
|
| 109 |
|
| 110 |
if __name__ == "__main__": |
| 111 |
unittest.main() |