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