[Python-checkins] cpython (merge 3.3 -> default): Added tests for issue #20501.
serhiy.storchaka
python-checkins at python.org
Wed Feb 26 20:05:30 CET 2014
http://hg.python.org/cpython/rev/1a38fa1f701d
changeset: 89405:1a38fa1f701d
parent: 89402:aaef9e79c19a
parent: 89404:b4a139713b3b
user: Serhiy Storchaka <storchaka at gmail.com>
date: Wed Feb 26 21:03:19 2014 +0200
summary:
Added tests for issue #20501.
files:
Lib/test/test_fileinput.py | 38 ++++++++++++++++++++++++++
1 files changed, 38 insertions(+), 0 deletions(-)
diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py
--- a/Lib/test/test_fileinput.py
+++ b/Lib/test/test_fileinput.py
@@ -260,6 +260,24 @@
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 @@
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()
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list