[issue45545] chdir __exit__ is not safe

2021-10-26 Thread Jeremy
Jeremy added the comment: > 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()? It has come up for me with some frequency. But I'm sure

[issue45545] chdir __exit__ is not safe

2021-10-25 Thread Eryk Sun
Eryk Sun 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

[issue45545] chdir __exit__ is not safe

2021-10-25 Thread Barry A. Warsaw
Barry A. Warsaw 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 >

[issue45545] chdir __exit__ is not safe

2021-10-25 Thread Jeremy
Jeremy 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

[issue45545] chdir __exit__ is not safe

2021-10-25 Thread Barry A. Warsaw
Barry A. Warsaw added the comment: Does a LBYL strategy actually fix the problem? E.g. what if the directory gets rm'd between __enter__ and __exit__? Maybe we shouldn't try to be clever at all and just leave it to the user to decide what to do, and how to handle any chdir-back failures?

[issue45545] chdir __exit__ is not safe

2021-10-25 Thread Filipe Laíns
Change by Filipe Laíns : -- pull_requests: +27483 pull_request: https://github.com/python/cpython/pull/29220 ___ Python tracker ___

[issue45545] chdir __exit__ is not safe

2021-10-25 Thread Filipe Laíns
Filipe Laíns added the comment: s/if we can chdir/if we can't chdir/ -- ___ Python tracker ___ ___ Python-bugs-list mailing list

[issue45545] chdir __exit__ is not safe

2021-10-25 Thread Filipe Laíns
Filipe Laíns added the comment: Alternatively, can't we just os.chdir(self._old_cwd) in __enter__ and preemptively fail? IMO it's probably better to just straight up fail if we can chdir back to the original directory than to have relatively fragile recovery logic. --

[issue45545] chdir __exit__ is not safe

2021-10-25 Thread Jeremy
Change by Jeremy : -- keywords: +patch pull_requests: +27481 stage: -> patch review pull_request: https://github.com/python/cpython/pull/29218 ___ Python tracker ___

[issue45545] chdir __exit__ is not safe

2021-10-22 Thread Filipe Laíns
Change by Filipe Laíns : -- nosy: +FFY00 ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe:

[issue45545] chdir __exit__ is not safe

2021-10-21 Thread Eryk Sun
Eryk Sun added the comment: Sorry, I seem to have missed your post last month when I scanned over the thread on python-ideas [1]. In POSIX, it could try to handle ENAMETOOLONG by walking the path forward with a relative chdir(). This could try to consume as many components as possible in

[issue45545] chdir __exit__ is not safe

2021-10-21 Thread Jeremy
Jeremy added the comment: >If os.chdir is in os.supports_fd, the context manager can use dirfd = >os.open(os.getcwd(), os.O_RDONLY). Using an fd should also work around the >deleted directory case, though POSIX doesn't specify whether fchdir() succeeds >in this case. It does in Linux, and

[issue45545] chdir __exit__ is not safe

2021-10-20 Thread Eryk Sun
Eryk Sun added the comment: > # I thought this would have to be 16, i.e. a path length over 4096, PATH_MAX > # but seemingly just crossing 1050 is enough to fail If os.pathconf() and PC_PATH_MAX are supported, the maximum allowed length of an absolute path is os.pathconf('/',

[issue45545] chdir __exit__ is not safe

2021-10-20 Thread Barry A. Warsaw
Barry A. Warsaw added the comment: > Yes, precisely. Besides being an unreachable long abs path, it might have > been deleted since last visited. I’m working on a few more odd test cases. Ah, the deleted case. Sounds like LBYL wouldn’t work in that case then. :( --

[issue45545] chdir __exit__ is not safe

2021-10-20 Thread Jeremy
Jeremy added the comment: Yes, precisely. Besides being an unreachable long abs path, it might have been deleted since last visited. I’m working on a few more odd test cases. -- ___ Python tracker

[issue45545] chdir __exit__ is not safe

2021-10-20 Thread Barry A. Warsaw
Barry A. Warsaw added the comment: Does this mean that CWD could be in a directory that you couldn't chdir() back into? -- nosy: +barry ___ Python tracker ___

[issue45545] chdir __exit__ is not safe

2021-10-20 Thread Jeremy
New submission from Jeremy : The way that contextlib.chdir currently restores the old working directory, an exception is raised if the program was already close to or beyond a system's PATH_MAX. The context manager has no issue crafting the path in __enter__ because os.getcwd() can return a