New submission from Serhiy Storchaka <storchaka+cpyt...@gmail.com>:

Path.glob() uses os.scandir() in the following code.

    entries = list(scandir(parent_path))

It properly closes the internal file descriptor opened by scandir() if success 
because it is automatically closed closed when the iterator is exhausted. But 
if it was interrupted (by KeyboardInterrupt, MemoryError or OSError), the file 
descriptor will be closed only when the iterator be collected by the garbage 
collector. It is unreliable on implementations like PyPy and emits a 
ResourceWarning.

The proposed code uses more reliable code

    with scandir(parent_path) as scandir_it:
        entries = list(scandir_it)

which is used in other sites (in the shutil module). I have no idea why I did 
not write it in this form at first place.

----------
components: Library (Lib)
messages: 363750
nosy: serhiy.storchaka
priority: normal
severity: normal
status: open
title: More reliable use of scandir in Path.glob()
type: resource usage
versions: Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39916>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to