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

Python code coverage for Lib/encodings/utf_32.py

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