ยปCore Development>Code coverage>Lib/encodings/utf_16.py

Python code coverage for Lib/encodings/utf_16.py

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