annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/encodings/utf_32.py @ 69:33d812a61356

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