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

gh-99370: fix test_zippath_from_non_installed_posix #99483

Merged
merged 3 commits into from
Nov 15, 2022
Merged
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
111 changes: 60 additions & 51 deletions Lib/test/test_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,58 +547,67 @@ def test_zippath_from_non_installed_posix(self):
rmtree(self.env_dir)
# First try to create a non-installed python. It's not a real full
# functional non-installed python, but enough for this test.
platlibdir = sys.platlibdir
non_installed_dir = os.path.realpath(tempfile.mkdtemp())
try:
bindir = os.path.join(non_installed_dir, self.bindir)
os.mkdir(bindir)
shutil.copy2(sys.executable, bindir)
libdir = os.path.join(non_installed_dir, *self.lib)
os.makedirs(libdir)
landmark = os.path.join(libdir, "os.py")
stdlib_zip = "python%d%d.zip" % sys.version_info[:2]
zip_landmark = os.path.join(non_installed_dir,
self.lib[0],
stdlib_zip)
additional_pythonpath_for_non_installed = []
# Copy stdlib files to the non-installed python so venv can
# correctly calculate the prefix.
for eachpath in sys.path:
if eachpath.endswith(".zip"):
if os.path.isfile(eachpath):
shutil.copyfile(
eachpath,
os.path.join(non_installed_dir, self.lib[0]))
elif os.path.isfile(os.path.join(eachpath, "os.py")):
for name in os.listdir(eachpath):
if name == "site-packages":
continue
fn = os.path.join(eachpath, name)
if os.path.isfile(fn):
shutil.copy(fn, libdir)
elif os.path.isdir(fn):
shutil.copytree(fn, os.path.join(libdir, name))
else:
additional_pythonpath_for_non_installed.append(
eachpath)
cmd = [os.path.join(non_installed_dir, self.bindir, self.exe),
"-m",
"venv",
"--without-pip",
self.env_dir]
# Our fake non-installed python is not fully functional because
# it cannot find the extensions. Set PYTHONPATH so it can run the
# venv module correctly.
pythonpath = os.pathsep.join(
additional_pythonpath_for_non_installed)
subprocess.check_call(cmd, env={"PYTHONPATH": pythonpath})
envpy = os.path.join(self.env_dir, self.bindir, self.exe)
# Now check the venv created from the non-installed python has
# correct zip path in pythonpath.
cmd = [envpy, '-S', '-c', 'import sys; print(sys.path)']
out, err = check_output(cmd)
self.assertTrue(zip_landmark.encode() in out)
finally:
rmtree(non_installed_dir)
self.addCleanup(rmtree, non_installed_dir)
bindir = os.path.join(non_installed_dir, self.bindir)
os.mkdir(bindir)
shutil.copy2(sys.executable, bindir)
libdir = os.path.join(non_installed_dir, platlibdir, self.lib[1])
os.makedirs(libdir)
landmark = os.path.join(libdir, "os.py")
stdlib_zip = "python%d%d.zip" % sys.version_info[:2]
zip_landmark = os.path.join(non_installed_dir,
platlibdir,
stdlib_zip)
additional_pythonpath_for_non_installed = []
# Copy stdlib files to the non-installed python so venv can
# correctly calculate the prefix.
for eachpath in sys.path:
if eachpath.endswith(".zip"):
if os.path.isfile(eachpath):
shutil.copyfile(
eachpath,
os.path.join(non_installed_dir, platlibdir))
elif os.path.isfile(os.path.join(eachpath, "os.py")):
for name in os.listdir(eachpath):
if name == "site-packages":
continue
fn = os.path.join(eachpath, name)
if os.path.isfile(fn):
shutil.copy(fn, libdir)
elif os.path.isdir(fn):
shutil.copytree(fn, os.path.join(libdir, name))
else:
additional_pythonpath_for_non_installed.append(
eachpath)
cmd = [os.path.join(non_installed_dir, self.bindir, self.exe),
"-m",
"venv",
"--without-pip",
self.env_dir]
# Our fake non-installed python is not fully functional because
# it cannot find the extensions. Set PYTHONPATH so it can run the
# venv module correctly.
pythonpath = os.pathsep.join(
additional_pythonpath_for_non_installed)
# For python built with shared enabled. We need to set
# LD_LIBRARY_PATH so the non-installed python can find and link
# libpython.so
ld_library_path = os.path.abspath(os.path.dirname(sys.executable))
if sys.platform == 'darwin':
ld_library_path_env = "DYLD_LIBRARY_PATH"
else:
ld_library_path_env = "LD_LIBRARY_PATH"
subprocess.check_call(cmd,
env={"PYTHONPATH": pythonpath,
ld_library_path_env: ld_library_path})
envpy = os.path.join(self.env_dir, self.bindir, self.exe)
# Now check the venv created from the non-installed python has
# correct zip path in pythonpath.
cmd = [envpy, '-S', '-c', 'import sys; print(sys.path)']
out, err = check_output(cmd)
self.assertTrue(zip_landmark.encode() in out)

@requireVenvCreate
class EnsurePipTest(BaseTest):
Expand Down