Skip to content

Commit 92b201d

Browse files
committed
Add two new tools to compare codecs and show differences and to
list all installed codecs.
1 parent c5694c8 commit 92b201d

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

‎Tools/unicode/comparecodecs.py‎

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

‎Tools/unicode/listcodecs.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
""" List all available codec modules.
2+
3+
(c) Copyright 2005, Marc-Andre Lemburg (mal@lemburg.com).
4+
5+
Licensed to PSF under a Contributor Agreement.
6+
7+
"""
8+
9+
import os, codecs, encodings
10+
11+
_debug = 0
12+
13+
def listcodecs(dir):
14+
names = []
15+
for filename in os.listdir(dir):
16+
if filename[-3:] != '.py':
17+
continue
18+
name = filename[:-3]
19+
# Check whether we've found a true codec
20+
try:
21+
codecs.lookup(name)
22+
except LookupError:
23+
# Codec not found
24+
continue
25+
except Exception, reason:
26+
# Probably an error from importing the codec; still it's
27+
# a valid code name
28+
if _debug:
29+
print '* problem importing codec %r: %s' % \
30+
(name, reason)
31+
names.append(name)
32+
return names
33+
34+
35+
if __name__ == '__main__':
36+
names = listcodecs(encodings.__path__[0])
37+
names.sort()
38+
print 'all_codecs = ['
39+
for name in names:
40+
print ' %r,' % name
41+
print ']'

0 commit comments

Comments
 (0)