Skip to content

Commit 909b4f2

Browse files
committed
gh-156404: Restore the 'fullname' variable for .pth import lines
PEP 829 moved .pth handling from site.addpackage() into StartupState._exec_imports(), and the local holding the path of the .pth file being processed was renamed from 'fullname' to 'filename'. Import lines in .pth files generated before Python 3.15 read that local, so they now fail with NameError on every interpreter startup. Inject 'fullname' into the frame which executes pth code, next to the 'sitedir' shim added for gh-149671. Reported downstream as karellen/wheel-axle#38
1 parent e2118b0 commit 909b4f2

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

Lib/site.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,12 @@ def _exec_imports(self):
510510
# generated by setuptools use: sys._getframe(1).f_locals['sitedir'].
511511
sitedir = os.path.dirname(filename)
512512

513+
# Inject 'fullname' local variable in the current frame for
514+
# compatibility with Python 3.14, where the path of the ".pth"
515+
# file being processed was available to import lines under that
516+
# name.
517+
fullname = filename
518+
513519
# Given "/path/to/foo.pth", check whether "/path/to/foo.start" was
514520
# registered in this same batch.
515521
name, dot, pth = filename.rpartition(".")

Lib/test/test_site.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,23 @@ def test_sitedir_variable(self):
247247
sitedir = stdout.getvalue().rstrip()
248248
self.assertEqual(sitedir, pth_dir)
249249

250+
def test_fullname_variable(self):
251+
# gh-156404: ".pth" files generated before Python 3.15 read the path
252+
# of the file being processed from the frame executing the import
253+
# line, under the name "fullname". For example, "wheel-axle"
254+
# generates: "import wheel_axle.runtime;
255+
# wheel_axle.runtime.finalize(fullname);"
256+
code = '; '.join((
257+
"import sys",
258+
"print(fullname)",
259+
"print(filename)",
260+
))
261+
pth_dir, pth_fn = self.make_pth(code)
262+
with support.captured_stdout() as stdout:
263+
site.addpackage(pth_dir, pth_fn, set())
264+
expected = os.path.join(pth_dir, pth_fn)
265+
self.assertEqual(stdout.getvalue().splitlines(), [expected, expected])
266+
250267
# This tests _getuserbase, hence the double underline
251268
# to distinguish from a test for getuserbase
252269
def test__getuserbase(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Restore compatibility with :file:`.pth` files generated before Python 3.15 in
2+
the :mod:`site` module. Inject the ``fullname`` variable in the frame which
3+
executes pth code.

0 commit comments

Comments
 (0)