Skip to content

Commit 7b7795a

Browse files
committed
Clean up some tests since the semantics have been updated
1 parent 615c6e6 commit 7b7795a

2 files changed

Lines changed: 29 additions & 16 deletions

File tree

Lib/site.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,10 @@ def addsitedir(sitedir, known_paths=None, *, defer_processing_start_files=False)
414414
for name in start_names:
415415
_read_start_file(sitedir, name)
416416

417-
# If standalone call (not from main()), flush immediately
418-
# so the caller sees the effect.
417+
# Generally, when addsitedir() is called explicitly, we'll want to process
418+
# all the startup file data immediately. However, when called through
419+
# main(), we'll want to batch up all the startup file processing. main()
420+
# will set this flag to True to defer processing.
419421
if not defer_processing_start_files:
420422
process_startup_files()
421423

Lib/test/test_site.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def test_addsitedir_dotfile(self):
214214
pth_file.cleanup(prep=True)
215215
try:
216216
pth_file.create()
217-
site.addsitedir(pth_file.base_dir, set())
217+
site.addsitedir(pth_file.base_dir)
218218
self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
219219
self.assertIn(pth_file.base_dir, sys.path)
220220
finally:
@@ -228,7 +228,7 @@ def test_addsitedir_hidden_flags(self):
228228
pth_file.create()
229229
st = os.stat(pth_file.file_path)
230230
os.chflags(pth_file.file_path, st.st_flags | stat.UF_HIDDEN)
231-
site.addsitedir(pth_file.base_dir, set())
231+
site.addsitedir(pth_file.base_dir)
232232
self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
233233
self.assertIn(pth_file.base_dir, sys.path)
234234
finally:
@@ -242,7 +242,7 @@ def test_addsitedir_hidden_file_attribute(self):
242242
try:
243243
pth_file.create()
244244
subprocess.check_call(['attrib', '+H', pth_file.file_path])
245-
site.addsitedir(pth_file.base_dir, set())
245+
site.addsitedir(pth_file.base_dir)
246246
self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], sys.path)
247247
self.assertIn(pth_file.base_dir, sys.path)
248248
finally:
@@ -1328,7 +1328,8 @@ def test_extend_syspath_nonexistent_dir(self):
13281328
def test_addsitedir_discovers_start_files(self):
13291329
# addsitedir() should discover .start files and accumulate entries.
13301330
self._make_start("os.path:join\n", name='foo')
1331-
site.addsitedir(self.sitedir, set())
1331+
site.addsitedir(self.sitedir, set(),
1332+
defer_processing_start_files=True)
13321333
fullname = os.path.join(self.sitedir, 'foo.start')
13331334
self.assertIn('os.path:join', site._pending_entrypoints[fullname])
13341335

@@ -1337,7 +1338,8 @@ def test_addsitedir_start_suppresses_pth_imports(self):
13371338
# at flush time by _exec_imports().
13381339
self._make_start("os.path:join\n", name='foo')
13391340
self._make_pth("import sys\n", name='foo')
1340-
site.addsitedir(self.sitedir, set())
1341+
site.addsitedir(self.sitedir, set(),
1342+
defer_processing_start_files=True)
13411343
pth_fullname = os.path.join(self.sitedir, 'foo.pth')
13421344
start_fullname = os.path.join(self.sitedir, 'foo.start')
13431345
# Import line was collected...
@@ -1352,15 +1354,17 @@ def test_addsitedir_pth_paths_still_work_with_start(self):
13521354
os.mkdir(subdir)
13531355
self._make_start("os.path:join\n", name='foo')
13541356
self._make_pth("mylib\n", name='foo')
1355-
site.addsitedir(self.sitedir, set())
1357+
site.addsitedir(self.sitedir, set(),
1358+
defer_processing_start_files=True)
13561359
fullname = os.path.join(self.sitedir, 'foo.pth')
13571360
self.assertIn(subdir, site._pending_syspaths.get(fullname, []))
13581361

13591362
def test_addsitedir_start_alphabetical_order(self):
13601363
# Multiple .start files are discovered alphabetically.
13611364
self._make_start("os.path:join\n", name='zzz')
13621365
self._make_start("os.path:exists\n", name='aaa')
1363-
site.addsitedir(self.sitedir, set())
1366+
site.addsitedir(self.sitedir, set(),
1367+
defer_processing_start_files=True)
13641368
all_entries = self._all_entrypoints()
13651369
entries = [entry for _, entry in all_entries]
13661370
idx_a = entries.index('os.path:exists')
@@ -1375,7 +1379,8 @@ def test_addsitedir_pth_before_start(self):
13751379
os.mkdir(subdir)
13761380
self._make_pth("mylib\n", name='foo')
13771381
self._make_start("os.path:join\n", name='foo')
1378-
site.addsitedir(self.sitedir, set())
1382+
site.addsitedir(self.sitedir, set(),
1383+
defer_processing_start_files=True)
13791384
# Both should be collected.
13801385
pth_fullname = os.path.join(self.sitedir, 'foo.pth')
13811386
start_fullname = os.path.join(self.sitedir, 'foo.start')
@@ -1384,9 +1389,13 @@ def test_addsitedir_pth_before_start(self):
13841389
site._pending_entrypoints.get(start_fullname, []))
13851390

13861391
def test_addsitedir_dotfile_start_ignored(self):
1387-
# .start files starting with '.' are skipped.
1392+
# .start files starting with '.' are skipped. Defer flushing so
1393+
# the assertion against _pending_entrypoints is meaningful;
1394+
# otherwise process_startup_files() would clear the dict
1395+
# regardless of whether the dotfile was picked up.
13881396
self._make_start("os.path:join\n", name='.hidden')
1389-
site.addsitedir(self.sitedir, set())
1397+
site.addsitedir(self.sitedir, set(),
1398+
defer_processing_start_files=True)
13901399
self.assertEqual(site._pending_entrypoints, {})
13911400

13921401
def test_addsitedir_standalone_flushes(self):
@@ -1400,13 +1409,15 @@ def test_addsitedir_standalone_flushes(self):
14001409
# Pending dicts should be cleared after flush.
14011410
self.assertEqual(site._pending_syspaths, {})
14021411

1403-
def test_addsitedir_internal_does_not_flush(self):
1404-
# When called with a known_paths set, addsitedir accumulates
1405-
# but does not flush.
1412+
def test_addsitedir_defer_does_not_flush(self):
1413+
# With defer_processing_start_files=True, addsitedir accumulates
1414+
# pending state but does not flush; sys.path is updated only when
1415+
# process_startup_files() is called explicitly.
14061416
subdir = os.path.join(self.sitedir, 'acclib')
14071417
os.mkdir(subdir)
14081418
self._make_pth("acclib\n", name='foo')
1409-
site.addsitedir(self.sitedir, set())
1419+
site.addsitedir(self.sitedir, set(),
1420+
defer_processing_start_files=True)
14101421
# Path is pending, not yet on sys.path.
14111422
self.assertNotIn(subdir, sys.path)
14121423
fullname = os.path.join(self.sitedir, 'foo.pth')

0 commit comments

Comments
 (0)