Marc-André Lemburg | ef34274 | 2001-01-10 10:21:11 +0000 | [diff] [blame] | 1 | """ Test Codecs (used by test_charmapcodec) |
| 2 | |
| 3 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 4 | |
| 5 | (c) Copyright 2000 Guido van Rossum. |
| 6 | |
| 7 | """#" |
| 8 | import codecs |
| 9 | |
| 10 | ### Codec APIs |
| 11 | |
| 12 | class Codec(codecs.Codec): |
| 13 | |
| 14 | def encode(self,input,errors='strict'): |
| 15 | |
| 16 | return codecs.charmap_encode(input,errors,encoding_map) |
Tim Peters | d2bf3b7 | 2001-01-18 02:22:22 +0000 | [diff] [blame] | 17 | |
Marc-André Lemburg | ef34274 | 2001-01-10 10:21:11 +0000 | [diff] [blame] | 18 | def decode(self,input,errors='strict'): |
| 19 | |
| 20 | return codecs.charmap_decode(input,errors,decoding_map) |
| 21 | |
| 22 | class StreamWriter(Codec,codecs.StreamWriter): |
| 23 | pass |
Tim Peters | d2bf3b7 | 2001-01-18 02:22:22 +0000 | [diff] [blame] | 24 | |
Marc-André Lemburg | ef34274 | 2001-01-10 10:21:11 +0000 | [diff] [blame] | 25 | class StreamReader(Codec,codecs.StreamReader): |
| 26 | pass |
| 27 | |
| 28 | ### encodings module API |
| 29 | |
| 30 | def getregentry(): |
| 31 | |
| 32 | return (Codec().encode,Codec().decode,StreamReader,StreamWriter) |
| 33 | |
| 34 | ### Decoding Map |
| 35 | |
| 36 | decoding_map = codecs.make_identity_dict(range(256)) |
| 37 | decoding_map.update({ |
| 38 | 0x78: u"abc", # 1-n decoding mapping |
| 39 | "abc": 0x0078,# 1-n encoding mapping |
| 40 | 0x01: None, # decoding mapping to <undefined> |
| 41 | 0x79: u"", # decoding mapping to <remove character> |
| 42 | }) |
| 43 | |
| 44 | ### Encoding Map |
| 45 | |
| 46 | encoding_map = {} |
| 47 | for k,v in decoding_map.items(): |
| 48 | encoding_map[v] = k |