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

Side by Side Diff: Lib/test/test_os.py

Issue 25994: File descriptor leaks in os.scandir()
Patch Set: Created 3 years, 8 months ago
Left:
Right:
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 unified diff | Download patch
« Doc/whatsnew/3.6.rst ('K') | « Lib/os.py ('k') | Misc/NEWS » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # As a test suite for the os module, this is woefully inadequate, but this 1 # As a test suite for the os module, this is woefully inadequate, but this
2 # does add tests for a few functions which have been determined to be more 2 # does add tests for a few functions which have been determined to be more
3 # portable than they had been thought to be. 3 # portable than they had been thought to be.
4 4
5 import asynchat 5 import asynchat
6 import asyncore 6 import asyncore
7 import codecs 7 import codecs
8 import contextlib 8 import contextlib
9 import decimal 9 import decimal
10 import errno 10 import errno
(...skipping 3011 matching lines...) Expand 10 before | Expand all | Expand 10 after
3022 entries = list(iterator) 3022 entries = list(iterator)
3023 self.assertEqual(len(entries), 1, entries) 3023 self.assertEqual(len(entries), 1, entries)
3024 3024
3025 # check than consuming the iterator twice doesn't raise exception 3025 # check than consuming the iterator twice doesn't raise exception
3026 entries2 = list(iterator) 3026 entries2 = list(iterator)
3027 self.assertEqual(len(entries2), 0, entries2) 3027 self.assertEqual(len(entries2), 0, entries2)
3028 3028
3029 def test_bad_path_type(self): 3029 def test_bad_path_type(self):
3030 for obj in [1234, 1.234, {}, []]: 3030 for obj in [1234, 1.234, {}, []]:
3031 self.assertRaises(TypeError, os.scandir, obj) 3031 self.assertRaises(TypeError, os.scandir, obj)
3032
3033 def test_close(self):
3034 self.create_file("file.txt")
3035 self.create_file("file2.txt")
3036 iterator = os.scandir(self.path)
3037 next(iterator)
3038 iterator.close()
3039 self.assertEqual(list(iterator), [])
3040 # multiple closes
3041 iterator.close()
3042 iterator.close()
3043
3044 def test_context_manager(self):
3045 self.create_file("file.txt")
3046 self.create_file("file2.txt")
3047 with os.scandir(self.path) as iterator:
3048 next(iterator)
3049 self.assertEqual(list(iterator), [])
3050
3051 def test_context_manager_close(self):
3052 self.create_file("file.txt")
3053 self.create_file("file2.txt")
3054 with os.scandir(self.path) as iterator:
3055 next(iterator)
3056 iterator.close()
3057 self.assertEqual(list(iterator), [])
3058
3059 def test_context_manager_exception(self):
3060 self.create_file("file.txt")
3061 self.create_file("file2.txt")
3062 with self.assertRaises(ZeroDivisionError):
3063 with os.scandir(self.path) as iterator:
3064 next(iterator)
3065 1/0
3066 self.assertEqual(list(iterator), [])
3067
3068 def test_resource_warning(self):
3069 self.create_file("file.txt")
3070 for i in range(100):
3071 self.create_file("file%d.txt" % i)
3072 iterator = os.scandir(self.path)
3073 next(iterator)
3074 with self.assertWarns(ResourceWarning):
3075 del iterator
3076 support.gc_collect()
3077
3078 iterator = os.scandir(self.path)
3079 next(iterator)
3080 iterator.close()
3081 with warnings.catch_warnings(record=True) as w:
3082 warnings.filterwarnings("always", category=ResourceWarning)
3083 del iterator
3084 support.gc_collect()
Martin Panter 2016/02/10 03:38:38 Do you need to assert there are no warnings record
storchaka 2016/02/10 10:03:42 Ah, yes, of course.
3085
3086 iterator = os.scandir(self.path)
3087 with iterator:
3088 next(iterator)
3089 with warnings.catch_warnings(record=True) as w:
3090 warnings.filterwarnings("always", category=ResourceWarning)
3091 del iterator
3092 support.gc_collect()
Martin Panter 2016/02/10 03:38:38 . . . and here?
storchaka 2016/02/10 10:03:42 Done.
3032 3093
3033 3094
3034 if __name__ == "__main__": 3095 if __name__ == "__main__":
3035 unittest.main() 3096 unittest.main()
OLDNEW
« 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+