Skip to content

Commit 3b83675

Browse files
author
BugBounty Mind
committed
gh-149819: Fix .pth files not loaded in Python subprocesses
After PR gh-149583 (Fix double evaluation of .pth and .site files in venvs), .pth files are no longer loaded in subprocesses started with subprocess.run([sys.executable, ...]). The root cause: main() seeds known_paths from removeduppaths() with all sys.path entries inherited from the parent process. addsitedir() then skips .pth processing for every directory already in known_paths. Fix: - main(): call removeduppaths() for dedup but start known_paths as a fresh empty set, so that addsitedir() processes .pth files in every site-packages directory regardless of inherited sys.path. - addsitedir(): move known_paths.add() before the sys.path.append and guard the append with 'sitedir not in sys.path' to avoid creating duplicate entries when called with a fresh known_paths. This preserves the gh-75723 dedup guarantee while allowing subprocesses to load .pth files.
1 parent 50aff5f commit 3b83675

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

Lib/site.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -490,13 +490,16 @@ def addsitedir(sitedir, known_paths=None, *, defer_processing_start_files=False)
490490
reset = False
491491
sitedir, sitedircase = makepath(sitedir)
492492

493-
# If the normcase'd new sitedir isn't already known, append it to
494-
# sys.path, keep a record of it, and process all .pth and .start files
495-
# found in that directory. If the new sitedir is known, be sure not
496-
# to process all of those more than once! gh-75723
493+
# If the normcase'd new sitedir isn't already known, record it to
494+
# prevent re-processing, append it to sys.path (only if not already
495+
# present), and process all .pth and .start files found in that
496+
# directory. Use a direct sys.path membership check for the append
497+
# guard so that callers (like main()) can pass a fresh known_paths
498+
# set while avoiding duplicate sys.path entries (gh-149819).
497499
if sitedircase not in known_paths:
498-
sys.path.append(sitedir)
499500
known_paths.add(sitedircase)
501+
if sitedir not in sys.path:
502+
sys.path.append(sitedir)
500503

501504
try:
502505
names = os.listdir(sitedir)
@@ -1000,7 +1003,8 @@ def main():
10001003
global ENABLE_USER_SITE
10011004

10021005
orig_path = sys.path[:]
1003-
known_paths = removeduppaths()
1006+
removeduppaths()
1007+
known_paths = set()
10041008
if orig_path != sys.path:
10051009
# removeduppaths() might make sys.path absolute.
10061010
# Fix __file__ of already imported modules too.

0 commit comments

Comments
 (0)