Skip to content

Commit 6e34cbc

Browse files
committed
Add a test to ensure site.addsitedir() works end-to-end
1 parent cc61acd commit 6e34cbc

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Lib/test/test_site.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,39 @@ def test_addsitedir_internal_does_not_flush(self):
13411341
fullname = os.path.join(self.sitedir, 'foo.pth')
13421342
self.assertIn(subdir, site._pending_syspaths.get(fullname, []))
13431343

1344+
def test_pth_path_is_available_to_start_entrypoint(self):
1345+
# Core PEP 829 invariant: all .pth path extensions are applied to
1346+
# sys.path *before* any .start entry point runs, so an entry
1347+
# point may live in a module reachable only via a .pth-extended
1348+
# path. If the flush phases were inverted, resolving the entry
1349+
# point would fail with ModuleNotFoundError.
1350+
extdir = os.path.join(self.sitedir, 'extdir')
1351+
os.mkdir(extdir)
1352+
modpath = os.path.join(extdir, 'mod.py')
1353+
with open(modpath, 'w') as f:
1354+
f.write("""\
1355+
called = False
1356+
def hook():
1357+
global called
1358+
called = True
1359+
""")
1360+
self.addCleanup(sys.modules.pop, 'mod', None)
1361+
1362+
# extdir is not on sys.path; only the .pth file makes it so.
1363+
self.assertNotIn(extdir, sys.path)
1364+
self._make_pth("extdir\n", name='extlib')
1365+
self._make_start("mod:hook\n", name='extlib')
1366+
1367+
# Standalone addsitedir() triggers the full flush sequence.
1368+
site.addsitedir(self.sitedir)
1369+
1370+
self.assertIn(extdir, sys.path)
1371+
import mod
1372+
self.assertTrue(
1373+
mod.called,
1374+
"entry point did not run; .pth path was likely not applied "
1375+
"before .start entry-point execution")
1376+
13441377

13451378
if __name__ == "__main__":
13461379
unittest.main()

0 commit comments

Comments
 (0)