diff options
| author | Bernát Gábor <bgabor8@bloomberg.net> | 2020-02-23 18:52:30 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-23 18:52:30 +0000 |
| commit | ef711b75ed8947e63f0d1e21ef34928239b8e545 (patch) | |
| tree | 79356223dac98293e7470deb8fbbbddda2db5e63 /src | |
| parent | 82b0d8b12adc3b7241e61d8d0d1a44c534362422 (diff) | |
| download | virtualenv-ef711b75ed8947e63f0d1e21ef34928239b8e545.tar.gz | |
fix pypy2 builtins are imported as stdlib modules (#1656)
* fix pypy2 builtins are imported as stdlib modules
Resolves #1652.
Signed-off-by: Bernat Gabor <bgabor8@bloomberg.net>
* test failure
Signed-off-by: Bernat Gabor <bgabor8@bloomberg.net>
Diffstat (limited to 'src')
4 files changed, 48 insertions, 20 deletions
diff --git a/src/virtualenv/create/via_global_ref/builtin/pypy/pypy2.py b/src/virtualenv/create/via_global_ref/builtin/pypy/pypy2.py index c3a9171..44edab6 100644 --- a/src/virtualenv/create/via_global_ref/builtin/pypy/pypy2.py +++ b/src/virtualenv/create/via_global_ref/builtin/pypy/pypy2.py @@ -2,6 +2,7 @@ from __future__ import absolute_import, unicode_literals import abc import logging +import os from six import add_metaclass @@ -65,6 +66,14 @@ class PyPy2(PyPy, Python2): logging.debug("no include folders as can't find include marker %s", host_include_marker) return dirs + @property + def skip_rewrite(self): + """ + PyPy2 built-in imports are handled by this path entry, don't overwrite to not disable it + see: https://github.com/pypa/virtualenv/issues/1652 + """ + return 'or value.endswith("lib_pypy{}__extensions__")'.format(os.sep) + class PyPy2Posix(PyPy2, PosixSupports): """PyPy 2 on POSIX""" diff --git a/src/virtualenv/create/via_global_ref/builtin/python2/python2.py b/src/virtualenv/create/via_global_ref/builtin/python2/python2.py index 4884f6e..f9e410a 100644 --- a/src/virtualenv/create/via_global_ref/builtin/python2/python2.py +++ b/src/virtualenv/create/via_global_ref/builtin/python2/python2.py @@ -32,16 +32,25 @@ class Python2(ViaGlobalRefVirtualenvBuiltin, Python2Supports): else: 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() - ) + + reload_code = os.linesep.join(" {}".format(i) for i in self.reload_code.splitlines()).lstrip() + custom_site_text = custom_site_text.replace("# ___RELOAD_CODE___", reload_code) + + skip_rewrite = os.linesep.join(" {}".format(i) for i in self.skip_rewrite.splitlines()).lstrip() + custom_site_text = custom_site_text.replace("# ___SKIP_REWRITE____", skip_rewrite) + 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' + @property + def skip_rewrite(self): + return "" + @classmethod def sources(cls, interpreter): for src in super(Python2, cls).sources(interpreter): diff --git a/src/virtualenv/create/via_global_ref/builtin/python2/site.py b/src/virtualenv/create/via_global_ref/builtin/python2/site.py index a2ffa9c..d0aafcd 100644 --- a/src/virtualenv/create/via_global_ref/builtin/python2/site.py +++ b/src/virtualenv/create/via_global_ref/builtin/python2/site.py @@ -86,16 +86,17 @@ def rewrite_standard_library_sys_path(): for at, value in enumerate(sys.path): value = abs_path(value) # replace old sys prefix path starts with new - if value == exe_dir: - pass # don't fix the current executable location, notably on Windows this gets added - elif value.startswith(exe_dir): - # content inside the exe folder needs to remap to original executables folder - orig_exe_folder = base_executable[: base_executable.rfind(sep)] - value = "{}{}".format(orig_exe_folder, value[len(exe_dir) :]) - elif value.startswith(prefix): - value = "{}{}".format(base_prefix, value[len(prefix) :]) - elif value.startswith(exec_prefix): - value = "{}{}".format(base_exec_prefix, value[len(exec_prefix) :]) + skip_rewrite = value == exe_dir # don't fix the current executable location, notably on Windows this gets added + skip_rewrite = skip_rewrite # ___SKIP_REWRITE____ + if not skip_rewrite: + if value.startswith(exe_dir): + # content inside the exe folder needs to remap to original executables folder + orig_exe_folder = base_executable[: base_executable.rfind(sep)] + value = "{}{}".format(orig_exe_folder, value[len(exe_dir) :]) + elif value.startswith(prefix): + value = "{}{}".format(base_prefix, value[len(prefix) :]) + elif value.startswith(exec_prefix): + value = "{}{}".format(base_exec_prefix, value[len(exec_prefix) :]) sys.path[at] = value diff --git a/src/virtualenv/seed/via_app_data/pip_install/base.py b/src/virtualenv/seed/via_app_data/pip_install/base.py index 904008a..4285574 100644 --- a/src/virtualenv/seed/via_app_data/pip_install/base.py +++ b/src/virtualenv/seed/via_app_data/pip_install/base.py @@ -4,7 +4,6 @@ import logging import os import re import shutil -import sys import zipfile from abc import ABCMeta, abstractmethod from contextlib import contextmanager @@ -135,28 +134,38 @@ class PipInstall(object): maker = ScriptMaker(None, str(to_folder)) maker.clobber = True # overwrite - maker.variants = {"", "X", "X.Y"} # create all variants + maker.variants = {""} maker.set_mode = True # ensure they are executable maker.executable = str(self._creator.exe) specification = "{} = {}".format(name, value) - with self.switch_sys_version(version_info): + with self.patch_distlib_correct_variants(version_info, maker): new_files = maker.make(specification) result.extend(Path(i) for i in new_files) return result @contextmanager - def switch_sys_version(self, version_info): + def patch_distlib_correct_variants(self, version_info, maker): """ Patch until upstream distutils supports creating scripts with different python target https://bitbucket.org/pypa/distlib/issues/134/allow-specifying-the-version-information """ - previous = sys.version_info + + def _write_script(scriptnames, shebang, script, filenames, ext): + name = next(iter(scriptnames)) + scriptnames = { # add our variants '', 'X', '-X.Y' + name, + "{}{}".format(name, version_info.major), + "{}-{}.{}".format(name, version_info.major, version_info.minor), + } + return previous(scriptnames, shebang, script, filenames, ext) + + previous = maker._write_script with self.lock: - sys.version_info = version_info + maker._write_script = _write_script try: yield finally: - sys.version_info = previous + maker._write_script = previous def clear(self): if self._image_dir.exists(): |
