22Tests for fileinput module.
33Nick Mathewson
44'''
5+ import io
56import os
67import sys
78import re
@@ -238,7 +239,7 @@ def test_opening_mode(self):
238239 # try opening in universal newline mode
239240 t1 = self .writeTmp (b"A\n B\r \n C\r D" , mode = "wb" )
240241 with warnings_helper .check_warnings (('' , DeprecationWarning )):
241- fi = FileInput (files = t1 , mode = "U" )
242+ fi = FileInput (files = t1 , mode = "U" , encoding = "utf-8" )
242243 with warnings_helper .check_warnings (('' , DeprecationWarning )):
243244 lines = list (fi )
244245 self .assertEqual (lines , ["A\n " , "B\n " , "C\n " , "D" ])
@@ -278,7 +279,7 @@ def test_file_opening_hook(self):
278279 class CustomOpenHook :
279280 def __init__ (self ):
280281 self .invoked = False
281- def __call__ (self , * args ):
282+ def __call__ (self , * args , ** kargs ):
282283 self .invoked = True
283284 return open (* args )
284285
@@ -334,6 +335,14 @@ def test_inplace_binary_write_mode(self):
334335 with open (temp_file , 'rb' ) as f :
335336 self .assertEqual (f .read (), b'New line.' )
336337
338+ def test_file_hook_backward_compatibility (self ):
339+ def old_hook (filename , mode ):
340+ return io .StringIO ("I used to receive only filename and mode" )
341+ t = self .writeTmp ("\n " )
342+ with FileInput ([t ], openhook = old_hook ) as fi :
343+ result = fi .readline ()
344+ self .assertEqual (result , "I used to receive only filename and mode" )
345+
337346 def test_context_manager (self ):
338347 t1 = self .writeTmp ("A\n B\n C" )
339348 t2 = self .writeTmp ("D\n E\n F" )
@@ -529,12 +538,14 @@ class MockFileInput:
529538 """A class that mocks out fileinput.FileInput for use during unit tests"""
530539
531540 def __init__ (self , files = None , inplace = False , backup = "" , * ,
532- mode = "r" , openhook = None ):
541+ mode = "r" , openhook = None , encoding = None , errors = None ):
533542 self .files = files
534543 self .inplace = inplace
535544 self .backup = backup
536545 self .mode = mode
537546 self .openhook = openhook
547+ self .encoding = encoding
548+ self .errors = errors
538549 self ._file = None
539550 self .invocation_counts = collections .defaultdict (lambda : 0 )
540551 self .return_values = {}
@@ -637,10 +648,11 @@ def do_test_call_input(self):
637648 backup = object ()
638649 mode = object ()
639650 openhook = object ()
651+ encoding = object ()
640652
641653 # call fileinput.input() with different values for each argument
642654 result = fileinput .input (files = files , inplace = inplace , backup = backup ,
643- mode = mode , openhook = openhook )
655+ mode = mode , openhook = openhook , encoding = encoding )
644656
645657 # ensure fileinput._state was set to the returned object
646658 self .assertIs (result , fileinput ._state , "fileinput._state" )
@@ -863,11 +875,15 @@ def test_state_is_not_None(self):
863875 self .assertIs (fileinput ._state , instance )
864876
865877class InvocationRecorder :
878+
866879 def __init__ (self ):
867880 self .invocation_count = 0
881+
868882 def __call__ (self , * args , ** kwargs ):
869883 self .invocation_count += 1
870884 self .last_invocation = (args , kwargs )
885+ return io .BytesIO (b'some bytes' )
886+
871887
872888class Test_hook_compressed (unittest .TestCase ):
873889 """Unit tests for fileinput.hook_compressed()"""
@@ -886,33 +902,43 @@ def test_gz_ext_fake(self):
886902 original_open = gzip .open
887903 gzip .open = self .fake_open
888904 try :
889- result = fileinput .hook_compressed ("test.gz" , 3 )
905+ result = fileinput .hook_compressed ("test.gz" , "3" )
890906 finally :
891907 gzip .open = original_open
892908
893909 self .assertEqual (self .fake_open .invocation_count , 1 )
894- self .assertEqual (self .fake_open .last_invocation , (("test.gz" , 3 ), {}))
910+ self .assertEqual (self .fake_open .last_invocation , (("test.gz" , "3" ), {}))
911+
912+ @unittest .skipUnless (gzip , "Requires gzip and zlib" )
913+ def test_gz_with_encoding_fake (self ):
914+ original_open = gzip .open
915+ gzip .open = lambda filename , mode : io .BytesIO (b'Ex-binary string' )
916+ try :
917+ result = fileinput .hook_compressed ("test.gz" , "3" , encoding = "utf-8" )
918+ finally :
919+ gzip .open = original_open
920+ self .assertEqual (list (result ), ['Ex-binary string' ])
895921
896922 @unittest .skipUnless (bz2 , "Requires bz2" )
897923 def test_bz2_ext_fake (self ):
898924 original_open = bz2 .BZ2File
899925 bz2 .BZ2File = self .fake_open
900926 try :
901- result = fileinput .hook_compressed ("test.bz2" , 4 )
927+ result = fileinput .hook_compressed ("test.bz2" , "4" )
902928 finally :
903929 bz2 .BZ2File = original_open
904930
905931 self .assertEqual (self .fake_open .invocation_count , 1 )
906- self .assertEqual (self .fake_open .last_invocation , (("test.bz2" , 4 ), {}))
932+ self .assertEqual (self .fake_open .last_invocation , (("test.bz2" , "4" ), {}))
907933
908934 def test_blah_ext (self ):
909- self .do_test_use_builtin_open ("abcd.blah" , 5 )
935+ self .do_test_use_builtin_open ("abcd.blah" , "5" )
910936
911937 def test_gz_ext_builtin (self ):
912- self .do_test_use_builtin_open ("abcd.Gz" , 6 )
938+ self .do_test_use_builtin_open ("abcd.Gz" , "6" )
913939
914940 def test_bz2_ext_builtin (self ):
915- self .do_test_use_builtin_open ("abcd.Bz2" , 7 )
941+ self .do_test_use_builtin_open ("abcd.Bz2" , "7" )
916942
917943 def do_test_use_builtin_open (self , filename , mode ):
918944 original_open = self .replace_builtin_open (self .fake_open )
@@ -923,7 +949,7 @@ def do_test_use_builtin_open(self, filename, mode):
923949
924950 self .assertEqual (self .fake_open .invocation_count , 1 )
925951 self .assertEqual (self .fake_open .last_invocation ,
926- ((filename , mode ), {}))
952+ ((filename , mode ), {'encoding' : 'locale' , 'errors' : None }))
927953
928954 @staticmethod
929955 def replace_builtin_open (new_open_func ):
0 commit comments