Debian Codegraph @

Lib/test/test_cppext/__init__.py

raw
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.path
4 import shlex
5 import shutil
6 import subprocess
7 import unittest
8 from test import support
9
10
11 SOURCE = os.path.join(os.path.dirname(__file__), 'extension.cpp')
12 SETUP = os.path.join(os.path.dirname(__file__), 'setup.py')
13
14
15 # With MSVC on a debug build, the linker fails with: cannot open file
16 # 'python311.lib', it should look 'python311_d.lib'.
17 @unittest.skipIf(support.MS_WINDOWS and support.Py_DEBUG,
18 'test fails on Windows debug build')
19 # Building and running an extension in clang sanitizing mode is not
20 # straightforward
21 @support.skip_if_sanitizer('test does not work with analyzing builds',
22 address=True, memory=True, ub=True, thread=True)
23 # the test uses venv+pip: skip if it's not available
24 @support.requires_venv_with_pip()
25 @support.requires_subprocess()
26 @support.requires_resource('cpu')
27 class TestCPPExt(unittest.TestCase):
28 def test_build(self):
29 self.check_build('_testcppext')
30
31 def test_build_cpp03(self):
32 self.check_build('_testcpp03ext', std='c++03')
33
34 @unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support /std:c++11")
35 def test_build_cpp11(self):
36 self.check_build('_testcpp11ext', std='c++11')
37
38 # Only test C++14 on MSVC.
39 # On s390x RHEL7, GCC 4.8.5 doesn't support C++14.
40 @unittest.skipIf(not support.MS_WINDOWS, "need Windows")
41 def test_build_cpp14(self):
42 self.check_build('_testcpp14ext', std='c++14')
43
44 @support.requires_gil_enabled('incompatible with Free Threading')
45 def test_build_limited(self):
46 self.check_build('_testcppext_limited', limited=True)
47
48 def check_build(self, extension_name, std=None, limited=False):
49 venv_dir = 'env'
50 with support.setup_venv_with_pip_setuptools(venv_dir) as python_exe:
51 self._check_build(extension_name, python_exe,
52 std=std, limited=limited)
53
54 def _check_build(self, extension_name, python_exe, std, limited):
55 pkg_dir = 'pkg'
56 os.mkdir(pkg_dir)
57 shutil.copy(SETUP, os.path.join(pkg_dir, os.path.basename(SETUP)))
58 shutil.copy(SOURCE, os.path.join(pkg_dir, os.path.basename(SOURCE)))
59
60 def run_cmd(operation, cmd):
61 env = os.environ.copy()
62 if std:
63 env['CPYTHON_TEST_CPP_STD'] = std
64 if limited:
65 env['CPYTHON_TEST_LIMITED'] = '1'
66 env['CPYTHON_TEST_EXT_NAME'] = extension_name
67 if support.verbose:
68 print('Run:', ' '.join(map(shlex.quote, cmd)))
69 subprocess.run(cmd, check=True, env=env)
70 else:
71 proc = subprocess.run(cmd,
72 env=env,
73 stdout=subprocess.PIPE,
74 stderr=subprocess.STDOUT,
75 text=True)
76 if proc.returncode:
77 print('Run:', ' '.join(map(shlex.quote, cmd)))
78 print(proc.stdout, end='')
79 self.fail(
80 f"{operation} failed with exit code {proc.returncode}")
81
82 # Build and install the C++ extension
83 cmd = [python_exe, '-X', 'dev',
84 '-m', 'pip', 'install', '--no-build-isolation',
85 os.path.abspath(pkg_dir)]
86 if support.verbose:
87 cmd.append('-v')
88 run_cmd('Install', cmd)
89
90 # Do a reference run. Until we test that running python
91 # doesn't leak references (gh-94755), run it so one can manually check
92 # -X showrefcount results against this baseline.
93 cmd = [python_exe,
94 '-X', 'dev',
95 '-X', 'showrefcount',
96 '-c', 'pass']
97 run_cmd('Reference run', cmd)
98
99 # Import the C++ extension
100 cmd = [python_exe,
101 '-X', 'dev',
102 '-X', 'showrefcount',
103 '-c', f"import {extension_name}"]
104 run_cmd('Import', cmd)
105
106
107 if __name__ == "__main__":
108 unittest.main()