Skip to content

Commit

Permalink
Merge pull request #137 from zooba/issue-132
Browse files Browse the repository at this point in the history
Detect when a venv is created from an in-tree build. Fixes #132
  • Loading branch information
jaraco committed Apr 30, 2022
2 parents 41d5f06 + 2632620 commit 13bb77f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
20 changes: 20 additions & 0 deletions distutils/_functools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import functools


# from jaraco.functools 3.5
def pass_none(func):
"""
Wrap func so it's not called if its first param is None
>>> print_text = pass_none(print)
>>> print_text('text')
text
>>> print_text(None)
"""

@functools.wraps(func)
def wrapper(param, *args, **kwargs):
if param is not None:
return func(param, *args, **kwargs)

return wrapper
22 changes: 18 additions & 4 deletions distutils/sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from .errors import DistutilsPlatformError
from . import py39compat
from ._functools import pass_none

IS_PYPY = '__pypy__' in sys.builtin_module_names

Expand Down Expand Up @@ -51,12 +52,25 @@ def _is_python_source_dir(d):

_sys_home = getattr(sys, '_home', None)


def _is_parent(dir_a, dir_b):
"""
Return True if a is a parent of b.
"""
return os.path.normcase(dir_a).startswith(os.path.normcase(dir_b))


if os.name == 'nt':
@pass_none
def _fix_pcbuild(d):
if d and os.path.normcase(d).startswith(
os.path.normcase(os.path.join(PREFIX, "PCbuild"))):
return PREFIX
return d
# In a venv, sys._home will be inside BASE_PREFIX rather than PREFIX.
prefixes = PREFIX, BASE_PREFIX
matched = (
prefix
for prefix in prefixes
if _is_parent(d, os.path.join(prefix, "PCbuild"))
)
return next(matched, d)
project_base = _fix_pcbuild(project_base)
_sys_home = _fix_pcbuild(_sys_home)

Expand Down
28 changes: 28 additions & 0 deletions distutils/tests/test_sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import textwrap
import unittest

import jaraco.envs

import distutils
from distutils import sysconfig
from distutils.ccompiler import get_default_compiler
from distutils.unixccompiler import UnixCCompiler
Expand Down Expand Up @@ -309,6 +312,31 @@ def test_win_ext_suffix(self):
self.assertTrue(sysconfig.get_config_var("EXT_SUFFIX").endswith(".pyd"))
self.assertNotEqual(sysconfig.get_config_var("EXT_SUFFIX"), ".pyd")

@unittest.skipUnless(
sys.platform == 'win32',
'Testing Windows build layout')
@unittest.skipUnless(
sys.implementation.name == 'cpython',
'Need cpython for this test')
@unittest.skipUnless(
'\\PCbuild\\'.casefold() in sys.executable.casefold(),
'Need sys.executable to be in a source tree')
def test_win_build_venv_from_source_tree(self):
"""Ensure distutils.sysconfig detects venvs from source tree builds."""
env = jaraco.envs.VEnv()
env.create_opts = env.clean_opts
env.root = TESTFN
env.ensure_env()
cmd = [
env.exe(),
"-c",
"import distutils.sysconfig; print(distutils.sysconfig.python_build)"
]
distutils_path = os.path.dirname(os.path.dirname(distutils.__file__))
out = subprocess.check_output(cmd, env={**os.environ, "PYTHONPATH": distutils_path})
assert out == "True"


def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SysconfigTestCase))
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ minversion = 3.25
[testenv]
deps =
pytest
jaraco.envs>=2.4
commands =
pytest {posargs}
setenv =
Expand Down

0 comments on commit 13bb77f

Please sign in to comment.