Skip to content

Commit ea75f07

Browse files
committed
Fix ResourceWarning caused by unclosed files in FSTestCase.test_files
1 parent 7ed4fb5 commit ea75f07

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

fs/test.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import os
1717
import time
1818
import unittest
19+
import warnings
1920

2021
import fs.copy
2122
import fs.move
@@ -884,8 +885,9 @@ def test_open_files(self):
884885
self.assertFalse(f.closed)
885886
self.assertTrue(f.closed)
886887

887-
iter_lines = iter(self.fs.open("text"))
888-
self.assertEqual(next(iter_lines), "Hello\n")
888+
with self.fs.open("text") as f:
889+
iter_lines = iter(f)
890+
self.assertEqual(next(iter_lines), "Hello\n")
889891

890892
with self.fs.open("unicode", "w") as f:
891893
self.assertEqual(12, f.write("Héllo\nWörld\n"))
@@ -1594,8 +1596,10 @@ def test_files(self):
15941596
self.assert_bytes("foo2", b"help")
15951597

15961598
# Test __del__ doesn't throw traceback
1597-
f = self.fs.open("foo2", "r")
1598-
del f
1599+
with warnings.catch_warnings():
1600+
warnings.simplefilter("ignore")
1601+
f = self.fs.open("foo2", "r")
1602+
del f
15991603

16001604
with self.assertRaises(IOError):
16011605
with self.fs.open("foo2", "r") as f:

0 commit comments

Comments
 (0)