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

Python code coverage for Lib/encodings/zlib_codec.py

#countcontent
1n/a"""Python 'zlib_codec' Codec - zlib compression encoding.
2n/a
3n/aThis codec de/encodes from bytes to bytes.
4n/a
5n/aWritten by Marc-Andre Lemburg (mal@lemburg.com).
6n/a"""
7n/a
8n/aimport codecs
9n/aimport zlib # this codec needs the optional zlib module !
10n/a
11n/a### Codec APIs
12n/a
13n/adef zlib_encode(input, errors='strict'):
14n/a assert errors == 'strict'
15n/a return (zlib.compress(input), len(input))
16n/a
17n/adef zlib_decode(input, errors='strict'):
18n/a assert errors == 'strict'
19n/a return (zlib.decompress(input), len(input))
20n/a
21n/aclass Codec(codecs.Codec):
22n/a def encode(self, input, errors='strict'):
23n/a return zlib_encode(input, errors)
24n/a def decode(self, input, errors='strict'):
25n/a return zlib_decode(input, errors)
26n/a
27n/aclass IncrementalEncoder(codecs.IncrementalEncoder):
28n/a def __init__(self, errors='strict'):
29n/a assert errors == 'strict'
30n/a self.errors = errors
31n/a self.compressobj = zlib.compressobj()
32n/a
33n/a def encode(self, input, final=False):
34n/a if final:
35n/a c = self.compressobj.compress(input)
36n/a return c + self.compressobj.flush()
37n/a else:
38n/a return self.compressobj.compress(input)
39n/a
40n/a def reset(self):
41n/a self.compressobj = zlib.compressobj()
42n/a
43n/aclass IncrementalDecoder(codecs.IncrementalDecoder):
44n/a def __init__(self, errors='strict'):
45n/a assert errors == 'strict'
46n/a self.errors = errors
47n/a self.decompressobj = zlib.decompressobj()
48n/a
49n/a def decode(self, input, final=False):
50n/a if final:
51n/a c = self.decompressobj.decompress(input)
52n/a return c + self.decompressobj.flush()
53n/a else:
54n/a return self.decompressobj.decompress(input)
55n/a
56n/a def reset(self):
57n/a self.decompressobj = zlib.decompressobj()
58n/a
59n/aclass StreamWriter(Codec, codecs.StreamWriter):
60n/a charbuffertype = bytes
61n/a
62n/aclass StreamReader(Codec, codecs.StreamReader):
63n/a charbuffertype = bytes
64n/a
65n/a### encodings module API
66n/a
67n/adef getregentry():
68n/a return codecs.CodecInfo(
69n/a name='zlib',
70n/a encode=zlib_encode,
71n/a decode=zlib_decode,
72n/a incrementalencoder=IncrementalEncoder,
73n/a incrementaldecoder=IncrementalDecoder,
74n/a streamreader=StreamReader,
75n/a streamwriter=StreamWriter,
76n/a _is_text_encoding=False,
77n/a )