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

Python code coverage for Lib/encodings/utf_8_sig.py

#countcontent
1n/a""" Python 'utf-8-sig' Codec
2n/aThis work similar to UTF-8 with the following changes:
3n/a
4n/a* On encoding/writing a UTF-8 encoded BOM will be prepended/written as the
5n/a first three bytes.
6n/a
7n/a* On decoding/reading if the first three bytes are a UTF-8 encoded BOM, these
8n/a bytes will be skipped.
9n/a"""
10n/aimport codecs
11n/a
12n/a### Codec APIs
13n/a
14n/adef encode(input, errors='strict'):
15n/a return (codecs.BOM_UTF8 + codecs.utf_8_encode(input, errors)[0],
16n/a len(input))
17n/a
18n/adef decode(input, errors='strict'):
19n/a prefix = 0
20n/a if input[:3] == codecs.BOM_UTF8:
21n/a input = input[3:]
22n/a prefix = 3
23n/a (output, consumed) = codecs.utf_8_decode(input, errors, True)
24n/a return (output, consumed+prefix)
25n/a
26n/aclass IncrementalEncoder(codecs.IncrementalEncoder):
27n/a def __init__(self, errors='strict'):
28n/a codecs.IncrementalEncoder.__init__(self, errors)
29n/a self.first = 1
30n/a
31n/a def encode(self, input, final=False):
32n/a if self.first:
33n/a self.first = 0
34n/a return codecs.BOM_UTF8 + \
35n/a codecs.utf_8_encode(input, self.errors)[0]
36n/a else:
37n/a return codecs.utf_8_encode(input, self.errors)[0]
38n/a
39n/a def reset(self):
40n/a codecs.IncrementalEncoder.reset(self)
41n/a self.first = 1
42n/a
43n/a def getstate(self):
44n/a return self.first
45n/a
46n/a def setstate(self, state):
47n/a self.first = state
48n/a
49n/aclass IncrementalDecoder(codecs.BufferedIncrementalDecoder):
50n/a def __init__(self, errors='strict'):
51n/a codecs.BufferedIncrementalDecoder.__init__(self, errors)
52n/a self.first = 1
53n/a
54n/a def _buffer_decode(self, input, errors, final):
55n/a if self.first:
56n/a if len(input) < 3:
57n/a if codecs.BOM_UTF8.startswith(input):
58n/a # not enough data to decide if this really is a BOM
59n/a # => try again on the next call
60n/a return ("", 0)
61n/a else:
62n/a self.first = 0
63n/a else:
64n/a self.first = 0
65n/a if input[:3] == codecs.BOM_UTF8:
66n/a (output, consumed) = \
67n/a codecs.utf_8_decode(input[3:], errors, final)
68n/a return (output, consumed+3)
69n/a return codecs.utf_8_decode(input, errors, final)
70n/a
71n/a def reset(self):
72n/a codecs.BufferedIncrementalDecoder.reset(self)
73n/a self.first = 1
74n/a
75n/a def getstate(self):
76n/a state = codecs.BufferedIncrementalDecoder.getstate(self)
77n/a # state[1] must be 0 here, as it isn't passed along to the caller
78n/a return (state[0], self.first)
79n/a
80n/a def setstate(self, state):
81n/a # state[1] will be ignored by BufferedIncrementalDecoder.setstate()
82n/a codecs.BufferedIncrementalDecoder.setstate(self, state)
83n/a self.first = state[1]
84n/a
85n/aclass StreamWriter(codecs.StreamWriter):
86n/a def reset(self):
87n/a codecs.StreamWriter.reset(self)
88n/a try:
89n/a del self.encode
90n/a except AttributeError:
91n/a pass
92n/a
93n/a def encode(self, input, errors='strict'):
94n/a self.encode = codecs.utf_8_encode
95n/a return encode(input, errors)
96n/a
97n/aclass StreamReader(codecs.StreamReader):
98n/a def reset(self):
99n/a codecs.StreamReader.reset(self)
100n/a try:
101n/a del self.decode
102n/a except AttributeError:
103n/a pass
104n/a
105n/a def decode(self, input, errors='strict'):
106n/a if len(input) < 3:
107n/a if codecs.BOM_UTF8.startswith(input):
108n/a # not enough data to decide if this is a BOM
109n/a # => try again on the next call
110n/a return ("", 0)
111n/a elif input[:3] == codecs.BOM_UTF8:
112n/a self.decode = codecs.utf_8_decode
113n/a (output, consumed) = codecs.utf_8_decode(input[3:],errors)
114n/a return (output, consumed+3)
115n/a # (else) no BOM present
116n/a self.decode = codecs.utf_8_decode
117n/a return codecs.utf_8_decode(input, errors)
118n/a
119n/a### encodings module API
120n/a
121n/adef getregentry():
122n/a return codecs.CodecInfo(
123n/a name='utf-8-sig',
124n/a encode=encode,
125n/a decode=decode,
126n/a incrementalencoder=IncrementalEncoder,
127n/a incrementaldecoder=IncrementalDecoder,
128n/a streamreader=StreamReader,
129n/a streamwriter=StreamWriter,
130n/a )