#32821: Use a with statement when using os.scandir()
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Uncategorized | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Chris Jerdonek:
Old description:
> I noticed that Django doesn't use a `with` statement (or call `close()`)
> when it uses `os.scandir()`, which is what the
> [https://docs.python.org/3/library/os.html#os.scandir.close Python docs
> advise as of 3.6]. Here is
> [https://github.com/django/django/blob/ecf8af79355c8daa67722bd0de946b351f7f613d/django/core/files/storage.py#L324-L328
> 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:
>
> {{{#!python
> 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.
New description:
I noticed that Django doesn't use a `with` statement (or call `close()`)
when it uses `os.scandir()`, which is what the
[https://docs.python.org/3/library/os.html#os.scandir.close Python docs
advise as of 3.6]. Here is
[https://github.com/django/django/blob/ecf8af79355c8daa67722bd0de946b351f7f613d/django/core/files/storage.py#L324-L328
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:
{{{#!python
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.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32821#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.53aa71f44af4041fb5e32403804ac132%40djangoproject.com.