diff -r c7f1acdd8be1 Lib/fileinput.py --- a/Lib/fileinput.py Mon Feb 01 12:47:15 2016 +0100 +++ b/Lib/fileinput.py Mon Feb 01 18:56:05 2016 -0500 @@ -412,9 +412,9 @@ return open(filename, mode) -def hook_encoded(encoding): +def hook_encoded(encoding, errors='strict'): def openhook(filename, mode): - return open(filename, mode, encoding=encoding) + return open(filename, mode, encoding=encoding, errors=errors) return openhook diff -r c7f1acdd8be1 Lib/test/test_fileinput.py --- a/Lib/test/test_fileinput.py Mon Feb 01 12:47:15 2016 +0100 +++ b/Lib/test/test_fileinput.py Mon Feb 01 18:56:05 2016 -0500 @@ -873,7 +873,8 @@ def test(self): encoding = object() - result = fileinput.hook_encoded(encoding) + errors = object() + result = fileinput.hook_encoded(encoding, errors=errors) fake_open = InvocationRecorder() original_open = builtins.open @@ -891,8 +892,27 @@ self.assertIs(args[0], filename) self.assertIs(args[1], mode) self.assertIs(kwargs.pop('encoding'), encoding) + self.assertIs(kwargs.pop('errors'), errors) self.assertFalse(kwargs) + def test_errors(self): + with open(TESTFN, 'wb') as f: + # UTF-7 is a convenient, seldom used encoding + f.write(b'\x80abc') + self.addCleanup(safe_unlink, TESTFN) + + def check(errors, expected_lines): + with FileInput(files=TESTFN, mode='r', + openhook=hook_encoded('utf-8', errors=errors)) as fi: + lines = list(fi) + self.assertEqual(lines, expected_lines) + + check('ignore', ['abc']) + with self.assertRaises(UnicodeDecodeError): + check('strict', ['abc']) + check('replace', ['\ufffdabc']) + check('backslashreplace', ['\\x80abc']) + def test_modes(self): with open(TESTFN, 'wb') as f: # UTF-7 is a convenient, seldom used encoding