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