[Python-checkins] [3.12] gh-105084: Tests: Use setuptools+wheel from sysconfig.get_config_var('WHEEL_PKG_DIR') if set (#105056) (#105424)

lysnikolaou webhook-mailer at python.org
Tue Jun 13 06:54:19 EDT 2023


https://github.com/python/cpython/commit/04b91680373149077f8c466e6694b97966e1f2c6
commit: 04b91680373149077f8c466e6694b97966e1f2c6
branch: 3.12
author: Miro Hrončok <miro at hroncok.cz>
committer: lysnikolaou <lisandrosnik at gmail.com>
date: 2023-06-13T12:54:11+02:00
summary:

[3.12] gh-105084: Tests: Use setuptools+wheel from sysconfig.get_config_var('WHEEL_PKG_DIR') if set (#105056) (#105424)

Includes part of the changes from afa759fb800be416f69e3e9c9b3efe68006316f5,
to make this apply.

Co-Authored-By: Lysandros Nikolaou <lisandrosnik at gmail.com>

(cherry picked from commit bd98b65e974b7a1e086a51e7b55131582f7a0491)

files:
A Misc/NEWS.d/next/Tests/2023-05-29-14-49-46.gh-issue-105084.lvVvoj.rst
M Lib/test/support/__init__.py
M Lib/test/test_cppext.py

diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index d555c53fee50a..4d01bb5a848f7 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -2270,6 +2270,62 @@ def requires_venv_with_pip():
     return unittest.skipUnless(ctypes, 'venv: pip requires ctypes')
 
 
+ at functools.cache
+def _findwheel(pkgname):
+    """Try to find a wheel with the package specified as pkgname.
+
+    If set, the wheels are searched for in WHEEL_PKG_DIR (see ensurepip).
+    Otherwise, they are searched for in the test directory.
+    """
+    wheel_dir = sysconfig.get_config_var('WHEEL_PKG_DIR') or TEST_HOME_DIR
+    filenames = os.listdir(wheel_dir)
+    filenames = sorted(filenames, reverse=True)  # approximate "newest" first
+    for filename in filenames:
+        # filename is like 'setuptools-67.6.1-py3-none-any.whl'
+        if not filename.endswith(".whl"):
+            continue
+        prefix = pkgname + '-'
+        if filename.startswith(prefix):
+            return os.path.join(wheel_dir, filename)
+    raise FileNotFoundError(f"No wheel for {pkgname} found in {wheel_dir}")
+
+
+# Context manager that creates a virtual environment, install setuptools and wheel in it
+# and returns the path to the venv directory and the path to the python executable
+ at contextlib.contextmanager
+def setup_venv_with_pip_setuptools_wheel(venv_dir):
+    import subprocess
+    from .os_helper import temp_cwd
+
+    with temp_cwd() as temp_dir:
+        # Create virtual environment to get setuptools
+        cmd = [sys.executable, '-X', 'dev', '-m', 'venv', venv_dir]
+        if verbose:
+            print()
+            print('Run:', ' '.join(cmd))
+        subprocess.run(cmd, check=True)
+
+        venv = os.path.join(temp_dir, venv_dir)
+
+        # Get the Python executable of the venv
+        python_exe = os.path.basename(sys.executable)
+        if sys.platform == 'win32':
+            python = os.path.join(venv, 'Scripts', python_exe)
+        else:
+            python = os.path.join(venv, 'bin', python_exe)
+
+        cmd = [python, '-X', 'dev',
+               '-m', 'pip', 'install',
+               _findwheel('setuptools'),
+               _findwheel('wheel')]
+        if verbose:
+            print()
+            print('Run:', ' '.join(cmd))
+        subprocess.run(cmd, check=True)
+
+        yield python
+
+
 # True if Python is built with the Py_DEBUG macro defined: if
 # Python is built in debug mode (./configure --with-pydebug).
 Py_DEBUG = hasattr(sys, 'gettotalrefcount')
diff --git a/Lib/test/test_cppext.py b/Lib/test/test_cppext.py
index 4fb62d87e860f..e2fedc9735079 100644
--- a/Lib/test/test_cppext.py
+++ b/Lib/test/test_cppext.py
@@ -35,39 +35,20 @@ def test_build_cpp03(self):
     # the test uses venv+pip: skip if it's not available
     @support.requires_venv_with_pip()
     def check_build(self, std_cpp03, extension_name):
-        # Build in a temporary directory
-        with os_helper.temp_cwd():
-            self._check_build(std_cpp03, extension_name)
+        venv_dir = 'env'
+        with support.setup_venv_with_pip_setuptools_wheel(venv_dir) as python_exe:
+            self._check_build(std_cpp03, extension_name, python_exe)
 
-    def _check_build(self, std_cpp03, extension_name):
+    def _check_build(self, std_cpp03, extension_name, python_exe):
         pkg_dir = 'pkg'
         os.mkdir(pkg_dir)
         shutil.copy(SETUP_TESTCPPEXT, os.path.join(pkg_dir, "setup.py"))
 
-        venv_dir = 'env'
-        verbose = support.verbose
-
-        # Create virtual environment to get setuptools
-        cmd = [sys.executable, '-X', 'dev', '-m', 'venv', venv_dir]
-        if verbose:
-            print()
-            print('Run:', ' '.join(cmd))
-        subprocess.run(cmd, check=True)
-
-        # Get the Python executable of the venv
-        python_exe = 'python'
-        if sys.executable.endswith('.exe'):
-            python_exe += '.exe'
-        if MS_WINDOWS:
-            python = os.path.join(venv_dir, 'Scripts', python_exe)
-        else:
-            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:
+            if support.verbose:
                 print('Run:', ' '.join(cmd))
                 subprocess.run(cmd, check=True, env=env)
             else:
@@ -81,14 +62,8 @@ def run_cmd(operation, cmd):
                     self.fail(
                         f"{operation} failed with exit code {proc.returncode}")
 
-        cmd = [python, '-X', 'dev',
-               '-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)
-
         # Build and install the C++ extension
-        cmd = [python, '-X', 'dev',
+        cmd = [python_exe, '-X', 'dev',
                '-m', 'pip', 'install', '--no-build-isolation',
                os.path.abspath(pkg_dir)]
         run_cmd('Install', cmd)
@@ -96,14 +71,14 @@ def run_cmd(operation, cmd):
         # Do a reference run. Until we test that running python
         # doesn't leak references (gh-94755), run it so one can manually check
         # -X showrefcount results against this baseline.
-        cmd = [python,
+        cmd = [python_exe,
                '-X', 'dev',
                '-X', 'showrefcount',
                '-c', 'pass']
         run_cmd('Reference run', cmd)
 
         # Import the C++ extension
-        cmd = [python,
+        cmd = [python_exe,
                '-X', 'dev',
                '-X', 'showrefcount',
                '-c', f"import {extension_name}"]
diff --git a/Misc/NEWS.d/next/Tests/2023-05-29-14-49-46.gh-issue-105084.lvVvoj.rst b/Misc/NEWS.d/next/Tests/2023-05-29-14-49-46.gh-issue-105084.lvVvoj.rst
new file mode 100644
index 0000000000000..5f80d50714734
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2023-05-29-14-49-46.gh-issue-105084.lvVvoj.rst
@@ -0,0 +1,3 @@
+When the Python build is configured ``--with-wheel-pkg-dir``, tests
+requiring the ``setuptools`` and ``wheel`` wheels will search for the wheels
+in ``WHEEL_PKG_DIR``.



More information about the Python-checkins mailing list