[Python-checkins] r52530 - in python/branches/release25-maint: Lib/encodings/bz2_codec.py Lib/encodings/zlib_codec.py Lib/test/test_codecs.py Misc/NEWS

georg.brandl python-checkins at python.org
Sun Oct 29 15:39:14 CET 2006


Author: georg.brandl
Date: Sun Oct 29 15:39:13 2006
New Revision: 52530
Modified:
 python/branches/release25-maint/Lib/encodings/bz2_codec.py
 python/branches/release25-maint/Lib/encodings/zlib_codec.py
 python/branches/release25-maint/Lib/test/test_codecs.py
 python/branches/release25-maint/Misc/NEWS
Log:
Bug #1586613: fix zlib and bz2 codecs' incremental en/decoders.
 (backport from rev. 52529)
Modified: python/branches/release25-maint/Lib/encodings/bz2_codec.py
==============================================================================
--- python/branches/release25-maint/Lib/encodings/bz2_codec.py	(original)
+++ python/branches/release25-maint/Lib/encodings/bz2_codec.py	Sun Oct 29 15:39:13 2006
@@ -52,14 +52,35 @@
 return bz2_decode(input, errors)
 
 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):
- assert self.errors == 'strict'
- return bz2.compress(input)
+ if final:
+ 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):
+ def __init__(self, errors='strict'):
+ assert errors == 'strict'
+ self.errors = errors
+ self.decompressobj = bz2.BZ2Decompressor()
+
 def decode(self, input, final=False):
- assert self.errors == 'strict'
- return bz2.decompress(input)
+ try:
+ return self.decompressobj.decompress(input)
+ except EOFError:
+ return ''
+
+ def reset(self):
+ self.decompressobj = bz2.BZ2Decompressor()
 
 class StreamWriter(Codec,codecs.StreamWriter):
 pass
Modified: python/branches/release25-maint/Lib/encodings/zlib_codec.py
==============================================================================
--- python/branches/release25-maint/Lib/encodings/zlib_codec.py	(original)
+++ python/branches/release25-maint/Lib/encodings/zlib_codec.py	Sun Oct 29 15:39:13 2006
@@ -51,14 +51,36 @@
 return zlib_decode(input, errors)
 
 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):
- assert self.errors == 'strict'
- return zlib.compress(input)
+ if final:
+ 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):
+ def __init__(self, errors='strict'):
+ assert errors == 'strict'
+ self.errors = errors
+ self.decompressobj = zlib.decompressobj()
+
 def decode(self, input, final=False):
- assert self.errors == 'strict'
- return zlib.decompress(input)
+ if final:
+ 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):
 pass
Modified: python/branches/release25-maint/Lib/test/test_codecs.py
==============================================================================
--- python/branches/release25-maint/Lib/test/test_codecs.py	(original)
+++ python/branches/release25-maint/Lib/test/test_codecs.py	Sun Oct 29 15:39:13 2006
@@ -1063,6 +1063,7 @@
 "punycode",
 "unicode_internal"
 ]
+broken_incremental_coders = broken_unicode_with_streams[:]
 
 try:
 import bz2
@@ -1112,6 +1113,7 @@
 decodedresult += reader.read()
 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
 # and C API) and iterencode()/iterdecode()
 try:
Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Sun Oct 29 15:39:13 2006
@@ -90,6 +90,8 @@
 Library
 -------
 
+- Bug #1586613: fix zlib and bz2 codecs' incremental en/decoders.
+
 - Patch #1583880: fix tarfile's problems with long names and posix/
 GNU modes.
 


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /