#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 Changed 16 months ago by Chris Jerdonek

Description: modified (diff)

comment:2 Changed 16 months ago by Chris Jerdonek

Has patch: set

comment:3 Changed 16 months ago by Chris Jerdonek

Description: modified (diff)

comment:4 Changed 16 months ago by Jacob Walls

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

comment:5 Changed 16 months ago by Chris Jerdonek

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 Changed 16 months ago by Claude Paroz

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

comment:7 Changed 16 months ago by Chris Jerdonek

Thanks, Claude.

comment:8 Changed 16 months ago by Mariusz Felisiak <felisiak.mariusz@…>

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