Opened 3 years ago

Closed 3 years ago

#32821 closed Cleanup/optimization (fixed)

Use a with statement when using os.scandir()

Reported by: Chris Jerdonek Owned by: Chris Jerdonek
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 Chris Jerdonek)

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. Indeed, this is how I first noticed this issue.

Change History (8)

comment:1 by Chris Jerdonek, 3 years ago

Description: modified (diff)

comment:3 by Chris Jerdonek, 3 years ago

Description: modified (diff)

comment:4 by Jacob Walls, 3 years ago

Component: UncategorizedTesting framework
Easy pickings: set
Owner: changed from nobody to Chris Jerdonek
Status: newassigned
Triage Stage: UnreviewedAccepted

comment:5 by Chris Jerdonek, 3 years ago

Two of the occurrences are in non-test code (django/core/files/storage.py and django/forms/fields.py), so "Testing framework" doesn't seem quite right to me. It gives the impression non-test code isn't affected.

comment:6 by Claude Paroz, 3 years ago

Component: Testing frameworkCore (Other)
Triage Stage: AcceptedReady for checkin

comment:7 by Chris Jerdonek, 3 years ago

Thanks, Claude.

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 7272e196:

Fixed #32821 -- Updated os.scandir() uses to use a context manager.

Note: See TracTickets for help on using tickets.
Back to Top