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

Unified Diff: Doc/library/os.rst

Issue 25994: File descriptor leaks in os.scandir()
Patch Set: Created 3 years, 7 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 Wed Feb 10 10:31:43 2016 +0200
+++ b/Doc/library/os.rst Wed Feb 10 11:02:33 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
Jim.J.Jewett 2016/02/10 23:20:41 happened -> happens
storchaka 2016/02/11 12:20:36 Done.
+ 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
Jim.J.Jewett 2016/02/10 23:20:41 "is not ... nor" -> "is neither ... nor"
Martin Panter 2016/02/11 02:22:20 Yes, even better. (This started off as “is not . .
storchaka 2016/02/11 12:20:36 Done.
+ exhausted nor explicitly closed a :exc:`ResourceWarning` will be emitted
+ 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+