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

gh-101961 fileinput.hookcompressed should not set the encoding value for the binary mode#102068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
corona10 merged 9 commits intopython:mainfrom0x1306e6d:gh-101961
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletionLib/fileinput.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -399,7 +399,7 @@ def isstdin(self):


def hook_compressed(filename, mode, *, encoding=None, errors=None):
if encoding is None: # EncodingWarning is emitted in FileInput() already.
if encoding is None and "b" not in mode: # EncodingWarning is emitted in FileInput() already.
encoding = "locale"
ext = os.path.splitext(filename)[1]
if ext == '.gz':
Expand Down
39 changes: 28 additions & 11 deletionsLib/test/test_fileinput.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -855,29 +855,29 @@ def setUp(self):
self.fake_open = InvocationRecorder()

def test_empty_string(self):
self.do_test_use_builtin_open("",1)
self.do_test_use_builtin_open_text("","r")

def test_no_ext(self):
self.do_test_use_builtin_open("abcd",2)
self.do_test_use_builtin_open_text("abcd","r")

@unittest.skipUnless(gzip, "Requires gzip and zlib")
def test_gz_ext_fake(self):
original_open = gzip.open
gzip.open = self.fake_open
try:
result = fileinput.hook_compressed("test.gz", "3")
result = fileinput.hook_compressed("test.gz", "r")
finally:
gzip.open = original_open

self.assertEqual(self.fake_open.invocation_count, 1)
self.assertEqual(self.fake_open.last_invocation, (("test.gz", "3"), {}))
self.assertEqual(self.fake_open.last_invocation, (("test.gz", "r"), {}))

@unittest.skipUnless(gzip, "Requires gzip and zlib")
def test_gz_with_encoding_fake(self):
original_open = gzip.open
gzip.open = lambda filename, mode: io.BytesIO(b'Ex-binary string')
try:
result = fileinput.hook_compressed("test.gz", "3", encoding="utf-8")
result = fileinput.hook_compressed("test.gz", "r", encoding="utf-8")
finally:
gzip.open = original_open
self.assertEqual(list(result), ['Ex-binary string'])
Expand All@@ -887,23 +887,40 @@ def test_bz2_ext_fake(self):
original_open = bz2.BZ2File
bz2.BZ2File = self.fake_open
try:
result = fileinput.hook_compressed("test.bz2", "4")
result = fileinput.hook_compressed("test.bz2", "r")
finally:
bz2.BZ2File = original_open

self.assertEqual(self.fake_open.invocation_count, 1)
self.assertEqual(self.fake_open.last_invocation, (("test.bz2", "4"), {}))
self.assertEqual(self.fake_open.last_invocation, (("test.bz2", "r"), {}))

def test_blah_ext(self):
self.do_test_use_builtin_open("abcd.blah", "5")
self.do_test_use_builtin_open_binary("abcd.blah", "rb")

def test_gz_ext_builtin(self):
self.do_test_use_builtin_open("abcd.Gz", "6")
self.do_test_use_builtin_open_binary("abcd.Gz", "rb")

def test_bz2_ext_builtin(self):
self.do_test_use_builtin_open("abcd.Bz2", "7")
self.do_test_use_builtin_open_binary("abcd.Bz2", "rb")

def do_test_use_builtin_open(self, filename, mode):
def test_binary_mode_encoding(self):
self.do_test_use_builtin_open_binary("abcd", "rb")

def test_text_mode_encoding(self):
self.do_test_use_builtin_open_text("abcd", "r")

def do_test_use_builtin_open_binary(self, filename, mode):
original_open = self.replace_builtin_open(self.fake_open)
try:
result = fileinput.hook_compressed(filename, mode)
finally:
self.replace_builtin_open(original_open)

self.assertEqual(self.fake_open.invocation_count, 1)
self.assertEqual(self.fake_open.last_invocation,
((filename, mode), {'encoding': None, 'errors': None}))

def do_test_use_builtin_open_text(self, filename, mode):
original_open = self.replace_builtin_open(self.fake_open)
try:
result = fileinput.hook_compressed(filename, mode)
Expand Down
1 change: 1 addition & 0 deletionsMisc/ACKS
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -927,6 +927,7 @@ Tyler Kieft
Mads Kiilerich
Jason Killen
Derek D. Kim
Gihwan Kim
Jan Kim
Taek Joo Kim
Sam Kimbrel
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
For the binary mode, :func:`fileinput.hookcompressed` doesn't set the ``encoding`` value
even if the value is ``None``. Patch by Gihwan Kim.

[8]ページ先頭

©2009-2025 Movatter.jp