| 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 platform |
| 5 |
import shlex |
| 6 |
import shutil |
| 7 |
import subprocess |
| 8 |
import sys |
| 9 |
import unittest |
| 10 |
from test import support |
| 11 |
|
| 12 |
|
| 13 |
SOURCE = os.path.join(os.path.dirname(__file__), 'extension.cpp') |
| 14 |
SETUP = os.path.join(os.path.dirname(__file__), 'setup.py') |
| 15 |
|
| 16 |
|
| 17 |
# With MSVC on a debug build, the linker fails with: cannot open file |
| 18 |
# 'python311.lib', it should look 'python311_d.lib'. |
| 19 |
@unittest.skipIf(support.MS_WINDOWS and support.Py_DEBUG, |
| 20 |
'test fails on Windows debug build') |
| 21 |
# Building and running an extension in clang sanitizing mode is not |
| 22 |
# straightforward |
| 23 |
@support.skip_if_sanitizer('test does not work with analyzing builds', |
| 24 |
address=True, memory=True, ub=True, thread=True) |
| 25 |
# the test uses venv+pip: skip if it's not available |
| 26 |
@support.requires_venv_with_pip() |
| 27 |
@support.requires_subprocess() |
| 28 |
@support.requires_resource('cpu') |
| 29 |
class BaseTests: |
| 30 |
TEST_INTERNAL_C_API = False |
| 31 |
|
| 32 |
def check_build(self, extension_name, std=None, limited=False, |
| 33 |
extra_cflags=None): |
| 34 |
venv_dir = 'env' |
| 35 |
with support.setup_venv_with_pip_setuptools(venv_dir) as python_exe: |
| 36 |
self._check_build(extension_name, python_exe, |
| 37 |
std=std, limited=limited, |
| 38 |
extra_cflags=extra_cflags) |
| 39 |
|
| 40 |
def _check_build(self, extension_name, python_exe, std, limited, |
| 41 |
extra_cflags=None): |
| 42 |
pkg_dir = 'pkg' |
| 43 |
os.mkdir(pkg_dir) |
| 44 |
shutil.copy(SETUP, os.path.join(pkg_dir, os.path.basename(SETUP))) |
| 45 |
shutil.copy(SOURCE, os.path.join(pkg_dir, os.path.basename(SOURCE))) |
| 46 |
|
| 47 |
def run_cmd(operation, cmd): |
| 48 |
env = os.environ.copy() |
| 49 |
if std: |
| 50 |
env['CPYTHON_TEST_CPP_STD'] = std |
| 51 |
if limited: |
| 52 |
env['CPYTHON_TEST_LIMITED'] = '1' |
| 53 |
env['CPYTHON_TEST_EXT_NAME'] = extension_name |
| 54 |
env['TEST_INTERNAL_C_API'] = str(int(self.TEST_INTERNAL_C_API)) |
| 55 |
if extra_cflags: |
| 56 |
env['CPYTHON_TEST_EXTRA_CFLAGS'] = extra_cflags |
| 57 |
if support.verbose: |
| 58 |
print('Run:', ' '.join(map(shlex.quote, cmd))) |
| 59 |
subprocess.run(cmd, check=True, env=env) |
| 60 |
else: |
| 61 |
proc = subprocess.run(cmd, |
| 62 |
env=env, |
| 63 |
stdout=subprocess.PIPE, |
| 64 |
stderr=subprocess.STDOUT, |
| 65 |
text=True) |
| 66 |
if proc.returncode: |
| 67 |
print('Run:', ' '.join(map(shlex.quote, cmd))) |
| 68 |
print(proc.stdout, end='') |
| 69 |
self.fail( |
| 70 |
f"{operation} failed with exit code {proc.returncode}") |
| 71 |
|
| 72 |
# Build and install the C++ extension |
| 73 |
cmd = [python_exe, '-X', 'dev', |
| 74 |
'-m', 'pip', 'install', '--no-build-isolation', |
| 75 |
os.path.abspath(pkg_dir)] |
| 76 |
if support.verbose: |
| 77 |
cmd.append('-v') |
| 78 |
run_cmd('Install', cmd) |
| 79 |
|
| 80 |
# Do a reference run. Until we test that running python |
| 81 |
# doesn't leak references (gh-94755), run it so one can manually check |
| 82 |
# -X showrefcount results against this baseline. |
| 83 |
cmd = [python_exe, |
| 84 |
'-X', 'dev', |
| 85 |
'-X', 'showrefcount', |
| 86 |
'-c', 'pass'] |
| 87 |
run_cmd('Reference run', cmd) |
| 88 |
|
| 89 |
# Import the C++ extension |
| 90 |
cmd = [python_exe, |
| 91 |
'-X', 'dev', |
| 92 |
'-X', 'showrefcount', |
| 93 |
'-c', f"import {extension_name}"] |
| 94 |
run_cmd('Import', cmd) |
| 95 |
|
| 96 |
|
| 97 |
class TestPublicCAPI(BaseTests, unittest.TestCase): |
| 98 |
def test_build(self): |
| 99 |
self.check_build('_testcppext') |
| 100 |
|
| 101 |
@support.requires_gil_enabled('incompatible with Free Threading') |
| 102 |
def test_build_limited_cpp03(self): |
| 103 |
self.check_build('_test_limited_cpp03ext', std='c++03', limited=True) |
| 104 |
|
| 105 |
@support.requires_gil_enabled('incompatible with Free Threading') |
| 106 |
def test_build_limited(self): |
| 107 |
self.check_build('_testcppext_limited', limited=True) |
| 108 |
|
| 109 |
def test_build_cpp03(self): |
| 110 |
# In public docs, we say C API is compatible with C++11. However, |
| 111 |
# in practice we do maintain C++03 compatibility in public headers. |
| 112 |
# Please ask the C API WG before adding a new C++11-only feature. |
| 113 |
self.check_build('_testcpp03ext', std='c++03') |
| 114 |
|
| 115 |
@unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support /std:c++11") |
| 116 |
def test_build_cpp11(self): |
| 117 |
self.check_build('_testcpp11ext', std='c++11') |
| 118 |
|
| 119 |
# Only test C++14 on MSVC. |
| 120 |
# On s390x RHEL7, GCC 4.8.5 doesn't support C++14. |
| 121 |
@unittest.skipIf(not support.MS_WINDOWS, "need Windows") |
| 122 |
def test_build_cpp14(self): |
| 123 |
self.check_build('_testcpp14ext', std='c++14') |
| 124 |
|
| 125 |
# Test that headers compile with Intel asm syntax, which may conflict |
| 126 |
# with inline assembly in free-threading headers that use AT&T syntax. |
| 127 |
@unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support -masm=intel") |
| 128 |
@unittest.skipUnless(platform.machine() in ('x86_64', 'i686', 'AMD64'), |
| 129 |
"x86-specific flag") |
| 130 |
def test_build_intel_asm(self): |
| 131 |
self.check_build('_testcppext_asm', extra_cflags='-masm=intel') |
| 132 |
|
| 133 |
|
| 134 |
class TestInteralCAPI(BaseTests, unittest.TestCase): |
| 135 |
TEST_INTERNAL_C_API = True |
| 136 |
|
| 137 |
def test_build(self): |
| 138 |
kwargs = {} |
| 139 |
if sys.platform == 'darwin': |
| 140 |
# Old Apple clang++ default C++ std is gnu++98 |
| 141 |
kwargs['std'] = 'c++11' |
| 142 |
self.check_build('_testcppext_internal', **kwargs) |
| 143 |
|
| 144 |
|
| 145 |
if __name__ == "__main__": |
| 146 |
unittest.main() |