gh-89727: Fix os.fwalk RecursionError on deep trees#100347
Conversation
|
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
Sorry, something went wrong.
|
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
Sorry, something went wrong.
|
TODO:
|
Sorry, something went wrong.
Use a stack to implement os.walk iteratively instead of recursively to avoid hitting recursion limits on deeply nested trees.
…2015) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Christian Heimes <christian@python.org> Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com> Fixes python#89051
…ions in imaplib docs (python#99625)
…with `_read_ready_cb` (python#100349)
Make sure file descriptors get closed if an exception occurs right after the file descriptor was popped from the stack. Also catch exceptions thrown by calling os.close on an already-closed file-descriptor when doing file descriptor cleanup.
|
Note that handling of a single exception should function the same, but repeated errors will interrupt the file descriptor cleanup (closing). Imagine a user holds down try:
...
except:
for fd in reversed(fd_stack):
try:
close(fd)
except OSError:
pass
raiseI'm not sure this is a problem. If I understand correctly, neither versions handles this perfectly. It seemed worth mentioning though. I thought about changing the |
Sorry, something went wrong.
|
Opening this up for review in response to #89727 (comment) |
Sorry, something went wrong.
If an exception occurred between `close(fd_stack[-1])` and `fd_stack.pop()`, then we would close the fd a second time in the except clause. It would be better to risk leaving the fd open, as the previous implementation of fwalk could
barneygale
left a comment
There was a problem hiding this comment.
LGTM, nice work!
Sorry, something went wrong.
|
A thought: could you merge |
Sorry, something went wrong.
|
@jonburdo I've fixed this in another PR - #119638. Shamefully I forgot about this PR :(. Comparing our code, I think we arrived at similar solutions, the main difference being that I moved the |
Sorry, something went wrong.
|
@barneygale No worries! Thanks for mentioning it. I meant to follow up on this. I'll take a look at your PR. Thanks for working on this function and the related ones! It feels great to see these improvements :) |
Sorry, something went wrong.
Use a stack to implement os.fwalk iteratively instead of recursively to avoid hitting recursion limits on deeply nested trees.
Similar to how this is done for
os.walkin #99803