Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(9275)

Unified Diff: Doc/library/os.rst

Issue 25994: File descriptor leaks in os.scandir()
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | Doc/whatsnew/3.6.rst » ('j') | Doc/whatsnew/3.6.rst » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Doc/library/os.rst Mon Feb 08 20:34:49 2016 -0800
+++ b/Doc/library/os.rst Tue Feb 09 20:04:35 2016 +0200
@@ -1891,14 +1891,29 @@ features:
:attr:`~DirEntry.path` attributes of each :class:`DirEntry` will be of
the same type as *path*.
+ The :func:`scandir` iterator supports the :term:`context manager` protocol
+ and has the following method:
+
+ .. method:: scandir.close()
+
+ Close the iterator and free acquired resources.
+
+ This is called automatically when the iterator is exhausted or garbage
+ collected, or when an error happened during iterating. However it
+ is advisable to call it explicitly or use the :keyword:`with`
+ statement.
+
+ .. versionadded:: 3.6
+
The following example shows a simple use of :func:`scandir` to display all
the files (excluding directories) in the given *path* that don't start with
``'.'``. The ``entry.is_file()`` call will generally not make an additional
system call::
- for entry in os.scandir(path):
- if not entry.name.startswith('.') and entry.is_file():
- print(entry.name)
+ with os.scandir(path) as it:
+ for entry in it:
+ if not entry.name.startswith('.') and entry.is_file():
+ print(entry.name)
.. note::
@@ -1914,6 +1929,12 @@ features:
.. versionadded:: 3.5
+ .. versionadded:: 3.6
+ Added support for the :term:`context manager` protocol and the
+ :func:`~scandir.close()` method. If a :func:`scandir` iterator is not
+ exhausted or explicitly closed a :exc:`ResourceWarning` will be emitted
Martin Panter 2016/02/10 03:38:38 not exhausted nor explicitly closed Otherwise it
haypo 2016/02/10 09:57:11 Ah, I was not the only one confused by this senten
storchaka 2016/02/10 10:03:42 Done.
+ in its destructor.
+
.. class:: DirEntry
« no previous file with comments | « no previous file | Doc/whatsnew/3.6.rst » ('j') | Doc/whatsnew/3.6.rst » ('J')

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+