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

add zip importer test #1727

Merged
merged 1 commit into from
Mar 17, 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
2 changes: 2 additions & 0 deletions docs/changelog/1715.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Do not fail if the distutils/setuptools patch happens on a C-extension loader (such as ``zipimporter`` on Python 3.7 or
earlier) - by :user:`gaborbernat`.
5 changes: 4 additions & 1 deletion src/virtualenv/create/via_global_ref/_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ def find_spec(self, fullname, path, target=None):
old = getattr(spec.loader, func_name)
func = self.exec_module if is_new_api else self.load_module
if old is not func:
setattr(spec.loader, func_name, partial(func, old))
try:
setattr(spec.loader, func_name, partial(func, old))
except AttributeError:
pass # C-Extension loaders are r/o such as zipimporter with <python 3.7
return spec
finally:
self.fullname = None
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/create/test_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import stat
import subprocess
import sys
import zipfile
from collections import OrderedDict
from itertools import product
from stat import S_IREAD, S_IRGRP, S_IROTH
Expand Down Expand Up @@ -486,3 +487,25 @@ def exists():
assert not (result.creator.stdlib / "os.py").exists()
assert (result.creator.stdlib / "os.pyc").exists()
assert "os.pyc" in result.creator.debug["os"]


def test_zip_importer_can_import_setuptools(tmp_path):
"""We're patching the loaders so might fail on r/o loaders, such as zipimporter on CPython<3.8"""
result = cli_run([str(tmp_path / "venv"), "--activators", "", "--no-pip", "--no-wheel", "--copies"])
zip_path = tmp_path / "site-packages.zip"
with zipfile.ZipFile(str(zip_path), "w", zipfile.ZIP_DEFLATED) as zip_handler:
lib = str(result.creator.purelib)
for root, _, files in os.walk(lib):
base = root[len(lib) :].lstrip(os.pathsep)
for file in files:
if not file.startswith("_virtualenv"):
zip_handler.write(filename=os.path.join(root, file), arcname=os.path.join(base, file))
for folder in result.creator.purelib.iterdir():
if not folder.name.startswith("_virtualenv"):
if folder.is_dir():
shutil.rmtree(str(folder), ignore_errors=True)
else:
folder.unlink()
env = os.environ.copy()
env[str("PYTHONPATH")] = str(zip_path)
subprocess.check_call([str(result.creator.exe), "-c", "from setuptools.dist import Distribution"], env=env)