[New-bugs-announce] [issue39916] More reliable use of scandir in Path.glob()
Serhiy Storchaka
report at bugs.python.org
Mon Mar 9 12:54:33 EDT 2020
New submission from Serhiy Storchaka <storchaka+cpython at 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 <report at bugs.python.org>
<https://bugs.python.org/issue39916>
_______________________________________
More information about the New-bugs-announce
mailing list