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

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

Issue 25994: File descriptor leaks in os.scandir()
Patch Set: Created 3 years, 7 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
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), [])
haypo 2016/02/09 16:50:33 Hum, the test doesn't fail if the destructor emits
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 @support.cpython_only
3069 def test_resource_warning(self):
3070 self.create_file("file.txt")
3071 self.create_file("file2.txt")
3072 iterator = os.scandir(self.path)
3073 next(iterator)
3074 with self.assertWarns(ResourceWarning):
3075 del iterator
3076 support.gc_collect()
3032 3077
3033 3078
3034 if __name__ == "__main__": 3079 if __name__ == "__main__":
3035 unittest.main() 3080 unittest.main()
OLDNEW

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