@@ -945,7 +945,8 @@ class Test_hook_encoded(unittest.TestCase):
945945
946946 def test (self ):
947947 encoding = object ()
948- result = fileinput .hook_encoded (encoding )
948+ errors = object ()
949+ result = fileinput .hook_encoded (encoding , errors = errors )
949950
950951 fake_open = InvocationRecorder ()
951952 original_open = builtins .open
@@ -963,8 +964,26 @@ def test(self):
963964 self .assertIs (args [0 ], filename )
964965 self .assertIs (args [1 ], mode )
965966 self .assertIs (kwargs .pop ('encoding' ), encoding )
967+ self .assertIs (kwargs .pop ('errors' ), errors )
966968 self .assertFalse (kwargs )
967969
970+ def test_errors (self ):
971+ with open (TESTFN , 'wb' ) as f :
972+ f .write (b'\x80 abc' )
973+ self .addCleanup (safe_unlink , TESTFN )
974+
975+ def check (errors , expected_lines ):
976+ with FileInput (files = TESTFN , mode = 'r' ,
977+ openhook = hook_encoded ('utf-8' , errors = errors )) as fi :
978+ lines = list (fi )
979+ self .assertEqual (lines , expected_lines )
980+
981+ check ('ignore' , ['abc' ])
982+ with self .assertRaises (UnicodeDecodeError ):
983+ check ('strict' , ['abc' ])
984+ check ('replace' , ['\ufffd abc' ])
985+ check ('backslashreplace' , ['\\ x80abc' ])
986+
968987 def test_modes (self ):
969988 with open (TESTFN , 'wb' ) as f :
970989 # UTF-7 is a convenient, seldom used encoding
0 commit comments