Skip to content

Commit

Permalink
need to patch setuptools too
Browse files Browse the repository at this point in the history
Signed-off-by: Bernat Gabor <[email protected]>
  • Loading branch information
gaborbernat committed Feb 21, 2020
1 parent 9501fc2 commit eb0dcc4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from virtualenv.create.via_global_ref.builtin.cpython.common import CPythonPosix
from virtualenv.create.via_global_ref.builtin.ref import PathRefToDest
from virtualenv.util.path import Path
from virtualenv.util.six import ensure_text

from .cpython2 import CPython2, is_mac_os_framework

Expand Down Expand Up @@ -73,7 +74,7 @@ def fix_mach_o(exe, current, new, max_size):
unneeded bits of information, however Mac OS X 10.5 and earlier cannot read this new Link Edit table format.
"""
try:
logging.debug("change Mach-O for %s from %s to %s", exe, current, new)
logging.debug(u"change Mach-O for %s from %s to %s", ensure_text(exe), current, ensure_text(new))
_builtin_change_mach_o(max_size)(exe, current, new)
except Exception as e:
logging.warning("Could not call _builtin_change_mac_o: %s. " "Trying to call install_name_tool instead.", e)
Expand Down
32 changes: 21 additions & 11 deletions src/virtualenv/create/via_global_ref/builtin/python2/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def main():
load_host_site()
if global_site_package_enabled:
add_global_site_package()
fix_distutils()
fix_install()


def load_host_site():
Expand Down Expand Up @@ -143,19 +143,29 @@ def add_global_site_package():
site.PREFIXES = orig_prefixes


def fix_distutils():
from distutils import dist
def fix_install():
def patch(dist_of):
# we cannot allow the prefix override as that would get packages installed outside of the virtual environment
old_parse_config_files = dist_of.Distribution.parse_config_files

old_parse_config_files = dist.Distribution.parse_config_files
def parse_config_files(self, *args, **kwargs):
result = old_parse_config_files(self, *args, **kwargs)
install_dict = self.get_option_dict("install")
if "prefix" in install_dict:
install_dict["prefix"] = "virtualenv.patch", abs_path(sys.prefix)
return result

def parse_config_files(self, filenames=None):
old_parse_config_files(self, filenames)
# we cannot allow the prefix override as that would get packages installed outside of the virtual environment
install_dict = self.get_option_dict("install")
if "prefix" in install_dict:
install_dict["prefix"] = "virtualenv.patch", abs_path(sys.prefix)
dist_of.Distribution.parse_config_files = parse_config_files

from distutils import dist

patch(dist)
try:
from setuptools import dist

dist.Distribution.parse_config_files = parse_config_files
patch(dist)
except ImportError:
pass # if setuptools is not around that's alright, just don't patch


main()
9 changes: 5 additions & 4 deletions tests/unit/create/test_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_destination_not_write_able(tmp_path, capsys):
def cleanup_sys_path(paths):
from virtualenv.create.creator import HERE

paths = [Path(os.path.abspath(i)) for i in paths]
paths = [Path(os.path.abspath(i)).resolve() for i in paths]
to_remove = [Path(HERE)]
if os.environ.get(str("PYCHARM_HELPERS_DIR")):
to_remove.append(Path(os.environ[str("PYCHARM_HELPERS_DIR")]).parent)
Expand Down Expand Up @@ -152,11 +152,12 @@ def test_create_no_seed(python, creator, isolated, system, coverage_env, special
assert any(p for p in our_paths if p.parts[-1] == "site-packages"), our_paths_repr

# ensure the global site package is added or not, depending on flag
last_from_system_path = next(j for j in reversed(system_sys_path) if str(j).startswith(system["sys"]["prefix"]))
global_sys_path = system_sys_path[-1]
if isolated == "isolated":
assert last_from_system_path not in sys_path, "last from system sys path {} is in venv sys path:\n{}".format(
ensure_text(str(last_from_system_path)), "\n".join(ensure_text(str(j)) for j in sys_path)
msg = "global sys path {} is in virtual environment sys path:\n{}".format(
ensure_text(str(global_sys_path)), "\n".join(ensure_text(str(j)) for j in sys_path)
)
assert global_sys_path not in sys_path, msg
else:
common = []
for left, right in zip(reversed(system_sys_path), reversed(sys_path)):
Expand Down

0 comments on commit eb0dcc4

Please sign in to comment.