Skip to content

Commit 9e67740

Browse files
authored
gh-95299: Rework test_cppext.py to not invoke setup.py directly (#103316)
* gh-95299: Rework test_cppext.py to not invoke setup.py directly * Add tests/cppextdata data to `TESTSUBDIRS` * Revert "Add tests/cppextdata data to `TESTSUBDIRS`" This reverts commit 635492e. * Revert "gh-95299: Rework test_cppext.py to not invoke setup.py directly" This reverts commit 41c5a66. * Build and install the extension in a temporary directory instead * Pull in wheels for setuptools and wheel for testing extension builds
1 parent fb38c1b commit 9e67740

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

Lib/test/setup_testcppext.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# gh-91321: Build a basic C++ test extension to check that the Python C API is
22
# compatible with C++ and does not emit C++ compiler warnings.
3+
import os
34
import sys
45
from test import support
56

@@ -25,14 +26,8 @@
2526

2627
def main():
2728
cppflags = list(CPPFLAGS)
28-
if '-std=c++03' in sys.argv:
29-
sys.argv.remove('-std=c++03')
30-
std = 'c++03'
31-
name = '_testcpp03ext'
32-
else:
33-
# Python currently targets C++11
34-
std = 'c++11'
35-
name = '_testcpp11ext'
29+
std = os.environ["CPYTHON_TEST_CPP_STD"]
30+
name = os.environ["CPYTHON_TEST_EXT_NAME"]
3631

3732
cppflags = [*CPPFLAGS, f'-std={std}']
3833

1.04 MB
Binary file not shown.

Lib/test/test_cppext.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# gh-91321: Build a basic C++ test extension to check that the Python C API is
22
# compatible with C++ and does not emit C++ compiler warnings.
33
import os.path
4+
import shutil
45
import sys
56
import unittest
67
import subprocess
@@ -39,6 +40,10 @@ def check_build(self, std_cpp03, extension_name):
3940
self._check_build(std_cpp03, extension_name)
4041

4142
def _check_build(self, std_cpp03, extension_name):
43+
pkg_dir = 'pkg'
44+
os.mkdir(pkg_dir)
45+
shutil.copy(SETUP_TESTCPPEXT, os.path.join(pkg_dir, "setup.py"))
46+
4247
venv_dir = 'env'
4348
verbose = support.verbose
4449

@@ -59,11 +64,15 @@ def _check_build(self, std_cpp03, extension_name):
5964
python = os.path.join(venv_dir, 'bin', python_exe)
6065

6166
def run_cmd(operation, cmd):
67+
env = os.environ.copy()
68+
env['CPYTHON_TEST_CPP_STD'] = 'c++03' if std_cpp03 else 'c++11'
69+
env['CPYTHON_TEST_EXT_NAME'] = extension_name
6270
if verbose:
6371
print('Run:', ' '.join(cmd))
64-
subprocess.run(cmd, check=True)
72+
subprocess.run(cmd, check=True, env=env)
6573
else:
6674
proc = subprocess.run(cmd,
75+
env=env,
6776
stdout=subprocess.PIPE,
6877
stderr=subprocess.STDOUT,
6978
text=True)
@@ -72,16 +81,16 @@ def run_cmd(operation, cmd):
7281
self.fail(
7382
f"{operation} failed with exit code {proc.returncode}")
7483

75-
# Build the C++ extension
7684
cmd = [python, '-X', 'dev',
77-
SETUP_TESTCPPEXT, 'build_ext', '--verbose']
78-
if std_cpp03:
79-
cmd.append('-std=c++03')
80-
run_cmd('Build', cmd)
85+
'-m', 'pip', 'install',
86+
support.findfile('setuptools-67.6.1-py3-none-any.whl'),
87+
support.findfile('wheel-0.40.0-py3-none-any.whl')]
88+
run_cmd('Install build dependencies', cmd)
8189

82-
# Install the C++ extension
90+
# Build and install the C++ extension
8391
cmd = [python, '-X', 'dev',
84-
SETUP_TESTCPPEXT, 'install']
92+
'-m', 'pip', 'install', '--no-build-isolation',
93+
os.path.abspath(pkg_dir)]
8594
run_cmd('Install', cmd)
8695

8796
# Do a reference run. Until we test that running python
63 KB
Binary file not shown.

0 commit comments

Comments
 (0)