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