Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix global site package always being added with bundled macOs python framework builds #1653

Merged
merged 1 commit into from
Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog/1561.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix global site package always being added with bundled macOs python framework builds - by :user:`gaborbernat`.
22 changes: 22 additions & 0 deletions src/virtualenv/create/via_global_ref/builtin/cpython/mac_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import struct
import subprocess
from textwrap import dedent

from virtualenv.create.via_global_ref.builtin.cpython.common import CPythonPosix
from virtualenv.create.via_global_ref.builtin.ref import PathRefToDest
Expand Down Expand Up @@ -48,6 +49,27 @@ def _executables(cls, interpreter):
fixed_host_exe = Path(interpreter.prefix) / "Resources" / "Python.app" / "Contents" / "MacOS" / "Python"
yield fixed_host_exe, targets

@property
def reload_code(self):
result = super(CPython2macOsFramework, self).reload_code
result = dedent(
"""
# the bundled site.py always adds the global site package if we're on python framework build, escape this
import sysconfig
config = sysconfig.get_config_vars()
before = config["PYTHONFRAMEWORK"]
try:
config["PYTHONFRAMEWORK"] = ""
{}
finally:
config["PYTHONFRAMEWORK"] = before
""".format(
result
)
)
return result


def fix_mach_o(exe, current, new, max_size):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ def create(self):
custom_site_text = custom_site.read_text()
expected = json.dumps([os.path.relpath(ensure_text(str(i)), ensure_text(str(site_py))) for i in self.libs])
custom_site_text = custom_site_text.replace("___EXPECTED_SITE_PACKAGES___", expected)
custom_site_text = custom_site_text.replace(
"# ___RELOAD_CODE___", os.linesep.join(" {}".format(i) for i in self.reload_code.splitlines()).lstrip()
)
site_py.write_text(custom_site_text)

@property
def reload_code(self):
return 'reload(sys.modules["site"]) # noqa # call system site.py to setup import libraries'

@classmethod
def sources(cls, interpreter):
for src in super(Python2, cls).sources(interpreter):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def load_host_site():

here = __file__ # the distutils.install patterns will be injected relative to this site.py, save it here

reload(sys.modules["site"]) # noqa # call system site.py to setup import libraries
# ___RELOAD_CODE___

# and then if the distutils site packages are not on the sys.path we add them via add_site_dir; note we must add
# them by invoking add_site_dir to trigger the processing of pth files
Expand Down