77import re
88import fileinput
99import collections
10- import types
11- import codecs
10+ import builtins
1211import unittest
1312
1413try :
@@ -807,18 +806,8 @@ def do_test_use_builtin_open(self, filename, mode):
807806
808807 @staticmethod
809808 def replace_builtin_open (new_open_func ):
810- builtins_type = type (__builtins__ )
811- if builtins_type is dict :
812- original_open = __builtins__ ["open" ]
813- __builtins__ ["open" ] = new_open_func
814- elif builtins_type is types .ModuleType :
815- original_open = __builtins__ .open
816- __builtins__ .open = new_open_func
817- else :
818- raise RuntimeError (
819- "unknown __builtins__ type: %r (unable to replace open)" %
820- builtins_type )
821-
809+ original_open = builtins .open
810+ builtins .open = new_open_func
822811 return original_open
823812
824813class Test_hook_encoded (unittest .TestCase ):
@@ -829,21 +818,22 @@ def test(self):
829818 result = fileinput .hook_encoded (encoding )
830819
831820 fake_open = InvocationRecorder ()
832- original_open = codecs .open
833- codecs .open = fake_open
821+ original_open = builtins .open
822+ builtins .open = fake_open
834823 try :
835824 filename = object ()
836825 mode = object ()
837826 open_result = result (filename , mode )
838827 finally :
839- codecs .open = original_open
828+ builtins .open = original_open
840829
841830 self .assertEqual (fake_open .invocation_count , 1 )
842831
843- args = fake_open .last_invocation [ 0 ]
832+ args , kwargs = fake_open .last_invocation
844833 self .assertIs (args [0 ], filename )
845834 self .assertIs (args [1 ], mode )
846- self .assertIs (args [2 ], encoding )
835+ self .assertIs (kwargs .pop ('encoding' ), encoding )
836+ self .assertFalse (kwargs )
847837
848838def test_main ():
849839 run_unittest (
0 commit comments