← 返回首页
bpo-25625: add contextlib.chdir by FFY00 · Pull Request #28271 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

bpo-25625: add contextlib.chdir#28271

Merged
ambv merged 8 commits into
python:mainfrom
FFY00:bpo-25625
Oct 19, 2021
Merged

bpo-25625: add contextlib.chdir#28271
ambv merged 8 commits into
python:mainfrom
FFY00:bpo-25625

Conversation

FFY00 commented Sep 10, 2021
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Member

This is probably the single snippet of code I find myself re-implementing the
most in projects. Not being thread safe is not optimal, but there isn't
really any good way to do so, and that does not negate the huge
usefulness of this function.

Signed-off-by: Filipe Laíns lains@riseup.net

https://bugs.python.org/issue25625

This is probably the single snippet of code I find myself re-implementing the most in projects. Not being thread safe is not optimal, but there isn't really any good way to do so, and that does not negate the huge usefulness of this function. Signed-off-by: Filipe Laíns <lains@riseup.net>
Comment thread Lib/test/test_contextlib.py Outdated Show resolved Hide resolved
Comment thread Lib/test/test_contextlib.py Outdated Show resolved Hide resolved
Comment thread Doc/library/contextlib.rst Outdated Show resolved Hide resolved
Comment thread Lib/test/test_contextlib.py Outdated Show resolved Hide resolved
Comment thread Lib/test/test_contextlib.py Outdated Show resolved Hide resolved
Co-authored-by: Filipe Laíns <filipe.lains@gmail.com>
Comment thread Doc/library/contextlib.rst Outdated Show resolved Hide resolved
Comment thread Lib/test/test_contextlib.py Outdated Show resolved Hide resolved
Signed-off-by: Filipe Laíns <lains@riseup.net>

tiran 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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

I'm -1 to add this feature.

The presence of a chdir decorator in Python's stdlib promotes the idea that the feature is a good standard practice. However it is ill-advised to change process global state such as the working directory in most cases.

serhiy-storchaka 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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Technically it looks correct to me. I am not sure that such feature should be added, although the can of worms was opened by redirect_stdout().

warsaw commented Sep 16, 2021

Copy link
Copy Markdown
Member

I agree with the comment that it can be problematic, but OTOH it's very common to reimplement this code over and over. There's something to be said for providing a solid reference implementation that at least does the best you can do, but with big red warnings for when it can go wrong. Not having this in the stdlib isn't going to stop people from doing it anyway.

ambv commented Sep 17, 2021

Copy link
Copy Markdown
Contributor

The presence of a chdir decorator in Python's stdlib promotes the idea that the feature is a good standard practice. However it is ill-advised to change process global state such as the working directory in most cases.

I disagree. os.chdir exists since forever, it "promotes" the idea the same way as a context manager would. If anything, the context manager is a better way to do it, since it will at least return to the previous state.

Sure, any global modifications of the environment are potential anti-patterns, like monkey-patching standard input/output that Serhiy already mentioned (see BPO-30511 for an egregious example why that is a problem).

That being said, again, os.chdir() is a popular facility of the language, used in our unit tests many times. Using a context manager for it would make more sense. In fact, we already do support that.

1st1 commented Sep 17, 2021

Copy link
Copy Markdown
Member

I'm really not sure this should be part of contextlib (or the stdlib). I'd just add this as a recipe to the docs.

Signed-off-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@riseup.net>

FFY00 commented Oct 18, 2021

Copy link
Copy Markdown
Member Author

Per python/steering-council#77, it seems that addition of such a context manager was approved, granted that we have an appropriate warning.
I think the current warning is good enough, does anyone have suggestions?

Otherwise, this PR is now unblocked 🙂

Comment thread Lib/contextlib.py Show resolved Hide resolved
ambv merged commit 3592980 into python:main Oct 19, 2021

Non parallel-safe context manager to change the current working directory.
As this changes a global state, the working directory, it is not suitable
for use in most threaded or aync contexts. It is also not suitable for most

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

typo on aync #29091

jcfr added a commit to jcfr/Slicer that referenced this pull request Apr 29, 2022
This commit implements a non thread-safe context manager to change the current working directory. Available in Python 3.11 as ``contextlib.chdir`` and adapted from python/cpython#28271.
jcfr added a commit to Slicer/Slicer that referenced this pull request Apr 29, 2022
This commit implements a non thread-safe context manager to change the current working directory. Available in Python 3.11 as ``contextlib.chdir`` and adapted from python/cpython#28271.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants

Footer

© 2026 GitHub, Inc.