diff options
author | Walter Dörwald <walter@livinglogic.de> | 2007-04-16 22:10:50 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2007-04-16 22:10:50 (GMT) |
commit | 3abcb013b8195aea38f80968d4111b5ac7e68c0b (patch) | |
tree | dffc08076dd91cbb7860e8115685b939d067a27f /Lib/encodings/utf_16.py | |
parent | 8981ad05c0f9c0edc8c2c7aeaad3615805abe907 (diff) | |
download | cpython-3abcb013b8195aea38f80968d4111b5ac7e68c0b.zip cpython-3abcb013b8195aea38f80968d4111b5ac7e68c0b.tar.gz cpython-3abcb013b8195aea38f80968d4111b5ac7e68c0b.tar.bz2 |
Apply SF patch #1698994: Add getstate() and setstate()
methods to incrementalcodecs.
Also forward port r54786 (fix the incremental
utf_8_sig decoder).
Diffstat (limited to 'Lib/encodings/utf_16.py')
-rw-r--r-- | Lib/encodings/utf_16.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/Lib/encodings/utf_16.py b/Lib/encodings/utf_16.py index eff08f3..cf096b5 100644 --- a/Lib/encodings/utf_16.py +++ b/Lib/encodings/utf_16.py @@ -34,6 +34,22 @@ class IncrementalEncoder(codecs.IncrementalEncoder): codecs.IncrementalEncoder.reset(self) self.encoder = None + def getstate(self): + # state info we return to the caller: + # 0: stream is in natural order for this platform + # 2: endianness hasn't been determined yet + # (we're never writing in unnatural order) + return (2 if self.encoder is None else 0) + + def setstate(self, state): + if state: + self.encoder = None + else: + if sys.byteorder == 'little': + self.encoder = codecs.utf_16_le_encode + else: + self.encoder = codecs.utf_16_be_encode + class IncrementalDecoder(codecs.BufferedIncrementalDecoder): def __init__(self, errors='strict'): codecs.BufferedIncrementalDecoder.__init__(self, errors) @@ -56,6 +72,35 @@ class IncrementalDecoder(codecs.BufferedIncrementalDecoder): codecs.BufferedIncrementalDecoder.reset(self) self.decoder = None + def getstate(self): + # additonal state info from the base class must be None here, + # as it isn't passed along to the caller + state = codecs.BufferedIncrementalDecoder.getstate(self)[0] + # additional state info we pass to the caller: + # 0: stream is in natural order for this platform + # 1: stream is in unnatural order + # 2: endianness hasn't been determined yet + if self.decoder is None: + return (state, 2) + addstate = int((sys.byteorder == "big") != + (self.decoder is codecs.utf_16_be_decode)) + return (state, addstate) + + def setstate(self, state): + # state[1] will be ignored by BufferedIncrementalDecoder.setstate() + codecs.BufferedIncrementalDecoder.setstate(self, state) + state = state[1] + if state == 0: + self.decoder = (codecs.utf_16_be_decode + if sys.byteorder == "big" + else codecs.utf_16_le_decode) + elif state == 1: + self.decoder = (codecs.utf_16_le_decode + if sys.byteorder == "big" + else codecs.utf_16_be_decode) + else: + self.decoder = None + class StreamWriter(codecs.StreamWriter): def __init__(self, stream, errors='strict'): self.bom_written = False |