summaryrefslogtreecommitdiff
path: root/Lib/test/test_fileinput.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-02-26 21:03:19 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2014-02-26 21:03:19 +0200
commit9fff849dbfe31642fac2e58d42e0ef73891f5c04 (patch)
tree28040c1a044bf1de23b335b319c44b2a0cb7accd /Lib/test/test_fileinput.py
parentf8a5892140e90a1337a639d62cc5f7662d9e5f84 (diff)
parent517b74734af130d19a7877b051ba026c76f381b2 (diff)
downloadcpython-git-9fff849dbfe31642fac2e58d42e0ef73891f5c04.tar.gz
Added tests for issue #20501.
Diffstat (limited to 'Lib/test/test_fileinput.py')
-rw-r--r--Lib/test/test_fileinput.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py
index db6082cb12..a537bc8c7c 100644
--- a/Lib/test/test_fileinput.py
+++ b/Lib/test/test_fileinput.py
@@ -260,6 +260,24 @@ class FileInputTests(unittest.TestCase):
fi.readline()
self.assertTrue(custom_open_hook.invoked, "openhook not invoked")
+ def test_readline(self):
+ with open(TESTFN, 'wb') as f:
+ f.write(b'A\nB\r\nC\r')
+ # Fill TextIOWrapper buffer.
+ f.write(b'123456789\n' * 1000)
+ # Issue #20501: readline() shouldn't read whole file.
+ f.write(b'\x80')
+ self.addCleanup(safe_unlink, TESTFN)
+
+ with FileInput(files=TESTFN,
+ openhook=hook_encoded('ascii'), bufsize=8) as fi:
+ self.assertEqual(fi.readline(), 'A\n')
+ self.assertEqual(fi.readline(), 'B\n')
+ self.assertEqual(fi.readline(), 'C\n')
+ with self.assertRaises(UnicodeDecodeError):
+ # Read to the end of file.
+ list(fi)
+
def test_context_manager(self):
try:
t1 = writeTmp(1, ["A\nB\nC"])
@@ -837,6 +855,26 @@ class Test_hook_encoded(unittest.TestCase):
self.assertIs(kwargs.pop('encoding'), encoding)
self.assertFalse(kwargs)
+ def test_modes(self):
+ # Unlikely UTF-7 is locale encoding
+ with open(TESTFN, 'wb') as f:
+ f.write(b'A\nB\r\nC\rD+IKw-')
+ self.addCleanup(safe_unlink, TESTFN)
+
+ def check(mode, expected_lines):
+ with FileInput(files=TESTFN, mode=mode,
+ openhook=hook_encoded('utf-7')) as fi:
+ lines = list(fi)
+ self.assertEqual(lines, expected_lines)
+
+ check('r', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
+ with self.assertWarns(DeprecationWarning):
+ check('rU', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
+ with self.assertWarns(DeprecationWarning):
+ check('U', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
+ with self.assertRaises(ValueError):
+ check('rb', ['A\n', 'B\r\n', 'C\r', 'D\u20ac'])
+
if __name__ == "__main__":
unittest.main()