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

Unified Diff: Lib/test/test_os.py

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
« Doc/whatsnew/3.6.rst ('K') | « Lib/os.py ('k') | Misc/NEWS » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Lib/test/test_os.py Wed Feb 10 10:31:43 2016 +0200
+++ b/Lib/test/test_os.py Wed Feb 10 11:02:33 2016 +0200
@@ -3030,6 +3030,72 @@ class TestScandir(unittest.TestCase):
for obj in [1234, 1.234, {}, []]:
self.assertRaises(TypeError, os.scandir, obj)
+ def test_close(self):
+ self.create_file("file.txt")
+ self.create_file("file2.txt")
+ iterator = os.scandir(self.path)
+ next(iterator)
+ iterator.close()
+ # multiple closes
+ iterator.close()
+ # check that there is no warnings
+ with warnings.catch_warnings(record=True) as w:
+ warnings.filterwarnings("always", category=ResourceWarning)
+ del iterator
+ support.gc_collect()
+ self.assertEqual(w, [])
+
+ def test_context_manager(self):
+ self.create_file("file.txt")
+ self.create_file("file2.txt")
+ with os.scandir(self.path) as iterator:
+ next(iterator)
+ # check that there is no warnings
+ with warnings.catch_warnings(record=True) as w:
+ warnings.filterwarnings("always", category=ResourceWarning)
+ del iterator
+ support.gc_collect()
+ self.assertEqual(w, [])
+
+ def test_context_manager_close(self):
+ self.create_file("file.txt")
+ self.create_file("file2.txt")
+ with os.scandir(self.path) as iterator:
+ next(iterator)
+ iterator.close()
+
+ def test_context_manager_exception(self):
+ self.create_file("file.txt")
+ self.create_file("file2.txt")
+ with self.assertRaises(ZeroDivisionError):
+ with os.scandir(self.path) as iterator:
+ next(iterator)
+ 1/0
+ # check that there is no warnings
+ with warnings.catch_warnings(record=True) as w:
+ warnings.filterwarnings("always", category=ResourceWarning)
+ del iterator
+ support.gc_collect()
+ self.assertEqual(w, [])
+
+ def test_resource_warning(self):
+ self.create_file("file.txt")
+ self.create_file("file2.txt")
+ iterator = os.scandir(self.path)
+ next(iterator)
+ with self.assertWarns(ResourceWarning):
+ del iterator
+ support.gc_collect()
+ # exhausted iterator
+ iterator = os.scandir(self.path)
+ list(iterator)
+ # check that there is no warnings
+ with warnings.catch_warnings(record=True) as w:
+ warnings.filterwarnings("always", category=ResourceWarning)
+ del iterator
+ support.gc_collect()
+ self.assertEqual(w, [])
haypo 2016/02/10 10:25:12 It looks like the code it duplicated 4 times. Why
storchaka 2016/02/10 10:28:54 Well, now may be worth to use it.
+
if __name__ == "__main__":
unittest.main()
« Doc/whatsnew/3.6.rst ('K') | « Lib/os.py ('k') | Misc/NEWS » ('j') | no next file with comments »

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