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.
--
Ticket URL: <https://code.djangoproject.com/ticket/32821>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
> I noticed that Django doesn't use a `with` statement 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.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32821#comment:1>
* has_patch: 0 => 1
Comment:
PR: https://github.com/django/django/pull/14497
--
Ticket URL: <https://code.djangoproject.com/ticket/32821#comment:2>
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>
* owner: nobody => Chris Jerdonek
* status: new => assigned
* component: Uncategorized => Testing framework
* easy: 0 => 1
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/32821#comment:4>
Comment (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.
--
Ticket URL: <https://code.djangoproject.com/ticket/32821#comment:5>
* component: Testing framework => Core (Other)
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/32821#comment:6>
Comment (by Chris Jerdonek):
Thanks, Claude.
--
Ticket URL: <https://code.djangoproject.com/ticket/32821#comment:7>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"7272e1963ffdf39c1d4fe225d5425a45dd095d11" 7272e196]:
{{{
#!CommitTicketReference repository=""
revision="7272e1963ffdf39c1d4fe225d5425a45dd095d11"
Fixed #32821 -- Updated os.scandir() uses to use a context manager.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32821#comment:8>