Skip to content

Commit 60ae2f3

Browse files
committed
Address Barry's review
1 parent e21f879 commit 60ae2f3

3 files changed

Lines changed: 8 additions & 12 deletions

File tree

Lib/site.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ class StartupState:
254254
'_path_entries',
255255
'_importexecs',
256256
'_entrypoints',
257-
'_sitedir',
258257
)
259258

260259
def __init__(self, known_paths=None):
@@ -287,7 +286,6 @@ def __init__(self, known_paths=None):
287286
# when an entry fails.
288287
self._importexecs = {}
289288
self._entrypoints = {}
290-
self._sitedir = None
291289

292290
def addsitedir(self, sitedir):
293291
"""Add a site directory and accumulate its .pth and .start startup data.
@@ -399,7 +397,6 @@ def _read_pth_file(self, sitedir, name):
399397
accepted paths are added to it so that subsequent .pth files in
400398
the same batch don't add them more than once.
401399
"""
402-
self._sitedir = sitedir
403400

404401
lines, filename = _read_pthstart_file(sitedir, name, ".pth")
405402
if lines is None:
@@ -504,16 +501,16 @@ def _extend_syspath(self):
504501
)
505502

506503
def _exec_imports(self):
507-
# Inject 'sitedir' local variable in the current frame for
508-
# compatibility with Python 3.14. Especially, "-nspkg.pth" files
509-
# generated by setuptools use: sys._getframe(1).f_locals['sitedir'].
510-
sitedir = self._sitedir
511-
512504
# For each `import` line we've seen in a .pth file, exec() it in
513505
# order, unless the .pth has a matching .start file in this same
514506
# batch. In that case, PEP 829 says the import lines are
515507
# suppressed in favor of the .start's entry points.
516508
for filename, imports in self._importexecs.items():
509+
# Inject 'sitedir' local variable in the current frame for
510+
# compatibility with Python 3.14. Especially, "-nspkg.pth" files
511+
# generated by setuptools use: sys._getframe(1).f_locals['sitedir'].
512+
sitedir = os.path.dirname(filename)
513+
517514
# Given "/path/to/foo.pth", check whether "/path/to/foo.start" was
518515
# registered in this same batch.
519516
name, dot, pth = filename.rpartition(".")

Lib/test/test_site.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,10 @@ def test_addsitedir_hidden_file_attribute(self):
234234
self.assertIn(pth_file.base_dir, sys.path)
235235

236236
def test_sitedir_variable(self):
237-
# gh-149671: Provide 'sitedir' local variable for compatibility with
238-
# Python 3.14
237+
# gh-149671: setuptools use of `-nspkg.pth` files in Python < 3.15.
239238
code = '; '.join((
239+
# Code used by "-nspkg.pth" files generated by setuptools.
240240
"import sys",
241-
# Code used by "-nspkg.pth" files generated by setuptools
242241
"sitedir = sys._getframe(1).f_locals['sitedir']",
243242
"print(sitedir)",
244243
))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Restore compatibility with setuptools ``nspkg.pth`` files in the :mod:`site`
1+
Restore compatibility with setuptools ``-nspkg.pth`` files in the :mod:`site`
22
module. Inject ``sitedir`` variable in the frame which executes pth code.
33
Patch by Victor Stinner.

0 commit comments

Comments
 (0)