all Python 3codecsterm
codecs.IncrementalDecoder
incremental codecs
The base class of incremental decoders: decode(data, final=False) keeps an incomplete tail between calls, while reset() and getstate() manage the state.
Syntax
class MyDecoder(codecs.IncrementalDecoder):
def decode(self, input, final=False): ...
On its own it decodes nothing: the base decode raises NotImplementedError. You get a working decoder from getincrementaldecoder and subclass this only when writing a codec. The same machinery sits inside io.TextIOWrapper, which is why open(..., encoding=...) reads UTF-8 in blocks correctly. getstate() shows the bytes waiting for more input: (b'\xd0', 0) is half a letter.
Examples
import codecs
dec = codecs.getincrementaldecoder('utf-8')()
print(repr(dec.decode(b'\xd0')), dec.getstate()) # → '' (b'\xd0', 0)
print(dec.decode(b'\xb6')) # → ж
print(isinstance(dec, codecs.IncrementalDecoder)) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy