Raymond Hettinger | 9a80c5d | 2003-09-23 20:21:01 +0000 | [diff] [blame] | 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 | |
Raymond Hettinger | 9edae34 | 2003-09-24 03:57:36 +0000 | [diff] [blame] | 6 | Adapted by Raymond Hettinger from zlib_codec.py which was written |
Raymond Hettinger | 9a80c5d | 2003-09-23 20:21:01 +0000 | [diff] [blame] | 7 | by Marc-Andre Lemburg (mal@lemburg.com). |
| 8 | |
| 9 | """ |
| 10 | import codecs |
Raymond Hettinger | a455170 | 2003-12-01 10:41:02 +0000 | [diff] [blame] | 11 | import bz2 |
Raymond Hettinger | 9a80c5d | 2003-09-23 20:21:01 +0000 | [diff] [blame] | 12 | |
Raymond Hettinger | a455170 | 2003-12-01 10:41:02 +0000 | [diff] [blame] | 13 | def encode(input, errors='strict'): |
Raymond Hettinger | 9a80c5d | 2003-09-23 20:21:01 +0000 | [diff] [blame] | 14 | assert errors == 'strict' |
| 15 | output = bz2.compress(input) |
| 16 | return (output, len(input)) |
| 17 | |
Raymond Hettinger | a455170 | 2003-12-01 10:41:02 +0000 | [diff] [blame] | 18 | def decode(input, errors='strict'): |
Raymond Hettinger | 9a80c5d | 2003-09-23 20:21:01 +0000 | [diff] [blame] | 19 | assert errors == 'strict' |
| 20 | output = bz2.decompress(input) |
| 21 | return (output, len(input)) |
| 22 | |
Raymond Hettinger | 9a80c5d | 2003-09-23 20:21:01 +0000 | [diff] [blame] | 23 | ### encodings module API |
| 24 | |
| 25 | def getregentry(): |
| 26 | |
Raymond Hettinger | a455170 | 2003-12-01 10:41:02 +0000 | [diff] [blame] | 27 | return (encode, decode, codecs.StreamReader, codecs.StreamWriter) |