Serhiy Storchaka <storchaka+cpyt...@gmail.com> added the comment:

Yes, I am the author of the code that uses os.scandir() in os.walk(), 
os.fwalk(), glob.iglob() and Path.glob() (see issue23605, issue25996, 
issue25596, issue26032). And it was always in mind to not keep many file 
descriptors open when traverse directories recursively. It was also a reason of 
rejecting my earlier patch for speeding up os.walk() by using os.fwalk() (see 
issue15200).

It is safe to iterated over os.scandir() without turning it into a list if we 
do not do this recursively.

Unfortunately there were no special tests for this. PR 15956 adds them. You can 
easily break tests for pathlib if remove any of list(). It is harder to break 
tests for glob, because fnmatch.filter() consumes the iterator and implicitly 
closes the scandir iterator. You need to replace it with a generator and 
fnmatch.fnmatch() called in a loop. Breaking the tests for os.walk() is 
difficult. The code of os.walk() is so complex because it needs to return lists 
of files and subdirectories, and therefore it consumes the scandir iterator and 
closes it. But with some tricks it is possible to break tests for os.walk(). It 
is unlikely somebody will do this unintentionally.

----------

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

Reply via email to