Skip to content

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

Open
vstinner wants to merge 2 commits into
python:mainfrom
vstinner:sitedir
Open

gh-149671: Restore compatibility with setuptools nspkg.pth files in site module#151319
vstinner wants to merge 2 commits into
python:mainfrom
vstinner:sitedir

Conversation

@vstinner

@vstinner vstinner commented Jun 11, 2026

Copy link
Copy Markdown
Member

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

…s in site module

Inject "sitedir" variable in the frame which executes pth code.
@vstinner vstinner requested a review from FFY00 as a code owner June 11, 2026 09:54
@vstinner vstinner added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 11, 2026
@vstinner vstinner requested a review from warsaw as a code owner June 11, 2026 09:54
Comment thread Lib/site.py
accepted paths are added to it so that subsequent .pth files in
the same batch don't add them more than once.
"""
self._sitedir = sitedir

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If adding an attribute is a blocker issue, Petr suggested to recreate sitedir from the PTH filename: #149671 (comment).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using an attribute is problematic for two reasons:

  • Most importantly, multiple calls to StartupState.addsitedir() will overwrite the previous value so there's probably a re-entrancy problem with this approach.
  • It doesn't means when we rip out import line support we'll have to make changes in more than one place, and by that time, we may not remember why we added self._sitedir in the first place (or, clients will come to depend on it even though it's non-public).

@encukou approach is less yucky.

@warsaw warsaw self-assigned this Jun 11, 2026

@warsaw warsaw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this PR together @vstinner. As mentioned below, I think @encukou approach is slightly better, and I'm not opposed to adding it.

Comment thread Lib/site.py
accepted paths are added to it so that subsequent .pth files in
the same batch don't add them more than once.
"""
self._sitedir = sitedir

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using an attribute is problematic for two reasons:

  • Most importantly, multiple calls to StartupState.addsitedir() will overwrite the previous value so there's probably a re-entrancy problem with this approach.
  • It doesn't means when we rip out import line support we'll have to make changes in more than one place, and by that time, we may not remember why we added self._sitedir in the first place (or, clients will come to depend on it even though it's non-public).

@encukou approach is less yucky.

Comment thread Lib/test/test_site.py

def test_sitedir_variable(self):
# gh-149671: Provide 'sitedir' local variable for compatibility with
# Python 3.14

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Python 3.14
# setuptools use of `-nspkg.pth` files in Python < 3.15.

And please add a link to the setuptools issue where they are going to remove these .pth files.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably a re-entrancy problem

or maybe not, but I still prefer the approach without the added attribute.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And please add a link to the setuptools issue where they are going to remove these .pth files.

I don't think there is such an issue.

Comment thread Lib/test/test_site.py
# Python 3.14
code = '; '.join((
"import sys",
# Code used by "-nspkg.pth" files generated by setuptools

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Code used by "-nspkg.pth" files generated by setuptools
# Code used by "-nspkg.pth" files generated by setuptools.

Probably move this comment to just above line 239, since it applies to the whole statement.

Also, test_site.py tends to use dedented triple-quote-strings for stuff like this, but maybe that line will end up being too long anyway since this must end up on the same line in the .pth file. It just always makes me nervous to see string joins with trailing commas because missing commas leads to bugs. Use your discretion.

@@ -0,0 +1,3 @@
Restore compatibility with setuptools ``nspkg.pth`` files in the :mod:`site`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Restore compatibility with setuptools ``nspkg.pth`` files in the :mod:`site`
Restore compatibility with setuptools ``-nspkg.pth`` files in the :mod:`site`

@bedevere-app

bedevere-app Bot commented Jun 11, 2026

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants