mirror of
https://github.com/python/cpython.git
synced 2025-10-07 07:31:46 +00:00
#1286: allow using fileinput.FileInput as context manager.
This commit is contained in:
parent
e42a59daec
commit
6cb7b6593e
4 changed files with 59 additions and 7 deletions
|
@ -231,6 +231,30 @@ class FileInputTests(unittest.TestCase):
|
|||
## finally:
|
||||
## remove_tempfiles(t1)
|
||||
|
||||
def test_context_manager(self):
|
||||
try:
|
||||
t1 = writeTmp(1, ["A\nB\nC"])
|
||||
t2 = writeTmp(2, ["D\nE\nF"])
|
||||
with FileInput(files=(t1, t2)) as fi:
|
||||
lines = list(fi)
|
||||
self.assertEqual(lines, ["A\n", "B\n", "C", "D\n", "E\n", "F"])
|
||||
self.assertEqual(fi.filelineno(), 3)
|
||||
self.assertEqual(fi.lineno(), 6)
|
||||
self.assertEqual(fi._files, ())
|
||||
finally:
|
||||
remove_tempfiles(t1, t2)
|
||||
|
||||
def test_close_on_exception(self):
|
||||
try:
|
||||
t1 = writeTmp(1, [""])
|
||||
with FileInput(files=t1) as fi:
|
||||
raise IOError
|
||||
except IOError:
|
||||
self.assertEqual(fi._files, ())
|
||||
finally:
|
||||
remove_tempfiles(t1)
|
||||
|
||||
|
||||
def test_main():
|
||||
run_unittest(BufferSizesTests, FileInputTests)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue