1 | n/a | #!/usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """ Compare the output of two codecs. |
---|
4 | n/a | |
---|
5 | n/a | (c) Copyright 2005, Marc-Andre Lemburg (mal@lemburg.com). |
---|
6 | n/a | |
---|
7 | n/a | Licensed to PSF under a Contributor Agreement. |
---|
8 | n/a | |
---|
9 | n/a | """ |
---|
10 | n/a | import sys |
---|
11 | n/a | |
---|
12 | n/a | def compare_codecs(encoding1, encoding2): |
---|
13 | n/a | |
---|
14 | n/a | print('Comparing encoding/decoding of %r and %r' % (encoding1, encoding2)) |
---|
15 | n/a | mismatch = 0 |
---|
16 | n/a | # Check encoding |
---|
17 | n/a | for i in range(sys.maxunicode+1): |
---|
18 | n/a | u = chr(i) |
---|
19 | n/a | try: |
---|
20 | n/a | c1 = u.encode(encoding1) |
---|
21 | n/a | except UnicodeError as reason: |
---|
22 | n/a | c1 = '<undefined>' |
---|
23 | n/a | try: |
---|
24 | n/a | c2 = u.encode(encoding2) |
---|
25 | n/a | except UnicodeError as reason: |
---|
26 | n/a | c2 = '<undefined>' |
---|
27 | n/a | if c1 != c2: |
---|
28 | n/a | print(' * encoding mismatch for 0x%04X: %-14r != %r' % \ |
---|
29 | n/a | (i, c1, c2)) |
---|
30 | n/a | mismatch += 1 |
---|
31 | n/a | # Check decoding |
---|
32 | n/a | for i in range(256): |
---|
33 | n/a | c = bytes([i]) |
---|
34 | n/a | try: |
---|
35 | n/a | u1 = c.decode(encoding1) |
---|
36 | n/a | except UnicodeError: |
---|
37 | n/a | u1 = '<undefined>' |
---|
38 | n/a | try: |
---|
39 | n/a | u2 = c.decode(encoding2) |
---|
40 | n/a | except UnicodeError: |
---|
41 | n/a | u2 = '<undefined>' |
---|
42 | n/a | if u1 != u2: |
---|
43 | n/a | print(' * decoding mismatch for 0x%04X: %-14r != %r' % \ |
---|
44 | n/a | (i, u1, u2)) |
---|
45 | n/a | mismatch += 1 |
---|
46 | n/a | if mismatch: |
---|
47 | n/a | print() |
---|
48 | n/a | print('Found %i mismatches' % mismatch) |
---|
49 | n/a | else: |
---|
50 | n/a | print('-> Codecs are identical.') |
---|
51 | n/a | |
---|
52 | n/a | if __name__ == '__main__': |
---|
53 | n/a | compare_codecs(sys.argv[1], sys.argv[2]) |
---|