Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6f25657

Browse files
authored
gh-101961 fileinput.hookcompressed should not set the encoding value for the binary mode (gh-102068)
1 parent022b44f commit6f25657

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

‎Lib/fileinput.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def isstdin(self):
399399

400400

401401
defhook_compressed(filename,mode,*,encoding=None,errors=None):
402-
ifencodingisNone:# EncodingWarning is emitted in FileInput() already.
402+
ifencodingisNoneand"b"notinmode:# EncodingWarning is emitted in FileInput() already.
403403
encoding="locale"
404404
ext=os.path.splitext(filename)[1]
405405
ifext=='.gz':

‎Lib/test/test_fileinput.py‎

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

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

860860
deftest_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
deftest_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
deftest_gz_with_encoding_fake(self):
877877
original_open=gzip.open
878878
gzip.open=lambdafilename,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
deftest_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
deftest_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
deftest_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-
defdo_test_use_builtin_open(self,filename,mode):
906+
deftest_binary_mode_encoding(self):
907+
self.do_test_use_builtin_open_binary("abcd","rb")
908+
909+
deftest_text_mode_encoding(self):
910+
self.do_test_use_builtin_open_text("abcd","r")
911+
912+
defdo_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+
defdo_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‎

Lines changed: 1 addition & 0 deletions
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
Lines changed: 2 additions & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp