""" Check .NET encoding encoding_maps exported using export-encodings.py Copyright (c) 2009, MAL """ import sys, codecs, marshal, glob _debug = 0 UNICODE_RANGE = sys.maxunicode def check_encoding_encoding_map(code_page, encoding, encoding_map): errors = 0 try: python_encoder = codecs.getencoder(encoding) except LookupError: print 'Unknown encoding in Python: %r' % encoding raise for i in range(UNICODE_RANGE): u = unichr(i) try: python_bytes, count = python_encoder(u) except ValueError: if _debug > 1: print '%i: skipped (Python encode error)' % i continue python_bytes = [ord(x) for x in python_bytes] net_bytes = encoding_map[i] if python_bytes == net_bytes: if _debug > 1: print '%i: OK' % i else: if _debug: print '%i: .NET=%r, Python=%r' % ( i, net_bytes, python_bytes) errors += 1 return errors for pattern in sys.argv[1:]: for mapfile in glob.glob(pattern): data = marshal.loads(open(mapfile, 'rb').read()) code_page, encoding, encoding_map = data print 'Checking code Page %i against encoding %r using file %r' % ( code_page, encoding, mapfile) errors = check_encoding_encoding_map(code_page, encoding, encoding_map) print print '%i errors' % errors print