Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scandir() crash by returning [] when directory is not found (#13083) #13086

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

delta87
Copy link

@delta87 delta87 commented Dec 26, 2024

This PR addresses issue #13083 by modifying the scandir function to handle the case where a non-existent directory is provided. Previously, the function did not return a result for such cases, potentially leading to unhandled errors. Now, if the directory does not exist, the function will return an empty list instead.

Changes made:

Updated scandir to catch FileNotFoundError and return an empty list if the directory does not exist.
Updated the function docstring to reflect the new behavior.
Testing:

Also two tests were added.
Closes #13083

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Dec 26, 2024
testing/test_pathlib.py Outdated Show resolved Hide resolved
entries.append(entry)
entries.sort(key=sort_key) # type: ignore[arg-type]
return entries
try:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer a narrow try/except as done in #13084, can you please update to use a similar solution?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer a narrow try/except as done in #13084, can you please update to use a similar solution?

Yes, of course

@delta87 delta87 requested a review from nicoddemus December 26, 2024 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided (automation) changelog entry is part of PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Race condition: collection error when directory is deleted before scandir() runs
3 participants