annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/encodings/utf_16.py @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 """ Python 'utf-16' Codec
jpayne@68 2
jpayne@68 3
jpayne@68 4 Written by Marc-Andre Lemburg (mal@lemburg.com).
jpayne@68 5
jpayne@68 6 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
jpayne@68 7
jpayne@68 8 """
jpayne@68 9 import codecs, sys
jpayne@68 10
jpayne@68 11 ### Codec APIs
jpayne@68 12
jpayne@68 13 encode = codecs.utf_16_encode
jpayne@68 14
jpayne@68 15 def decode(input, errors='strict'):
jpayne@68 16 return codecs.utf_16_decode(input, errors, True)
jpayne@68 17
jpayne@68 18 class IncrementalEncoder(codecs.IncrementalEncoder):
jpayne@68 19 def __init__(self, errors='strict'):
jpayne@68 20 codecs.IncrementalEncoder.__init__(self, errors)
jpayne@68 21 self.encoder = None
jpayne@68 22
jpayne@68 23 def encode(self, input, final=False):
jpayne@68 24 if self.encoder is None:
jpayne@68 25 result = codecs.utf_16_encode(input, self.errors)[0]
jpayne@68 26 if sys.byteorder == 'little':
jpayne@68 27 self.encoder = codecs.utf_16_le_encode
jpayne@68 28 else:
jpayne@68 29 self.encoder = codecs.utf_16_be_encode
jpayne@68 30 return result
jpayne@68 31 return self.encoder(input, self.errors)[0]
jpayne@68 32
jpayne@68 33 def reset(self):
jpayne@68 34 codecs.IncrementalEncoder.reset(self)
jpayne@68 35 self.encoder = None
jpayne@68 36
jpayne@68 37 def getstate(self):
jpayne@68 38 # state info we return to the caller:
jpayne@68 39 # 0: stream is in natural order for this platform
jpayne@68 40 # 2: endianness hasn't been determined yet
jpayne@68 41 # (we're never writing in unnatural order)
jpayne@68 42 return (2 if self.encoder is None else 0)
jpayne@68 43
jpayne@68 44 def setstate(self, state):
jpayne@68 45 if state:
jpayne@68 46 self.encoder = None
jpayne@68 47 else:
jpayne@68 48 if sys.byteorder == 'little':
jpayne@68 49 self.encoder = codecs.utf_16_le_encode
jpayne@68 50 else:
jpayne@68 51 self.encoder = codecs.utf_16_be_encode
jpayne@68 52
jpayne@68 53 class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
jpayne@68 54 def __init__(self, errors='strict'):
jpayne@68 55 codecs.BufferedIncrementalDecoder.__init__(self, errors)
jpayne@68 56 self.decoder = None
jpayne@68 57
jpayne@68 58 def _buffer_decode(self, input, errors, final):
jpayne@68 59 if self.decoder is None:
jpayne@68 60 (output, consumed, byteorder) = \
jpayne@68 61 codecs.utf_16_ex_decode(input, errors, 0, final)
jpayne@68 62 if byteorder == -1:
jpayne@68 63 self.decoder = codecs.utf_16_le_decode
jpayne@68 64 elif byteorder == 1:
jpayne@68 65 self.decoder = codecs.utf_16_be_decode
jpayne@68 66 elif consumed >= 2:
jpayne@68 67 raise UnicodeError("UTF-16 stream does not start with BOM")
jpayne@68 68 return (output, consumed)
jpayne@68 69 return self.decoder(input, self.errors, final)
jpayne@68 70
jpayne@68 71 def reset(self):
jpayne@68 72 codecs.BufferedIncrementalDecoder.reset(self)
jpayne@68 73 self.decoder = None
jpayne@68 74
jpayne@68 75 def getstate(self):
jpayne@68 76 # additional state info from the base class must be None here,
jpayne@68 77 # as it isn't passed along to the caller
jpayne@68 78 state = codecs.BufferedIncrementalDecoder.getstate(self)[0]
jpayne@68 79 # additional state info we pass to the caller:
jpayne@68 80 # 0: stream is in natural order for this platform
jpayne@68 81 # 1: stream is in unnatural order
jpayne@68 82 # 2: endianness hasn't been determined yet
jpayne@68 83 if self.decoder is None:
jpayne@68 84 return (state, 2)
jpayne@68 85 addstate = int((sys.byteorder == "big") !=
jpayne@68 86 (self.decoder is codecs.utf_16_be_decode))
jpayne@68 87 return (state, addstate)
jpayne@68 88
jpayne@68 89 def setstate(self, state):
jpayne@68 90 # state[1] will be ignored by BufferedIncrementalDecoder.setstate()
jpayne@68 91 codecs.BufferedIncrementalDecoder.setstate(self, state)
jpayne@68 92 state = state[1]
jpayne@68 93 if state == 0:
jpayne@68 94 self.decoder = (codecs.utf_16_be_decode
jpayne@68 95 if sys.byteorder == "big"
jpayne@68 96 else codecs.utf_16_le_decode)
jpayne@68 97 elif state == 1:
jpayne@68 98 self.decoder = (codecs.utf_16_le_decode
jpayne@68 99 if sys.byteorder == "big"
jpayne@68 100 else codecs.utf_16_be_decode)
jpayne@68 101 else:
jpayne@68 102 self.decoder = None
jpayne@68 103
jpayne@68 104 class StreamWriter(codecs.StreamWriter):
jpayne@68 105 def __init__(self, stream, errors='strict'):
jpayne@68 106 codecs.StreamWriter.__init__(self, stream, errors)
jpayne@68 107 self.encoder = None
jpayne@68 108
jpayne@68 109 def reset(self):
jpayne@68 110 codecs.StreamWriter.reset(self)
jpayne@68 111 self.encoder = None
jpayne@68 112
jpayne@68 113 def encode(self, input, errors='strict'):
jpayne@68 114 if self.encoder is None:
jpayne@68 115 result = codecs.utf_16_encode(input, errors)
jpayne@68 116 if sys.byteorder == 'little':
jpayne@68 117 self.encoder = codecs.utf_16_le_encode
jpayne@68 118 else:
jpayne@68 119 self.encoder = codecs.utf_16_be_encode
jpayne@68 120 return result
jpayne@68 121 else:
jpayne@68 122 return self.encoder(input, errors)
jpayne@68 123
jpayne@68 124 class StreamReader(codecs.StreamReader):
jpayne@68 125
jpayne@68 126 def reset(self):
jpayne@68 127 codecs.StreamReader.reset(self)
jpayne@68 128 try:
jpayne@68 129 del self.decode
jpayne@68 130 except AttributeError:
jpayne@68 131 pass
jpayne@68 132
jpayne@68 133 def decode(self, input, errors='strict'):
jpayne@68 134 (object, consumed, byteorder) = \
jpayne@68 135 codecs.utf_16_ex_decode(input, errors, 0, False)
jpayne@68 136 if byteorder == -1:
jpayne@68 137 self.decode = codecs.utf_16_le_decode
jpayne@68 138 elif byteorder == 1:
jpayne@68 139 self.decode = codecs.utf_16_be_decode
jpayne@68 140 elif consumed>=2:
jpayne@68 141 raise UnicodeError("UTF-16 stream does not start with BOM")
jpayne@68 142 return (object, consumed)
jpayne@68 143
jpayne@68 144 ### encodings module API
jpayne@68 145
jpayne@68 146 def getregentry():
jpayne@68 147 return codecs.CodecInfo(
jpayne@68 148 name='utf-16',
jpayne@68 149 encode=encode,
jpayne@68 150 decode=decode,
jpayne@68 151 incrementalencoder=IncrementalEncoder,
jpayne@68 152 incrementaldecoder=IncrementalDecoder,
jpayne@68 153 streamreader=StreamReader,
jpayne@68 154 streamwriter=StreamWriter,
jpayne@68 155 )