Opened 3 years ago
Last modified 3 years ago
#32821 closed Cleanup/optimization
Use a with statement when using os.scandir() — at Version 1
Reported by: | Chris Jerdonek | Owned by: | nobody |
---|---|---|---|
Component: | Core (Other) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description (last modified by )
I noticed that Django doesn't use a with
statement (or call close()
) when it uses os.scandir()
, which is what the Python docs advise as of 3.6. Here is one example in the code. There appear to be 7 uses of os.scandir()
in all, with 5 in test code.
Usage with the with
statement looks like this:
with os.scandir(path) as entries: for entry in entries: ...
Not using a with
statement or closing the iterator can cause a ResourceWarning
, e.g. when an exception is raised.
Note:
See TracTickets
for help on using tickets.