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

Delta Between Two Patch Sets: Lib/test/test_os.py

Issue 25994: File descriptor leaks in os.scandir()
Left Patch Set: Created 3 years, 7 months ago
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« Doc/whatsnew/3.6.rst ('K') | « Lib/os.py ('k') | Misc/NEWS » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 3018 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 3032
3033 def test_close(self): 3033 def test_close(self):
3034 self.create_file("file.txt") 3034 self.create_file("file.txt")
3035 self.create_file("file2.txt") 3035 self.create_file("file2.txt")
3036 iterator = os.scandir(self.path) 3036 iterator = os.scandir(self.path)
3037 next(iterator) 3037 next(iterator)
3038 iterator.close() 3038 iterator.close()
3039 self.assertEqual(list(iterator), [])
3040 # multiple closes 3039 # multiple closes
3041 iterator.close() 3040 iterator.close()
3042 iterator.close() 3041 # check that there is no warnings
3042 with warnings.catch_warnings(record=True) as w:
3043 warnings.filterwarnings("always", category=ResourceWarning)
3044 del iterator
3045 support.gc_collect()
3046 self.assertEqual(w, [])
3043 3047
3044 def test_context_manager(self): 3048 def test_context_manager(self):
3045 self.create_file("file.txt") 3049 self.create_file("file.txt")
3046 self.create_file("file2.txt") 3050 self.create_file("file2.txt")
3047 with os.scandir(self.path) as iterator: 3051 with os.scandir(self.path) as iterator:
3048 next(iterator) 3052 next(iterator)
3049 self.assertEqual(list(iterator), []) 3053 # check that there is no warnings
3054 with warnings.catch_warnings(record=True) as w:
3055 warnings.filterwarnings("always", category=ResourceWarning)
3056 del iterator
3057 support.gc_collect()
3058 self.assertEqual(w, [])
3050 3059
3051 def test_context_manager_close(self): 3060 def test_context_manager_close(self):
3052 self.create_file("file.txt") 3061 self.create_file("file.txt")
3053 self.create_file("file2.txt") 3062 self.create_file("file2.txt")
3054 with os.scandir(self.path) as iterator: 3063 with os.scandir(self.path) as iterator:
3055 next(iterator) 3064 next(iterator)
3056 iterator.close() 3065 iterator.close()
3057 self.assertEqual(list(iterator), [])
3058 3066
3059 def test_context_manager_exception(self): 3067 def test_context_manager_exception(self):
3060 self.create_file("file.txt") 3068 self.create_file("file.txt")
3061 self.create_file("file2.txt") 3069 self.create_file("file2.txt")
3062 with self.assertRaises(ZeroDivisionError): 3070 with self.assertRaises(ZeroDivisionError):
3063 with os.scandir(self.path) as iterator: 3071 with os.scandir(self.path) as iterator:
3064 next(iterator) 3072 next(iterator)
3065 1/0 3073 1/0
3066 self.assertEqual(list(iterator), []) 3074 # check that there is no warnings
3075 with warnings.catch_warnings(record=True) as w:
3076 warnings.filterwarnings("always", category=ResourceWarning)
3077 del iterator
3078 support.gc_collect()
3079 self.assertEqual(w, [])
3067 3080
3068 def test_resource_warning(self): 3081 def test_resource_warning(self):
3069 self.create_file("file.txt") 3082 self.create_file("file.txt")
3070 for i in range(100): 3083 self.create_file("file2.txt")
3071 self.create_file("file%d.txt" % i)
3072 iterator = os.scandir(self.path) 3084 iterator = os.scandir(self.path)
3073 next(iterator) 3085 next(iterator)
3074 with self.assertWarns(ResourceWarning): 3086 with self.assertWarns(ResourceWarning):
3075 del iterator 3087 del iterator
3076 support.gc_collect() 3088 support.gc_collect()
3077 3089 # exhausted iterator
3078 iterator = os.scandir(self.path) 3090 iterator = os.scandir(self.path)
3079 next(iterator) 3091 list(iterator)
3080 iterator.close() 3092 # check that there is no warnings
3081 with warnings.catch_warnings(record=True) as w: 3093 with warnings.catch_warnings(record=True) as w:
3082 warnings.filterwarnings("always", category=ResourceWarning) 3094 warnings.filterwarnings("always", category=ResourceWarning)
3083 del iterator 3095 del iterator
3084 support.gc_collect() 3096 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 3097 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.
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.
3093 3098
3094 3099
3095 if __name__ == "__main__": 3100 if __name__ == "__main__":
3096 unittest.main() 3101 unittest.main()
LEFTRIGHT

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