Skip to content

Commit 6aef7f9

Browse files
committed
Use environment variables to pass data into test setup script
This avoids meddling with `sys.argv` and removes reliance on careful control over the arguments passed to the script.
1 parent ef633e5 commit 6aef7f9

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

Lib/test/setup_testcppext.py

Lines changed: 3 additions & 8 deletions
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

Lib/test/test_cppext.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,18 @@ def _check_build(self, std_cpp03, extension_name):
5959
python = os.path.join(venv_dir, 'bin', python_exe)
6060

6161
def run_cmd(operation, cmd):
62+
env = os.environ.copy()
63+
env['CPYTHON_TEST_CPP_STD'] = 'c++03' if std_cpp03 else 'c++11'
64+
env['CPYTHON_TEST_EXT_NAME'] = extension_name
6265
if verbose:
6366
print('Run:', ' '.join(cmd))
64-
subprocess.run(cmd, check=True)
67+
subprocess.run(cmd, check=True, env=env)
6568
else:
6669
proc = subprocess.run(cmd,
6770
stdout=subprocess.PIPE,
6871
stderr=subprocess.STDOUT,
69-
text=True)
72+
text=True,
73+
env=env)
7074
if proc.returncode:
7175
print(proc.stdout, end='')
7276
self.fail(
@@ -75,8 +79,6 @@ def run_cmd(operation, cmd):
7579
# Build the C++ extension
7680
cmd = [python, '-X', 'dev',
7781
SETUP_TESTCPPEXT, 'build_ext', '--verbose']
78-
if std_cpp03:
79-
cmd.append('-std=c++03')
8082
run_cmd('Build', cmd)
8183

8284
# Install the C++ extension

0 commit comments

Comments
 (0)