Merged release21-maint changes.
[python/dscho.git] / Lib / encodings / zlib_codec.py
blob035bb0480a6ee6e14919135eca109cbc2d2efe3e
1 """ Python 'zlib_codec' Codec - zlib compression encoding
3 Unlike most of the other codecs which target Unicode, this codec
4 will return Python string objects for both encode and decode.
6 Written by Marc-Andre Lemburg (mal@lemburg.com).
8 """
9 import codecs
10 import zlib # this codec needs the optional zlib module !
12 ### Codec APIs
14 def zlib_encode(input,errors='strict'):
16 """ Encodes the object input and returns a tuple (output
17 object, length consumed).
19 errors defines the error handling to apply. It defaults to
20 'strict' handling which is the only currently supported
21 error handling for this codec.
23 """
24 assert errors == 'strict'
25 output = zlib.compress(input)
26 return (output, len(input))
28 def zlib_decode(input,errors='strict'):
30 """ Decodes the object input and returns a tuple (output
31 object, length consumed).
33 input must be an object which provides the bf_getreadbuf
34 buffer slot. Python strings, buffer objects and memory
35 mapped files are examples of objects providing this slot.
37 errors defines the error handling to apply. It defaults to
38 'strict' handling which is the only currently supported
39 error handling for this codec.
41 """
42 assert errors == 'strict'
43 output = zlib.decompress(input)
44 return (output, len(input))
46 class Codec(codecs.Codec):
48 encode = zlib_encode
49 decode = zlib_decode
51 class StreamWriter(Codec,codecs.StreamWriter):
52 pass
54 class StreamReader(Codec,codecs.StreamReader):
55 pass
57 ### encodings module API
59 def getregentry():
61 return (zlib_encode,zlib_decode,StreamReader,StreamWriter)