diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-02-11 11:21:30 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-02-11 11:21:30 (GMT) |
commit | ffe96ae10be8a3117fa18c35034fcfc45c3cf7b7 (patch) | |
tree | 8bda90834ab525f75a46f6dc26ea9adba141ffe7 /Lib/test | |
parent | 2feb64258558fc280f1d87bec211d83def48a9f0 (diff) | |
download | cpython-ffe96ae10be8a3117fa18c35034fcfc45c3cf7b7.zip cpython-ffe96ae10be8a3117fa18c35034fcfc45c3cf7b7.tar.gz cpython-ffe96ae10be8a3117fa18c35034fcfc45c3cf7b7.tar.bz2 |
Issue #25994: Added the close() method and the support of the context manager
protocol for the os.scandir() iterator.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_os.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 66a426a..07682f2 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -2808,6 +2808,8 @@ class ExportsTests(unittest.TestCase): class TestScandir(unittest.TestCase): + check_no_resource_warning = support.check_no_resource_warning + def setUp(self): self.path = os.path.realpath(support.TESTFN) self.addCleanup(support.rmtree, self.path) @@ -3030,6 +3032,56 @@ 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() + with self.check_no_resource_warning(): + del iterator + + def test_context_manager(self): + self.create_file("file.txt") + self.create_file("file2.txt") + with os.scandir(self.path) as iterator: + next(iterator) + with self.check_no_resource_warning(): + del iterator + + 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 + with self.check_no_resource_warning(): + del iterator + + 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) + with self.check_no_resource_warning(): + del iterator + if __name__ == "__main__": unittest.main() |