This commit is contained in:
Filipe Laíns 2025-12-23 01:07:51 -05:00 committed by GitHub
commit 696674fe2a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 1 deletions

View file

@ -807,7 +807,12 @@ class chdir(AbstractContextManager):
self._old_cwd = []
def __enter__(self):
self._old_cwd.append(os.getcwd())
# try to chdir to the current cwd so that we preemptively fail if we are
# unnable to chdir back to it, see bpo-45545
old_cwd = os.getcwd()
os.chdir(old_cwd)
self._old_cwd.append(old_cwd)
os.chdir(self.path)
def __exit__(self, *excinfo):

View file

@ -0,0 +1,2 @@
Premptively fail in :func:`contextdir.chdir` if we can't change back to the
original directory.