Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
sys.prefix
is now set to point to the virtual environment before this code runs, addingsys.prefix
here is now incorrect, we want to addsys.base_prefix
andsys.base_exec_prefix
, and because the existing directories inPREFIXES
are now already the venv, we want to append the base prefixes instead.Additionally, resetting
PREFIXES
whensystem_site
isfalse
is now unnecessary, because it already has the correct value.The comment above can also be removed, or moved to
getsitepackages
, as there's no possibility of us adding a duplicate prefix now.PREFIXES
will already have a duplicated prefix from its initialization if we are running on a virtual environment.Background into this:
GH-126987 moved the venv initialization logic to
getpath
, the code that initializes the prefixes andsys.path
. Previously, that was done here insite
, which was somewhat problematic, but you can read about that in the isssue.So, before that change, when
site
, initializedPREFIXES
on import, it would effectively be set tosys.base_prefix
andsys.base_exec_prefix
, which at that point would always have the same value assys.prefix
andsys.exec_prefix
. 1cpython/Lib/site.py
Line 80 in e635bf2
Then, here in
venv()
, if we detect a virtual environment, we would setsys.prefix
andsys.exec_prefix
tosite_prefix
in the snippet above that now issues warnings if we detect a mismatch (link to the change in GH-126987).After that, we either reset
PREFIXES
to[sys.prefix]
, ifsystem_site
isfalse
, or appendsys.prefix
, which had now been updated tosite_prefix
. We only do this forsys.prefix
, and ignoresys.exec_prefix
, because in virtual environments there's no distinction, they always have the same value.Considering the
getpath
changes, withPREFIXES
getting set to[sys.prefix, sys.exec_prefix]
on import, it already includes the virtual environment prefix, so all we need to do here now is to appendsys.base_prefix
andsys.base_exec_prefix
whensystem_site
istrue
.Footnotes
Why it was not explicitly initialized to the base prefixes, which should be the intent, I am not sure, but I guess that's an implementation detail that did not really matter at the time. ↩