mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
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.
This commit is contained in:
parent
a40cf31de6
commit
78a0be6ab3
3 changed files with 169 additions and 9 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue