From a011e2b2fac5cc63c8214315190a54cc71cbd089 Mon Sep 17 00:00:00 2001 From: Florent Xicluna Date: Mon, 7 Nov 2011 19:43:07 +0100 Subject: [PATCH] fileinput: replace this last occurence of codecs.open with builtins.open. --- Lib/fileinput.py | 3 +-- Lib/test/test_fileinput.py | 28 +++++++++------------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/Lib/fileinput.py b/Lib/fileinput.py index 554beb2448c..dbbbb2192ea 100644 --- a/Lib/fileinput.py +++ b/Lib/fileinput.py @@ -398,9 +398,8 @@ def hook_compressed(filename, mode): def hook_encoded(encoding): - import codecs def openhook(filename, mode): - return codecs.open(filename, mode, encoding) + return open(filename, mode, encoding=encoding) return openhook diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py index a96d48a5619..1e70641150d 100644 --- a/Lib/test/test_fileinput.py +++ b/Lib/test/test_fileinput.py @@ -7,8 +7,7 @@ import sys import re import fileinput import collections -import types -import codecs +import builtins import unittest try: @@ -807,18 +806,8 @@ class Test_hook_compressed(unittest.TestCase): @staticmethod def replace_builtin_open(new_open_func): - builtins_type = type(__builtins__) - if builtins_type is dict: - original_open = __builtins__["open"] - __builtins__["open"] = new_open_func - elif builtins_type is types.ModuleType: - original_open = __builtins__.open - __builtins__.open = new_open_func - else: - raise RuntimeError( - "unknown __builtins__ type: %r (unable to replace open)" % - builtins_type) - + original_open = builtins.open + builtins.open = new_open_func return original_open class Test_hook_encoded(unittest.TestCase): @@ -829,21 +818,22 @@ class Test_hook_encoded(unittest.TestCase): result = fileinput.hook_encoded(encoding) fake_open = InvocationRecorder() - original_open = codecs.open - codecs.open = fake_open + original_open = builtins.open + builtins.open = fake_open try: filename = object() mode = object() open_result = result(filename, mode) finally: - codecs.open = original_open + builtins.open = original_open self.assertEqual(fake_open.invocation_count, 1) - args = fake_open.last_invocation[0] + args, kwargs = fake_open.last_invocation self.assertIs(args[0], filename) self.assertIs(args[1], mode) - self.assertIs(args[2], encoding) + self.assertIs(kwargs.pop('encoding'), encoding) + self.assertFalse(kwargs) def test_main(): run_unittest(