https://github.com/python/cpython/commit/18f3ffec43b98db34c6d378cfc012814a901bb41
commit: 18f3ffec43b98db34c6d378cfc012814a901bb41
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-06-15T08:29:52Z
summary:

gh-149671: Restore compatibility with setuptools -nspkg.pth files in site 
module (#151319)

Inject the "sitedir" variable in the frame which executes ".pth" code.

files:
A Misc/NEWS.d/next/Library/2026-06-11-11-52-23.gh-issue-149671.6Rpr5r.rst
M Lib/site.py
M Lib/test/test_site.py

diff --git a/Lib/site.py b/Lib/site.py
index b7f5c7f0246bc1..d06549b8df800e 100644
--- a/Lib/site.py
+++ b/Lib/site.py
@@ -505,6 +505,11 @@ def _exec_imports(self):
         # batch.  In that case, PEP 829 says the import lines are
         # suppressed in favor of the .start's entry points.
         for filename, imports in self._importexecs.items():
+            # Inject 'sitedir' local variable in the current frame for
+            # compatibility with Python 3.14. Especially, "-nspkg.pth" files
+            # generated by setuptools use: 
sys._getframe(1).f_locals['sitedir'].
+            sitedir = os.path.dirname(filename)
+
             # Given "/path/to/foo.pth", check whether "/path/to/foo.start" was
             # registered in this same batch.
             name, dot, pth = filename.rpartition(".")
diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py
index 5fd65ad999210e..bcb51ed7d8476a 100644
--- a/Lib/test/test_site.py
+++ b/Lib/test/test_site.py
@@ -233,6 +233,20 @@ def test_addsitedir_hidden_file_attribute(self):
             self.assertNotIn(site.makepath(pth_file.good_dir_path)[0], 
sys.path)
             self.assertIn(pth_file.base_dir, sys.path)
 
+    def test_sitedir_variable(self):
+        # gh-149671: setuptools use of `-nspkg.pth` files in Python < 3.15.
+        code = '; '.join((
+            # Code used by "-nspkg.pth" files generated by setuptools.
+            "import sys",
+            "sitedir = sys._getframe(1).f_locals['sitedir']",
+            "print(sitedir)",
+        ))
+        pth_dir, pth_fn = self.make_pth(code)
+        with support.captured_stdout() as stdout:
+            known_paths = site.addpackage(pth_dir, pth_fn, set())
+        sitedir = stdout.getvalue().rstrip()
+        self.assertEqual(sitedir, pth_dir)
+
     # This tests _getuserbase, hence the double underline
     # to distinguish from a test for getuserbase
     def test__getuserbase(self):
diff --git 
a/Misc/NEWS.d/next/Library/2026-06-11-11-52-23.gh-issue-149671.6Rpr5r.rst 
b/Misc/NEWS.d/next/Library/2026-06-11-11-52-23.gh-issue-149671.6Rpr5r.rst
new file mode 100644
index 00000000000000..5c08828e5fd77e
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-06-11-11-52-23.gh-issue-149671.6Rpr5r.rst
@@ -0,0 +1,3 @@
+Restore compatibility with setuptools ``-nspkg.pth`` files in the :mod:`site`
+module. Inject ``sitedir`` variable in the frame which executes pth code.
+Patch by Victor Stinner.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to