-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-89708: use fds when possible in contextlib.chdir #132982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This fixes two failure modes: original directories that are longer than PATH_MAX or that were deleted. Use this safer mode when possible, falling back to the existing mode if fds cannot be used.
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a preliminary review but I'm unsure whether it's a good solution. I would actually prefer not to do anything smarter as it was said on the issue. Using file descriptors instead seems a new feature not a bugfix. So I'm rather -1 on this one (there wasn't a real need for that and using file descriptors eventually complicates the logic)
Misc/NEWS.d/next/Library/2025-04-26-12-10-32.gh-issue-89708.haEUh3.rst
Outdated
Show resolved
Hide resolved
Sure, thanks very much for it, nonetheless!
I mildly disagree: there is a need for it, as demonstrated by the OP opening a ticket because the current implementation doesn't work for their purposes, while the new one would. Although admittedly probably not a very common one! I also don't think it complicates the logic very much, although I might design it slightly differently if I was starting from scratch (e.g. maybe have separate It does completely change how the implementation works, and ofc there are ways the abstraction can leak (e.g. file descriptors are a global resource and holding an extra one open could in theory cause problems). I can add a way for the user to force using the original implementation, either globally and/or on a per- I think it is a useful change that is almost a strict improvement on the current implementation anywhere it is applicable, but I can certainly accept other people's opinions differing on this, and would understand if it is rejected. |
And this is usually a good indicator of why it shouldn't be part of the standard library.
That's not sufficient IMO.
That's not how the standard library is meant to be used. There should preferrably one way to do something.
I'd say it's up to the module's maintainer but I still think it does complicate it. In addition, it's not a universal solution as it is conditioned to the platform supporting it. So I'm not really fond of that as well. |
Quite possibly, but it is, so IMO we may as well make it work as best we can.
Yes, agreed, as I said.
Ah, there are maintainers for this module: I should have checked the "experts index" and tagged them in to start with (ping @ncoghlan and @1st1). With regards "conditioned to the platform": IMV this is all inherently platform/filesystem dependent, especially the conditions under which it succeeds/fails, just like However, to repeat, I totally accept this is a significant implementation change and may not be justified. If the maintainers and/or other core devs don't like it then fair enough! |
As some comments in the issue hinted at, holding on to the fd for the duration of the context manager could cause problems: not just using more of a scarce resource, but also potentially preventing operations on the old cwd. Scenarios where that would matter are surely not common, but this may be a compatibility issue for existing users. |
I don't think it should prevent any operations on Linux, but please let me know if I missed anything. It certainly might on Windows, but I was under the impression Windows doesn't support using file descriptors with However, an fd-based alternative could easily be provided on pypi, or just added in directly by those users who care, so it isn't a big deal if it remains as-is in the std library. If people think the risk and/or complexity isn't worth it I'll withdraw the PR. In that case the issue should be closed as unplanned, if I understand things correctly. Thanks for the reviews and feedback, everyone! |
This fixes two failure modes: original directories that are longer than PATH_MAX or that were deleted. Use this safer mode when possible, falling back to the existing mode if fds cannot be used.