From: Pradyun Gedam Date: Thu, 6 Apr 2023 17:27:53 +0000 (+0100) Subject: gh-95299: Rework test_cppext.py to not invoke setup.py directly X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=41c5a667b5de7070bbde5780f1c124f96863c91d;p=thirdparty%2FPython%2Fcpython.git gh-95299: Rework test_cppext.py to not invoke setup.py directly --- diff --git a/Lib/test/setup_testcppext.py b/Lib/test/cppextdata/setup.py similarity index 81% rename from Lib/test/setup_testcppext.py rename to Lib/test/cppextdata/setup.py index c6b68104d133..22fe750085fd 100644 --- a/Lib/test/setup_testcppext.py +++ b/Lib/test/cppextdata/setup.py @@ -1,5 +1,6 @@ # gh-91321: Build a basic C++ test extension to check that the Python C API is # compatible with C++ and does not emit C++ compiler warnings. +import os import sys from test import support @@ -25,14 +26,8 @@ else: def main(): cppflags = list(CPPFLAGS) - if '-std=c++03' in sys.argv: - sys.argv.remove('-std=c++03') - std = 'c++03' - name = '_testcpp03ext' - else: - # Python currently targets C++11 - std = 'c++11' - name = '_testcpp11ext' + std = os.environ["CPYTHON_TEST_CPP_STD"] + name = os.environ["CPYTHON_TEST_EXT_NAME"] cppflags = [*CPPFLAGS, f'-std={std}'] diff --git a/Lib/test/test_cppext.py b/Lib/test/test_cppext.py index 465894d24e7d..c059e1fca6c4 100644 --- a/Lib/test/test_cppext.py +++ b/Lib/test/test_cppext.py @@ -1,6 +1,10 @@ # gh-91321: Build a basic C++ test extension to check that the Python C API is # compatible with C++ and does not emit C++ compiler warnings. import os.path +try: + import ssl +except ImportError: + ssl = None import sys import unittest import subprocess @@ -11,8 +15,7 @@ from test.support import os_helper MS_WINDOWS = (sys.platform == 'win32') - -SETUP_TESTCPPEXT = support.findfile('setup_testcppext.py') +PKG_CPPEXTDATA = os.path.join(os.path.dirname(__file__), "cppextdata") @support.requires_subprocess() @@ -31,6 +34,8 @@ class TestCPPExt(unittest.TestCase): @unittest.skipIf( '-fsanitize' in (sysconfig.get_config_var('PY_CFLAGS') or ''), 'test does not work with analyzing builds') + # the test uses pip which needs a TLS connection to PyPI + @unittest.skipIf(ssl is None, 'No ssl module') # the test uses venv+pip: skip if it's not available @support.requires_venv_with_pip() def check_build(self, std_cpp03, extension_name): @@ -59,11 +64,15 @@ class TestCPPExt(unittest.TestCase): python = os.path.join(venv_dir, 'bin', python_exe) def run_cmd(operation, cmd): + env = os.environ.copy() + env['CPYTHON_TEST_CPP_STD'] = 'c++03' if std_cpp03 else 'c++11' + env['CPYTHON_TEST_EXT_NAME'] = extension_name if verbose: print('Run:', ' '.join(cmd)) - subprocess.run(cmd, check=True) + subprocess.run(cmd, check=True, env=env) else: proc = subprocess.run(cmd, + env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) @@ -72,16 +81,9 @@ class TestCPPExt(unittest.TestCase): self.fail( f"{operation} failed with exit code {proc.returncode}") - # Build the C++ extension - cmd = [python, '-X', 'dev', - SETUP_TESTCPPEXT, 'build_ext', '--verbose'] - if std_cpp03: - cmd.append('-std=c++03') - run_cmd('Build', cmd) - - # Install the C++ extension + # Build and install the C++ extension cmd = [python, '-X', 'dev', - SETUP_TESTCPPEXT, 'install'] + '-m', 'pip', 'install', PKG_CPPEXTDATA] run_cmd('Install', cmd) # Do a reference run. Until we test that running python