Skip to content
Prev Previous commit
Check sysconfig for site-packages path
  • Loading branch information
lysnikolaou committed May 26, 2023
commit b3ab1a90536edb8ce93521057975b7fcb76fd241
2 changes: 1 addition & 1 deletion Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2304,7 +2304,7 @@ def setup_venv_with_pip_setuptools_wheel(venv_dir):
print('Run:', ' '.join(cmd))
subprocess.run(cmd, check=True)

yield venv, python
yield python


# True if Python is built with the Py_DEBUG macro defined: if
Expand Down
12 changes: 6 additions & 6 deletions Lib/test/test_cppext.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ def test_build_cpp03(self):
@support.requires_venv_with_pip()
def check_build(self, std_cpp03, extension_name):
venv_dir = 'env'
with support.setup_venv_with_pip_setuptools_wheel(venv_dir) as (_, python):
self._check_build(std_cpp03, extension_name, python)
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, python):
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"))
Expand All @@ -63,22 +63,22 @@ def run_cmd(operation, cmd):
f"{operation} failed with exit code {proc.returncode}")

# 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)

# 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}"]
Expand Down
12 changes: 7 additions & 5 deletions Lib/test/test_peg_generator/test_c_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import subprocess
import sysconfig
import textwrap
import unittest
Expand Down Expand Up @@ -90,11 +91,12 @@ def setUpClass(cls):
cls.addClassCleanup(shutil.rmtree, cls.library_dir)

with contextlib.ExitStack() as stack:
full_venv_path, _ = stack.enter_context(support.setup_venv_with_pip_setuptools_wheel("venv"))
stack.enter_context(import_helper.DirsOnSysPath(os.path.join(full_venv_path,
"lib",
f"python{sysconfig.get_python_version()}",
"site-packages")))
python_exe = stack.enter_context(support.setup_venv_with_pip_setuptools_wheel("venv"))
sitepackages = subprocess.check_output(
[python_exe, "-c", "import sysconfig; print(sysconfig.get_path('platlib'))"],
text=True,
).strip()
stack.enter_context(import_helper.DirsOnSysPath(sitepackages))
cls.addClassCleanup(stack.pop_all().close)

@support.requires_venv_with_pip()
Expand Down