From 2c44b7a474704212f71f1f29c8152a397e5688f1 Mon Sep 17 00:00:00 2001 From: Dmitrii Sutiagin Date: Wed, 2 Nov 2022 15:35:34 -0700 Subject: [PATCH] Fix python-version-specific paths in PYTHONPATH When SHIV_EXTED_PYTHONPATH is used, PYTHONPATH env variable gets not only zipapp sit-packages and other new stuff, but also the default sys.path of the current Python (for ex. Python3.10). This breaks ability to run a different version of Python in a subprocess, because PYTHONPATH takes precedence over default sys.path, and so a subprocess starting Python3.9 will attempt to use Python 3.10 builtin modules, and will most likely break in unpredictable ways. This change fixes that by only adding new paths which appear after injecting zipapp site-packages. --- setup.cfg | 2 +- src/shiv/bootstrap/__init__.py | 10 ++++++++-- src/shiv/cli.py | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/setup.cfg b/setup.cfg index 400e676..4b9cc3f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -24,7 +24,7 @@ sections = FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER [metadata] name = shiv -version = 1.0.2 +version = 1.0.3 description = A command line utility for building fully self contained Python zipapps. long_description = file: README.md long_description_content_type = text/markdown diff --git a/src/shiv/bootstrap/__init__.py b/src/shiv/bootstrap/__init__.py index 4542952..6f0c584 100644 --- a/src/shiv/bootstrap/__init__.py +++ b/src/shiv/bootstrap/__init__.py @@ -208,6 +208,9 @@ def bootstrap(): # pragma: no cover # Find the first instance of an existing site-packages on sys.path index = get_first_sitedir_index() or length + # copy sys.path to determine diff + sys_path_before = sys.path.copy() + # append site-packages using the stdlib blessed way of extending path # so as to handle .pth files correctly site.addsitedir(site_packages) @@ -215,13 +218,16 @@ def bootstrap(): # pragma: no cover # reorder to place our site-packages before any others found sys.path = sys.path[:index] + sys.path[length:] + sys.path[index:length] + # determine newly added paths + new_paths = [p for p in sys.path if p not in sys_path_before] + # check if source files have been modified, if required if env.no_modify: ensure_no_modify(site_packages, env.hashes) - # add our site-packages to the environment, if requested + # add any new paths to the environment, if requested if env.extend_pythonpath: - extend_python_path(os.environ, sys.path.copy()) + extend_python_path(os.environ, new_paths) # if a preamble script was provided, run it if env.preamble: diff --git a/src/shiv/cli.py b/src/shiv/cli.py index 758e83e..f361091 100644 --- a/src/shiv/cli.py +++ b/src/shiv/cli.py @@ -26,7 +26,7 @@ SOURCE_DATE_EPOCH_ENV, ) -__version__ = "1.0.2" +__version__ = "1.0.3" def find_entry_point(site_packages_dirs: List[Path], console_script: str) -> str: