Bug #1586613: fix zlib and bz2 codecs' incremental en/decoders.

This commit is contained in:
Georg Brandl 2006-10-29 14:39:09 +00:00
parent 5b4e1c2530
commit 2c9838e30f
4 changed files with 55 additions and 8 deletions

View File

@ -52,14 +52,35 @@ class Codec(codecs.Codec):
return bz2_decode(input, errors) return bz2_decode(input, errors)
class IncrementalEncoder(codecs.IncrementalEncoder): class IncrementalEncoder(codecs.IncrementalEncoder):
def __init__(self, errors='strict'):
assert errors == 'strict'
self.errors = errors
self.compressobj = bz2.BZ2Compressor()
def encode(self, input, final=False): def encode(self, input, final=False):
assert self.errors == 'strict' if final:
return bz2.compress(input) c = self.compressobj.compress(input)
return c + self.compressobj.flush()
else:
return self.compressobj.compress(input)
def reset(self):
self.compressobj = bz2.BZ2Compressor()
class IncrementalDecoder(codecs.IncrementalDecoder): class IncrementalDecoder(codecs.IncrementalDecoder):
def __init__(self, errors='strict'):
assert errors == 'strict'
self.errors = errors
self.decompressobj = bz2.BZ2Decompressor()
def decode(self, input, final=False): def decode(self, input, final=False):
assert self.errors == 'strict' try:
return bz2.decompress(input) return self.decompressobj.decompress(input)
except EOFError:
return ''
def reset(self):
self.decompressobj = bz2.BZ2Decompressor()
class StreamWriter(Codec,codecs.StreamWriter): class StreamWriter(Codec,codecs.StreamWriter):
pass pass

View File

@ -51,14 +51,36 @@ class Codec(codecs.Codec):
return zlib_decode(input, errors) return zlib_decode(input, errors)
class IncrementalEncoder(codecs.IncrementalEncoder): class IncrementalEncoder(codecs.IncrementalEncoder):
def __init__(self, errors='strict'):
assert errors == 'strict'
self.errors = errors
self.compressobj = zlib.compressobj()
def encode(self, input, final=False): def encode(self, input, final=False):
assert self.errors == 'strict' if final:
return zlib.compress(input) c = self.compressobj.compress(input)
return c + self.compressobj.flush()
else:
return self.compressobj.compress(input)
def reset(self):
self.compressobj = zlib.compressobj()
class IncrementalDecoder(codecs.IncrementalDecoder): class IncrementalDecoder(codecs.IncrementalDecoder):
def __init__(self, errors='strict'):
assert errors == 'strict'
self.errors = errors
self.decompressobj = zlib.decompressobj()
def decode(self, input, final=False): def decode(self, input, final=False):
assert self.errors == 'strict' if final:
return zlib.decompress(input) c = self.decompressobj.decompress(input)
return c + self.decompressobj.flush()
else:
return self.decompressobj.decompress(input)
def reset(self):
self.decompressobj = zlib.decompressobj()
class StreamWriter(Codec,codecs.StreamWriter): class StreamWriter(Codec,codecs.StreamWriter):
pass pass

View File

@ -1062,6 +1062,7 @@ broken_unicode_with_streams = [
"punycode", "punycode",
"unicode_internal" "unicode_internal"
] ]
broken_incremental_coders = broken_unicode_with_streams[:]
try: try:
import bz2 import bz2
@ -1111,6 +1112,7 @@ class BasicUnicodeTest(unittest.TestCase):
decodedresult += reader.read() decodedresult += reader.read()
self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding)) self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding))
if encoding not in broken_incremental_coders:
# check incremental decoder/encoder (fetched via the Python # check incremental decoder/encoder (fetched via the Python
# and C API) and iterencode()/iterdecode() # and C API) and iterencode()/iterdecode()
try: try:

View File

@ -89,6 +89,8 @@ Core and builtins
Library Library
------- -------
- Bug #1586613: fix zlib and bz2 codecs' incremental en/decoders.
- Patch #1583880: fix tarfile's problems with long names and posix/ - Patch #1583880: fix tarfile's problems with long names and posix/
GNU modes. GNU modes.