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

Python code coverage for Lib/encodings/bz2_codec.py

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