Skip to content

Commit b4a1ade

Browse files
0x1306e6dpython-sidebar
authored andcommitted
pythongh-101961 fileinput.hookcompressed should not set the encoding value for the binary mode (pythongh-102068)
1 parent 21d60eb commit b4a1ade

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
@@ -399,7 +399,7 @@ def isstdin(self):
399399

400400

401401
def hook_compressed(filename, mode, *, encoding=None, errors=None):
402-
if encoding is None: # EncodingWarning is emitted in FileInput() already.
402+
if encoding is None and "b" not in mode: # EncodingWarning is emitted in FileInput() already.
403403
encoding = "locale"
404404
ext = os.path.splitext(filename)[1]
405405
if ext == '.gz':

Lib/test/test_fileinput.py

+28-11
Original file line numberDiff line numberDiff line change
@@ -855,29 +855,29 @@ def setUp(self):
855855
self.fake_open = InvocationRecorder()
856856

857857
def test_empty_string(self):
858-
self.do_test_use_builtin_open("", 1)
858+
self.do_test_use_builtin_open_text("", "r")
859859

860860
def test_no_ext(self):
861-
self.do_test_use_builtin_open("abcd", 2)
861+
self.do_test_use_builtin_open_text("abcd", "r")
862862

863863
@unittest.skipUnless(gzip, "Requires gzip and zlib")
864864
def test_gz_ext_fake(self):
865865
original_open = gzip.open
866866
gzip.open = self.fake_open
867867
try:
868-
result = fileinput.hook_compressed("test.gz", "3")
868+
result = fileinput.hook_compressed("test.gz", "r")
869869
finally:
870870
gzip.open = original_open
871871

872872
self.assertEqual(self.fake_open.invocation_count, 1)
873-
self.assertEqual(self.fake_open.last_invocation, (("test.gz", "3"), {}))
873+
self.assertEqual(self.fake_open.last_invocation, (("test.gz", "r"), {}))
874874

875875
@unittest.skipUnless(gzip, "Requires gzip and zlib")
876876
def test_gz_with_encoding_fake(self):
877877
original_open = gzip.open
878878
gzip.open = lambda filename, mode: io.BytesIO(b'Ex-binary string')
879879
try:
880-
result = fileinput.hook_compressed("test.gz", "3", encoding="utf-8")
880+
result = fileinput.hook_compressed("test.gz", "r", encoding="utf-8")
881881
finally:
882882
gzip.open = original_open
883883
self.assertEqual(list(result), ['Ex-binary string'])
@@ -887,23 +887,40 @@ def test_bz2_ext_fake(self):
887887
original_open = bz2.BZ2File
888888
bz2.BZ2File = self.fake_open
889889
try:
890-
result = fileinput.hook_compressed("test.bz2", "4")
890+
result = fileinput.hook_compressed("test.bz2", "r")
891891
finally:
892892
bz2.BZ2File = original_open
893893

894894
self.assertEqual(self.fake_open.invocation_count, 1)
895-
self.assertEqual(self.fake_open.last_invocation, (("test.bz2", "4"), {}))
895+
self.assertEqual(self.fake_open.last_invocation, (("test.bz2", "r"), {}))
896896

897897
def test_blah_ext(self):
898-
self.do_test_use_builtin_open("abcd.blah", "5")
898+
self.do_test_use_builtin_open_binary("abcd.blah", "rb")
899899

900900
def test_gz_ext_builtin(self):
901-
self.do_test_use_builtin_open("abcd.Gz", "6")
901+
self.do_test_use_builtin_open_binary("abcd.Gz", "rb")
902902

903903
def test_bz2_ext_builtin(self):
904-
self.do_test_use_builtin_open("abcd.Bz2", "7")
904+
self.do_test_use_builtin_open_binary("abcd.Bz2", "rb")
905905

906-
def do_test_use_builtin_open(self, filename, mode):
906+
def test_binary_mode_encoding(self):
907+
self.do_test_use_builtin_open_binary("abcd", "rb")
908+
909+
def test_text_mode_encoding(self):
910+
self.do_test_use_builtin_open_text("abcd", "r")
911+
912+
def do_test_use_builtin_open_binary(self, filename, mode):
913+
original_open = self.replace_builtin_open(self.fake_open)
914+
try:
915+
result = fileinput.hook_compressed(filename, mode)
916+
finally:
917+
self.replace_builtin_open(original_open)
918+
919+
self.assertEqual(self.fake_open.invocation_count, 1)
920+
self.assertEqual(self.fake_open.last_invocation,
921+
((filename, mode), {'encoding': None, 'errors': None}))
922+
923+
def do_test_use_builtin_open_text(self, filename, mode):
907924
original_open = self.replace_builtin_open(self.fake_open)
908925
try:
909926
result = fileinput.hook_compressed(filename, mode)

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,7 @@ Tyler Kieft
927927
Mads Kiilerich
928928
Jason Killen
929929
Derek D. Kim
930+
Gihwan Kim
930931
Jan Kim
931932
Taek Joo Kim
932933
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)