[issue45545] chdir __exit__ is not safe

Eryk Sun report at bugs.python.org
Tue Oct 26 01:37:29 EDT 2021


Eryk Sun <eryksun at gmail.com> added the comment:

> Alternatively, can't we just os.chdir(self._old_cwd) in __enter__ and preemptively fail? 

If the context manager isn't going to address the long-path case reliably using either a file-descriptor approach or repeated relative chdir() calls, then I think failing early like this is the next best choice.

The previous directory getting deleted is a random environment error, which can be left up to the caller. In POSIX, it might be avoidable using a file-descriptor approach, but POSIX doesn't actually guarantee that fchdir() will succeed if the file descriptor refers to a deleted directory.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45545>
_______________________________________


More information about the Python-bugs-list mailing list