[issue45545] chdir __exit__ is not safe

Barry A. Warsaw report at bugs.python.org
Tue Oct 26 00:48:06 EDT 2021


Barry A. Warsaw <barry at python.org> added the comment:

> A LBYL won't always raise errors early as you point out. It will give earlier warnings for a lot of cases, but makes contextlib.chdir usable in less places than os.chdir.
> Some return paths will always be errors, and some will be technically recoverable but too difficult to detect and or fragile. That's why I think any solution should incorporate the `ignore_errors` flag. Its pretty ugly to wrap a context manager in a try: except: just because you were trying to clean up after whatever you were doing but the cwd changed in unexpected ways, maybe out of your control.

How common do you expect such errors to be though?  Do you expect them to be more or less common than with os.chdir()?  Do you expect the mitigations to be any different than with a failing os.chdir()?

I’ve certainly written a chdir context manager several times and for the use cases I care about, I’ve never had such a failure, at least not one that wasn’t caused by some other underlying bug, which I was glad wasn’t silenced.

----------

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


More information about the Python-bugs-list mailing list