Skip to content

Commit

Permalink
add zip importer test (#1727)
Browse files Browse the repository at this point in the history
Signed-off-by: Bernat Gabor <[email protected]>
  • Loading branch information
gaborbernat authored Mar 17, 2020
1 parent b2185e4 commit 1de7c30
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
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)

0 comments on commit 1de7c30

Please sign in to comment.