Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a80c5d

Browse files
committed
Added codec for bz2 compression.
1 parent 9b32acd commit 9a80c5d

4 files changed

Lines changed: 75 additions & 0 deletions

File tree

Doc/lib/libcodecs.tex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,11 @@ \subsection{Standard Encodings}
804804
{byte string}
805805
{Convert operand to MIME base64}
806806

807+
\lineiv{bz2_codec}
808+
{bz2}
809+
{byte string}
810+
{Compress the operand using bz2}
811+
807812
\lineiv{hex_codec}
808813
{hex}
809814
{byte string}

Lib/encodings/aliases.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
'base64' : 'base64_codec',
4242
'base_64' : 'base64_codec',
4343

44+
# bz2_codec codec
45+
'bz2' : 'bz2_codec',
46+
4447
# cp037 codec
4548
'csibm037' : 'cp037',
4649
'ebcdic_cp_ca' : 'cp037',

Lib/encodings/bz2_codec.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
""" Python 'bz2_codec' Codec - bz2 compression encoding
2+
3+
Unlike most of the other codecs which target Unicode, this codec
4+
will return Python string objects for both encode and decode.
5+
6+
Adapted by Raymond Hettinger from bz2_codec which was written
7+
by Marc-Andre Lemburg ([email protected]).
8+
9+
"""
10+
import codecs
11+
import bz2 # this codec needs the optional bz2 module !
12+
13+
### Codec APIs
14+
15+
def bz2_encode(input,errors='strict'):
16+
17+
""" Encodes the object input and returns a tuple (output
18+
object, length consumed).
19+
20+
errors defines the error handling to apply. It defaults to
21+
'strict' handling which is the only currently supported
22+
error handling for this codec.
23+
24+
"""
25+
assert errors == 'strict'
26+
output = bz2.compress(input)
27+
return (output, len(input))
28+
29+
def bz2_decode(input,errors='strict'):
30+
31+
""" Decodes the object input and returns a tuple (output
32+
object, length consumed).
33+
34+
input must be an object which provides the bf_getreadbuf
35+
buffer slot. Python strings, buffer objects and memory
36+
mapped files are examples of objects providing this slot.
37+
38+
errors defines the error handling to apply. It defaults to
39+
'strict' handling which is the only currently supported
40+
error handling for this codec.
41+
42+
"""
43+
assert errors == 'strict'
44+
output = bz2.decompress(input)
45+
return (output, len(input))
46+
47+
class Codec(codecs.Codec):
48+
49+
def encode(self, input, errors='strict'):
50+
return bz2_encode(input, errors)
51+
def decode(self, input, errors='strict'):
52+
return bz2_decode(input, errors)
53+
54+
class StreamWriter(Codec,codecs.StreamWriter):
55+
pass
56+
57+
class StreamReader(Codec,codecs.StreamReader):
58+
pass
59+
60+
### encodings module API
61+
62+
def getregentry():
63+
64+
return (bz2_encode,bz2_decode,StreamReader,StreamWriter)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ Extension modules
4646
Library
4747
-------
4848

49+
- encodings.bz2_codec was added for access to bz2 compression
50+
using "a long string".encode('bz2')
51+
4952
- Various improvements to unittest.py, realigned with PyUnit CVS.
5053

5154
- dircache now passes exceptions to the caller, instead of returning

0 commit comments

Comments
 (0)