You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.4 KiB

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