Skip to content

Commit

Permalink
Fix global site package always being added with bundled macOs python …
Browse files Browse the repository at this point in the history
…framework builds (#1653)

Signed-off-by: Bernat Gabor <bgabor8@bloomberg.net>
  • Loading branch information
gaborbernat authored Feb 22, 2020
1 parent 26200e0 commit dd20aef
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
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

0 comments on commit dd20aef

Please sign in to comment.