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
forked frompython/cpython

Commit5b3a658

Browse files
committed
Fix a regression introduced inpythonGH-101251, causing GzipFile.flush() to
not flush the compressor (nor pass along the zip_mode argument).
1 parent5816199 commit5b3a658

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

‎Lib/gzip.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,9 @@ def close(self):
370370
defflush(self,zlib_mode=zlib.Z_SYNC_FLUSH):
371371
self._check_not_closed()
372372
ifself.mode==WRITE:
373-
# Ensure the compressor's buffer is flushed
374373
self._buffer.flush()
374+
# Ensure the compressor's buffer is flushed
375+
self.fileobj.write(self.compress.flush(zlib_mode))
375376
self.fileobj.flush()
376377

377378
deffileno(self):

‎Lib/test/test_gzip.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
importstruct
1010
importsys
1111
importunittest
12+
importzlib
1213
fromsubprocessimportPIPE,Popen
1314
fromtest.supportimportimport_helper
1415
fromtest.supportimportos_helper
@@ -616,6 +617,54 @@ def test_issue44439(self):
616617
self.assertEqual(f.write(q),LENGTH)
617618
self.assertEqual(f.tell(),LENGTH)
618619

620+
deftest_flush_flushes_compressor(self):
621+
# See issue GH-105808.
622+
b=io.BytesIO()
623+
message=b"important message here."
624+
withgzip.GzipFile(fileobj=b,mode='w')asf:
625+
f.write(message)
626+
f.flush()
627+
partial_data=b.getvalue()
628+
full_data=b.getvalue()
629+
self.assertEqual(gzip.decompress(full_data),message)
630+
# The partial data should contain the gzip header and the complete
631+
# message, but not the end-of-stream markers (so we can't just
632+
# decompress it directly).
633+
withself.assertRaises(EOFError):
634+
gzip.decompress(partial_data)
635+
d=zlib.decompressobj(wbits=-zlib.MAX_WBITS)
636+
f=io.BytesIO(partial_data)
637+
gzip._read_gzip_header(f)
638+
read_message=d.decompress(f.read())
639+
self.assertEqual(read_message,message)
640+
641+
deftest_flush_modes(self):
642+
# Make sure the argument to flush is properly passed to the
643+
# zlib.compressobj; see issue GH-105808.
644+
classFakeCompressor:
645+
def__init__(self):
646+
self.modes= []
647+
defcompress(self,data):
648+
returnb''
649+
defflush(self,mode=-1):
650+
self.modes.append(mode)
651+
returnb''
652+
b=io.BytesIO()
653+
fc=FakeCompressor()
654+
withgzip.GzipFile(fileobj=b,mode='w')asf:
655+
f.compress=fc
656+
f.flush()
657+
f.flush(50)
658+
f.flush(zlib_mode=100)
659+
# The implicit close will also flush the compressor.
660+
expected_modes= [
661+
zlib.Z_SYNC_FLUSH,
662+
50,
663+
100,
664+
-1,
665+
]
666+
self.assertEqual(fc.modes,expected_modes)
667+
619668

620669
classTestOpen(BaseTest):
621670
deftest_binary_modes(self):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp