ยปCore Development>Code coverage>Tools/unicode/comparecodecs.py

Python code coverage for Tools/unicode/comparecodecs.py

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