summaryrefslogtreecommitdiffstats
path: root/Lib/codecs.py
diff options
context:
space:
mode:
authorWalter Dörwald <walter@livinglogic.de>2006-04-14 18:25:39 (GMT)
committerWalter Dörwald <walter@livinglogic.de>2006-04-14 18:25:39 (GMT)
commit78a0be6ab373680335e12cb76b3d811afbae32d0 (patch)
tree2d6e525b7c7abd9ba026eea52a07348f3daa8fa5 /Lib/codecs.py
parenta40cf31de67c51bae91a897bd007fa36d6d5daf9 (diff)
downloadcpython-78a0be6ab373680335e12cb76b3d811afbae32d0.zip
cpython-78a0be6ab373680335e12cb76b3d811afbae32d0.tar.gz
cpython-78a0be6ab373680335e12cb76b3d811afbae32d0.tar.bz2
Add a BufferedIncrementalEncoder class that can be used for implementing
an incremental encoder that must retain part of the data between calls to the encode() method. Fix the incremental encoder and decoder for the IDNA encoding. This closes SF patch #1453235.
Diffstat (limited to 'Lib/codecs.py')
-rw-r--r--Lib/codecs.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/Lib/codecs.py b/Lib/codecs.py
index c138187..1518d75 100644
--- a/Lib/codecs.py
+++ b/Lib/codecs.py
@@ -181,6 +181,33 @@ class IncrementalEncoder(object):
Resets the encoder to the initial state.
"""
+class BufferedIncrementalEncoder(IncrementalEncoder):
+ """
+ This subclass of IncrementalEncoder can be used as the baseclass for an
+ incremental encoder if the encoder must keep some of the output in a
+ buffer between calls to encode().
+ """
+ def __init__(self, errors='strict'):
+ IncrementalEncoder.__init__(self, errors)
+ self.buffer = "" # unencoded input that is kept between calls to encode()
+
+ def _buffer_encode(self, input, errors, final):
+ # Overwrite this method in subclasses: It must encode input
+ # and return an (output, length consumed) tuple
+ raise NotImplementedError
+
+ def encode(self, input, final=False):
+ # encode input (taking the buffer into account)
+ data = self.buffer + input
+ (result, consumed) = self._buffer_encode(data, self.errors, final)
+ # keep unencoded input until the next call
+ self.buffer = data[consumed:]
+ return result
+
+ def reset(self):
+ IncrementalEncoder.reset(self)
+ self.buffer = ""
+
class IncrementalDecoder(object):
"""
An IncrementalDecoder decodes an input in multiple steps. The input can be