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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zlib.error: Error -2 while flushing: inconsistent stream state #74203
Comments
I had some statements inside a I refactored those common statements into a function and called that function zlib.error: Error -2 while flushing: inconsistent stream state I can't figure out why it matters whether the writing happens in the -------------------------------------------------------------------------------- import io, pprint, zipfile
from zipfile import ZIP_DEFLATED
def printLiteral( data, out ) :
encoder = io.TextIOWrapper( out, encoding='utf-8', write_through=True )
pprint.pprint( data, stream=encoder )
data = { 'not' : 'much', 'just' : 'some K \N{RIGHTWARDS WHITE ARROW} V pairs' }
with zipfile.ZipFile( 'zzz.zip', mode='w', compression=ZIP_DEFLATED ) as myzip :
with myzip.open( 'this one works', 'w' ) as out :
encoder = io.TextIOWrapper( out, encoding='utf-8', write_through=True )
pprint.pprint( data, stream=encoder )
with myzip.open( 'this one fails', 'w' ) as out :
printLiteral( data, out )
print( 'printed but entry still open' )
print( 'entry has been closed but not file' )
print( 'zip file has been closed' ) And here's the output on my Arch Linux 64bit with package -------------------------------------------------------------------------------- printed but entry still open
Traceback (most recent call last):
File "zzz.py", line 21, in <module>
print( 'printed but entry still open' )
File "/usr/lib/python3.6/zipfile.py", line 995, in close
buf = self._compressor.flush()
zlib.error: Error -2 while flushing: inconsistent stream state I tried debugging this in PyDev but got lost. Turning off the compression makes |
At a guess, when encoder goes out of scope, it closes the underlying file object. Then, on exiting the with block, the zipfile tries to take some action with the closed file and errors out? |
It looks like the zip entry writer object may not expect its “close” method to be called multiple times. Other file objects tend to allow this with no ill effect. Adding Serhiy and Thomas who implemented the writer object in bpo-26039. The first time it is called will be when the context manager (“with” statement) exits. The second time will be when the TextIOWrapper is garbage-collected. This is documented behaviour <https://docs.python.org/3.6/library/io.html#io.IOBase.\_\_del__\>, but I think it is a design mistake of the IOBase hierarchy; see bpo-19829. A reasonable workaround would be to call encoder.detach() after you have finished writing the zip entry. |
I agree, that close() should be an idempotent operation. Proposed patch fixes this. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: