[Python-checkins] gh-95299: Rework test_cppext.py to not invoke setup.py directly (#103316)

pradyunsg webhook-mailer at python.org
Thu Apr 13 00:17:43 EDT 2023


https://github.com/python/cpython/commit/9e677406ee6666b32d869ce68c826519ff877445
commit: 9e677406ee6666b32d869ce68c826519ff877445
branch: main
author: Pradyun Gedam <pradyunsg at gmail.com>
committer: pradyunsg <pradyunsg at gmail.com>
date: 2023-04-12T23:17:36-05:00
summary:

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 635492e53954fb0fc2a2875c8961bde99266c48d.

* Revert "gh-95299: Rework test_cppext.py to not invoke setup.py directly"

This reverts commit 41c5a667b5de7070bbde5780f1c124f96863c91d.

* Build and install the extension in a temporary directory instead

* Pull in wheels for setuptools and wheel for testing extension builds

files:
A Lib/test/setuptools-67.6.1-py3-none-any.whl
A Lib/test/wheel-0.40.0-py3-none-any.whl
M Lib/test/setup_testcppext.py
M Lib/test/test_cppext.py

diff --git a/Lib/test/setup_testcppext.py b/Lib/test/setup_testcppext.py
index c6b68104d133..22fe750085fd 100644
--- a/Lib/test/setup_testcppext.py
+++ b/Lib/test/setup_testcppext.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 @@
 
 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/setuptools-67.6.1-py3-none-any.whl b/Lib/test/setuptools-67.6.1-py3-none-any.whl
new file mode 100644
index 000000000000..4b7ffd2e49e1
Binary files /dev/null and b/Lib/test/setuptools-67.6.1-py3-none-any.whl differ
diff --git a/Lib/test/test_cppext.py b/Lib/test/test_cppext.py
index 465894d24e7d..4fb62d87e860 100644
--- a/Lib/test/test_cppext.py
+++ b/Lib/test/test_cppext.py
@@ -1,6 +1,7 @@
 # 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
+import shutil
 import sys
 import unittest
 import subprocess
@@ -39,6 +40,10 @@ def check_build(self, std_cpp03, extension_name):
             self._check_build(std_cpp03, extension_name)
 
     def _check_build(self, std_cpp03, extension_name):
+        pkg_dir = 'pkg'
+        os.mkdir(pkg_dir)
+        shutil.copy(SETUP_TESTCPPEXT, os.path.join(pkg_dir, "setup.py"))
+
         venv_dir = 'env'
         verbose = support.verbose
 
@@ -59,11 +64,15 @@ def _check_build(self, std_cpp03, extension_name):
             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,16 @@ def run_cmd(operation, cmd):
                     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)
+               '-m', 'pip', 'install',
+               support.findfile('setuptools-67.6.1-py3-none-any.whl'),
+               support.findfile('wheel-0.40.0-py3-none-any.whl')]
+        run_cmd('Install build dependencies', cmd)
 
-        # Install the C++ extension
+        # Build and install the C++ extension
         cmd = [python, '-X', 'dev',
-               SETUP_TESTCPPEXT, 'install']
+               '-m', 'pip', 'install', '--no-build-isolation',
+               os.path.abspath(pkg_dir)]
         run_cmd('Install', cmd)
 
         # Do a reference run. Until we test that running python
diff --git a/Lib/test/wheel-0.40.0-py3-none-any.whl b/Lib/test/wheel-0.40.0-py3-none-any.whl
new file mode 100644
index 000000000000..410132385bba
Binary files /dev/null and b/Lib/test/wheel-0.40.0-py3-none-any.whl differ



More information about the Python-checkins mailing list