Debian Codegraph @

lib-python/3/test/test_cppext/__init__.py

raw symbols SCIP
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 sys
5 import unittest
6 import subprocess
7 import sysconfig
8 from test import support
9 from test.support import os_helper
10
11
12 SETUP = os.path.join(os.path.dirname(__file__), 'setup.py')
13
14
15 @support.requires_subprocess()
16 class TestCPPExt(unittest.TestCase):
17 @support.requires_resource('cpu')
18 def test_build_cpp11(self):
19 self.check_build(False, '_testcpp11ext')
20
21 @support.requires_resource('cpu')
22 def test_build_cpp03(self):
23 self.check_build(True, '_testcpp03ext')
24
25 # With MSVC, the linker fails with: cannot open file 'python311.lib'
26 # https://github.com/python/cpython/pull/32175#issuecomment-1111175897
27 @unittest.skipIf(support.MS_WINDOWS, 'test fails on Windows')
28 # Building and running an extension in clang sanitizing mode is not
29 # straightforward
30 @unittest.skipIf(
31 '-fsanitize' in (sysconfig.get_config_var('PY_CFLAGS') or ''),
32 'test does not work with analyzing builds')
33 # the test uses venv+pip: skip if it's not available
34 @support.requires_venv_with_pip()
35 def check_build(self, std_cpp03, extension_name):
36 # Build in a temporary directory
37 with os_helper.temp_cwd():
38 self._check_build(std_cpp03, extension_name)
39
40 def _check_build(self, std_cpp03, extension_name):
41 venv_dir = 'env'
42 verbose = support.verbose
43
44 # Create virtual environment to get setuptools
45 cmd = [sys.executable, '-X', 'dev', '-m', 'venv', venv_dir]
46 if verbose:
47 print()
48 print('Run:', ' '.join(cmd))
49 subprocess.run(cmd, check=True)
50
51 # Get the Python executable of the venv
52 python_exe = 'python'
53 if sys.executable.endswith('.exe'):
54 python_exe += '.exe'
55 if support.MS_WINDOWS:
56 python = os.path.join(venv_dir, 'Scripts', python_exe)
57 else:
58 python = os.path.join(venv_dir, 'bin', python_exe)
59
60 def run_cmd(operation, cmd):
61 if verbose:
62 print('Run:', ' '.join(cmd))
63 subprocess.run(cmd, check=True)
64 else:
65 proc = subprocess.run(cmd,
66 stdout=subprocess.PIPE,
67 stderr=subprocess.STDOUT,
68 text=True)
69 if proc.returncode:
70 print(proc.stdout, end='')
71 self.fail(
72 f"{operation} failed with exit code {proc.returncode}")
73
74 # Build the C++ extension
75 cmd = [python, '-X', 'dev',
76 SETUP, 'build_ext', '--verbose']
77 if std_cpp03:
78 cmd.append('-std=c++03')
79 run_cmd('Build', cmd)
80
81 # Install the C++ extension
82 cmd = [python, '-X', 'dev',
83 SETUP, 'install']
84 run_cmd('Install', cmd)
85
86 # Do a reference run. Until we test that running python
87 # doesn't leak references (gh-94755), run it so one can manually check
88 # -X showrefcount results against this baseline.
89 cmd = [python,
90 '-X', 'dev',
91 '-X', 'showrefcount',
92 '-c', 'pass']
93 run_cmd('Reference run', cmd)
94
95 # Import the C++ extension
96 cmd = [python,
97 '-X', 'dev',
98 '-X', 'showrefcount',
99 '-c', f"import {extension_name}"]
100 run_cmd('Import', cmd)
101
102
103 if __name__ == "__main__":
104 unittest.main()