Skip to content

Commit d9dce23

Browse files
miss-islingtonGihwan Kim
and
Gihwan Kim
authored
[3.10] gh-101961 fileinput.hookcompressed should not set the encoding value for the binary mode (gh-102068) (#102099)
gh-101961 fileinput.hookcompressed should not set the encoding value for the binary mode (gh-102068) (cherry picked from commit 6f25657) Co-authored-by: Gihwan Kim <[email protected]>
1 parent 7bb41d9 commit d9dce23

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

Lib/fileinput.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def isstdin(self):
419419

420420

421421
def hook_compressed(filename, mode, *, encoding=None, errors=None):
422-
if encoding is None: # EncodingWarning is emitted in FileInput() already.
422+
if encoding is None and "b" not in mode: # EncodingWarning is emitted in FileInput() already.
423423
encoding = "locale"
424424
ext = os.path.splitext(filename)[1]
425425
if ext == '.gz':

Lib/test/test_fileinput.py

+28-11
Original file line numberDiff line numberDiff line change
@@ -903,29 +903,29 @@ def setUp(self):
903903
self.fake_open = InvocationRecorder()
904904

905905
def test_empty_string(self):
906-
self.do_test_use_builtin_open("", 1)
906+
self.do_test_use_builtin_open_text("", "r")
907907

908908
def test_no_ext(self):
909-
self.do_test_use_builtin_open("abcd", 2)
909+
self.do_test_use_builtin_open_text("abcd", "r")
910910

911911
@unittest.skipUnless(gzip, "Requires gzip and zlib")
912912
def test_gz_ext_fake(self):
913913
original_open = gzip.open
914914
gzip.open = self.fake_open
915915
try:
916-
result = fileinput.hook_compressed("test.gz", "3")
916+
result = fileinput.hook_compressed("test.gz", "r")
917917
finally:
918918
gzip.open = original_open
919919

920920
self.assertEqual(self.fake_open.invocation_count, 1)
921-
self.assertEqual(self.fake_open.last_invocation, (("test.gz", "3"), {}))
921+
self.assertEqual(self.fake_open.last_invocation, (("test.gz", "r"), {}))
922922

923923
@unittest.skipUnless(gzip, "Requires gzip and zlib")
924924
def test_gz_with_encoding_fake(self):
925925
original_open = gzip.open
926926
gzip.open = lambda filename, mode: io.BytesIO(b'Ex-binary string')
927927
try:
928-
result = fileinput.hook_compressed("test.gz", "3", encoding="utf-8")
928+
result = fileinput.hook_compressed("test.gz", "r", encoding="utf-8")
929929
finally:
930930
gzip.open = original_open
931931
self.assertEqual(list(result), ['Ex-binary string'])
@@ -935,23 +935,40 @@ def test_bz2_ext_fake(self):
935935
original_open = bz2.BZ2File
936936
bz2.BZ2File = self.fake_open
937937
try:
938-
result = fileinput.hook_compressed("test.bz2", "4")
938+
result = fileinput.hook_compressed("test.bz2", "r")
939939
finally:
940940
bz2.BZ2File = original_open
941941

942942
self.assertEqual(self.fake_open.invocation_count, 1)
943-
self.assertEqual(self.fake_open.last_invocation, (("test.bz2", "4"), {}))
943+
self.assertEqual(self.fake_open.last_invocation, (("test.bz2", "r"), {}))
944944

945945
def test_blah_ext(self):
946-
self.do_test_use_builtin_open("abcd.blah", "5")
946+
self.do_test_use_builtin_open_binary("abcd.blah", "rb")
947947

948948
def test_gz_ext_builtin(self):
949-
self.do_test_use_builtin_open("abcd.Gz", "6")
949+
self.do_test_use_builtin_open_binary("abcd.Gz", "rb")
950950

951951
def test_bz2_ext_builtin(self):
952-
self.do_test_use_builtin_open("abcd.Bz2", "7")
952+
self.do_test_use_builtin_open_binary("abcd.Bz2", "rb")
953953

954-
def do_test_use_builtin_open(self, filename, mode):
954+
def test_binary_mode_encoding(self):
955+
self.do_test_use_builtin_open_binary("abcd", "rb")
956+
957+
def test_text_mode_encoding(self):
958+
self.do_test_use_builtin_open_text("abcd", "r")
959+
960+
def do_test_use_builtin_open_binary(self, filename, mode):
961+
original_open = self.replace_builtin_open(self.fake_open)
962+
try:
963+
result = fileinput.hook_compressed(filename, mode)
964+
finally:
965+
self.replace_builtin_open(original_open)
966+
967+
self.assertEqual(self.fake_open.invocation_count, 1)
968+
self.assertEqual(self.fake_open.last_invocation,
969+
((filename, mode), {'encoding': None, 'errors': None}))
970+
971+
def do_test_use_builtin_open_text(self, filename, mode):
955972
original_open = self.replace_builtin_open(self.fake_open)
956973
try:
957974
result = fileinput.hook_compressed(filename, mode)

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ Tyler Kieft
912912
Mads Kiilerich
913913
Jason Killen
914914
Derek D. Kim
915+
Gihwan Kim
915916
Jan Kim
916917
Taek Joo Kim
917918
Sam Kimbrel
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
For the binary mode, :func:`fileinput.hookcompressed` doesn't set the ``encoding`` value
2+
even if the value is ``None``. Patch by Gihwan Kim.

0 commit comments

Comments
 (0)