#32821: Use a with statement when using os.scandir()
------------------------------------------------+------------------------
               Reporter:  Chris Jerdonek        |          Owner:  nobody
                   Type:  Cleanup/optimization  |         Status:  new
              Component:  Uncategorized         |        Version:  dev
               Severity:  Normal                |       Keywords:
           Triage Stage:  Unreviewed            |      Has patch:  0
    Needs documentation:  0                     |    Needs tests:  0
Patch needs improvement:  0                     |  Easy pickings:  0
                  UI/UX:  0                     |
------------------------------------------------+------------------------
 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.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32821>
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/052.33bbe7d8315b56b54a4fe176f7a3839d%40djangoproject.com.

Reply via email to