Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 33.9k
bpo-25625: add contextlib.chdir#28271
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
8 commits Select commit Hold shift + click to select a range
1d1f1e8 bpo-25625: add contextlib.chdir
FFY00 b6d9e3e Ensure own exception
ambv cd63024 Use Oxford comma
ambv 6bdf5bb Update Lib/test/test_contextlib.py
ambv e30c9d8 Serhiy's feedback
FFY00 0cb2d8b Merge `main` to work around BPO-45220 unrelated Windows failures
ambv 9ad9398 refeer that the cm is not parallel-safe in the news entry
FFY00 ce10241 fix typo in news
FFY00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| """Utilities for with-statement contexts. See PEP 343.""" | ||
| import abc | ||
| import os | ||
| import sys | ||
| import _collections_abc | ||
| from collections import deque | ||
| @@ -9,7 +10,8 @@ | ||
| __all__ = ["asynccontextmanager", "contextmanager", "closing", "nullcontext", | ||
| "AbstractContextManager", "AbstractAsyncContextManager", | ||
| "AsyncExitStack", "ContextDecorator", "ExitStack", | ||
| "redirect_stdout", "redirect_stderr", "suppress", "aclosing"] | ||
| "redirect_stdout", "redirect_stderr", "suppress", "aclosing", | ||
| "chdir"] | ||
| class AbstractContextManager(abc.ABC): | ||
| @@ -754,3 +756,18 @@ async def __aenter__(self): | ||
| async def __aexit__(self, *excinfo): | ||
| pass | ||
| class chdir(AbstractContextManager): | ||
| """Non thread-safe context manager to change the current working directory.""" | ||
| def __init__(self, path): | ||
| self.path = path | ||
| self._old_cwd = [] | ||
warsaw marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| def __enter__(self): | ||
| self._old_cwd.append(os.getcwd()) | ||
| os.chdir(self.path) | ||
| def __exit__(self, *excinfo): | ||
| os.chdir(self._old_cwd.pop()) | ||
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
3 changes: 3 additions & 0 deletions 3 Misc/NEWS.d/next/Library/2021-09-10-12-53-28.bpo-25625.SzcBCw.rst
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Added non parallel-safe :func:`~contextlib.chdir` context manager to change | ||
| the current working directory and then restore it on exit. Simple wrapper | ||
| around :func:`~os.chdir`. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
typo on
aync#29091