gh-149671: Restore compatibility with setuptools nspkg.pth files in site module#151319
gh-149671: Restore compatibility with setuptools nspkg.pth files in site module#151319vstinner wants to merge 2 commits into
Conversation
…s in site module Inject "sitedir" variable in the frame which executes pth code.
| 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 |
There was a problem hiding this comment.
If adding an attribute is a blocker issue, Petr suggested to recreate sitedir from the PTH filename: #149671 (comment).
There was a problem hiding this comment.
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
importline support we'll have to make changes in more than one place, and by that time, we may not remember why we addedself._sitedirin the first place (or, clients will come to depend on it even though it's non-public).
@encukou approach is less yucky.
| 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 |
There was a problem hiding this comment.
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
importline support we'll have to make changes in more than one place, and by that time, we may not remember why we addedself._sitedirin the first place (or, clients will come to depend on it even though it's non-public).
@encukou approach is less yucky.
|
|
||
| def test_sitedir_variable(self): | ||
| # gh-149671: Provide 'sitedir' local variable for compatibility with | ||
| # Python 3.14 |
There was a problem hiding this comment.
| # 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.
There was a problem hiding this comment.
probably a re-entrancy problem
or maybe not, but I still prefer the approach without the added attribute.
There was a problem hiding this comment.
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.
| # Python 3.14 | ||
| code = '; '.join(( | ||
| "import sys", | ||
| # Code used by "-nspkg.pth" files generated by setuptools |
There was a problem hiding this comment.
| # 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` | |||
There was a problem hiding this comment.
| Restore compatibility with setuptools ``nspkg.pth`` files in the :mod:`site` | |
| Restore compatibility with setuptools ``-nspkg.pth`` files in the :mod:`site` |
|
When you're done making the requested changes, leave the comment: |
Inject "sitedir" variable in the frame which executes pth code.
KeyError: "local variable ''sitedir'' is not defined"#149671