Skip to content

Commit 04b9168

Browse files
authored
[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 afa759f, to make this apply. Co-Authored-By: Lysandros Nikolaou <lisandrosnik@gmail.com> (cherry picked from commit bd98b65)
1 parent 92929fd commit 04b9168

File tree

3 files changed

+67
-33
lines changed

3 files changed

+67
-33
lines changed

Lib/test/support/__init__.py

+56
Original file line numberDiff line numberDiff line change
@@ -2270,6 +2270,62 @@ def requires_venv_with_pip():
22702270
return unittest.skipUnless(ctypes, 'venv: pip requires ctypes')
22712271

22722272

2273+
@functools.cache
2274+
def _findwheel(pkgname):
2275+
"""Try to find a wheel with the package specified as pkgname.
2276+
2277+
If set, the wheels are searched for in WHEEL_PKG_DIR (see ensurepip).
2278+
Otherwise, they are searched for in the test directory.
2279+
"""
2280+
wheel_dir = sysconfig.get_config_var('WHEEL_PKG_DIR') or TEST_HOME_DIR
2281+
filenames = os.listdir(wheel_dir)
2282+
filenames = sorted(filenames, reverse=True) # approximate "newest" first
2283+
for filename in filenames:
2284+
# filename is like 'setuptools-67.6.1-py3-none-any.whl'
2285+
if not filename.endswith(".whl"):
2286+
continue
2287+
prefix = pkgname + '-'
2288+
if filename.startswith(prefix):
2289+
return os.path.join(wheel_dir, filename)
2290+
raise FileNotFoundError(f"No wheel for {pkgname} found in {wheel_dir}")
2291+
2292+
2293+
# Context manager that creates a virtual environment, install setuptools and wheel in it
2294+
# and returns the path to the venv directory and the path to the python executable
2295+
@contextlib.contextmanager
2296+
def setup_venv_with_pip_setuptools_wheel(venv_dir):
2297+
import subprocess
2298+
from .os_helper import temp_cwd
2299+
2300+
with temp_cwd() as temp_dir:
2301+
# Create virtual environment to get setuptools
2302+
cmd = [sys.executable, '-X', 'dev', '-m', 'venv', venv_dir]
2303+
if verbose:
2304+
print()
2305+
print('Run:', ' '.join(cmd))
2306+
subprocess.run(cmd, check=True)
2307+
2308+
venv = os.path.join(temp_dir, venv_dir)
2309+
2310+
# Get the Python executable of the venv
2311+
python_exe = os.path.basename(sys.executable)
2312+
if sys.platform == 'win32':
2313+
python = os.path.join(venv, 'Scripts', python_exe)
2314+
else:
2315+
python = os.path.join(venv, 'bin', python_exe)
2316+
2317+
cmd = [python, '-X', 'dev',
2318+
'-m', 'pip', 'install',
2319+
_findwheel('setuptools'),
2320+
_findwheel('wheel')]
2321+
if verbose:
2322+
print()
2323+
print('Run:', ' '.join(cmd))
2324+
subprocess.run(cmd, check=True)
2325+
2326+
yield python
2327+
2328+
22732329
# True if Python is built with the Py_DEBUG macro defined: if
22742330
# Python is built in debug mode (./configure --with-pydebug).
22752331
Py_DEBUG = hasattr(sys, 'gettotalrefcount')

Lib/test/test_cppext.py

+8-33
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,20 @@ def test_build_cpp03(self):
3535
# the test uses venv+pip: skip if it's not available
3636
@support.requires_venv_with_pip()
3737
def check_build(self, std_cpp03, extension_name):
38-
# Build in a temporary directory
39-
with os_helper.temp_cwd():
40-
self._check_build(std_cpp03, extension_name)
38+
venv_dir = 'env'
39+
with support.setup_venv_with_pip_setuptools_wheel(venv_dir) as python_exe:
40+
self._check_build(std_cpp03, extension_name, python_exe)
4141

42-
def _check_build(self, std_cpp03, extension_name):
42+
def _check_build(self, std_cpp03, extension_name, python_exe):
4343
pkg_dir = 'pkg'
4444
os.mkdir(pkg_dir)
4545
shutil.copy(SETUP_TESTCPPEXT, os.path.join(pkg_dir, "setup.py"))
4646

47-
venv_dir = 'env'
48-
verbose = support.verbose
49-
50-
# Create virtual environment to get setuptools
51-
cmd = [sys.executable, '-X', 'dev', '-m', 'venv', venv_dir]
52-
if verbose:
53-
print()
54-
print('Run:', ' '.join(cmd))
55-
subprocess.run(cmd, check=True)
56-
57-
# Get the Python executable of the venv
58-
python_exe = 'python'
59-
if sys.executable.endswith('.exe'):
60-
python_exe += '.exe'
61-
if MS_WINDOWS:
62-
python = os.path.join(venv_dir, 'Scripts', python_exe)
63-
else:
64-
python = os.path.join(venv_dir, 'bin', python_exe)
65-
6647
def run_cmd(operation, cmd):
6748
env = os.environ.copy()
6849
env['CPYTHON_TEST_CPP_STD'] = 'c++03' if std_cpp03 else 'c++11'
6950
env['CPYTHON_TEST_EXT_NAME'] = extension_name
70-
if verbose:
51+
if support.verbose:
7152
print('Run:', ' '.join(cmd))
7253
subprocess.run(cmd, check=True, env=env)
7354
else:
@@ -81,29 +62,23 @@ def run_cmd(operation, cmd):
8162
self.fail(
8263
f"{operation} failed with exit code {proc.returncode}")
8364

84-
cmd = [python, '-X', 'dev',
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)
89-
9065
# Build and install the C++ extension
91-
cmd = [python, '-X', 'dev',
66+
cmd = [python_exe, '-X', 'dev',
9267
'-m', 'pip', 'install', '--no-build-isolation',
9368
os.path.abspath(pkg_dir)]
9469
run_cmd('Install', cmd)
9570

9671
# Do a reference run. Until we test that running python
9772
# doesn't leak references (gh-94755), run it so one can manually check
9873
# -X showrefcount results against this baseline.
99-
cmd = [python,
74+
cmd = [python_exe,
10075
'-X', 'dev',
10176
'-X', 'showrefcount',
10277
'-c', 'pass']
10378
run_cmd('Reference run', cmd)
10479

10580
# Import the C++ extension
106-
cmd = [python,
81+
cmd = [python_exe,
10782
'-X', 'dev',
10883
'-X', 'showrefcount',
10984
'-c', f"import {extension_name}"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
When the Python build is configured ``--with-wheel-pkg-dir``, tests
2+
requiring the ``setuptools`` and ``wheel`` wheels will search for the wheels
3+
in ``WHEEL_PKG_DIR``.

0 commit comments

Comments
 (0)