diff Tools/scripts/gencodec.py @ 15706:bf9bb0e3dd34 legacy-trunk

This patch changes the default behaviour of the builtin charmap codec to not apply Latin-1 mappings for keys which are not found in the mapping dictionaries, but instead treat them as undefined mappings. The patch was originally written by Martin v. Loewis with some additional (cosmetic) changes and an updated test script by Marc-Andre Lemburg. The standard codecs were recreated from the most current files available at the Unicode.org site using the Tools/scripts/gencodec.py tool. This patch closes the bugs #116285 and #119960.
author Marc-André Lemburg <mal@egenix.com>
date Wed, 03 Jan 2001 21:29:14 +0000
parents ba80c9064491
children 14a27acc755a
line wrap: on
line diff
--- a/Tools/scripts/gencodec.py	Wed Jan 03 16:44:56 2001 +0000
+++ b/Tools/scripts/gencodec.py	Wed Jan 03 21:29:14 2001 +0000
@@ -1,9 +1,9 @@
 """ Unicode Mapping Parser and Codec Generator.
 
 This script parses Unicode mapping files as available from the Unicode
-site (ftp.unicode.org) and creates Python codec modules from them. The
-codecs use the standard character mapping codec to actually apply the
-mapping.
+site (ftp://ftp.unicode.org/Public/MAPPINGS/) and creates Python codec
+modules from them. The codecs use the standard character mapping codec
+to actually apply the mapping.
 
 Synopsis: gencodec.py dir codec_prefix
 
@@ -18,6 +18,7 @@
 Written by Marc-Andre Lemburg (mal@lemburg.com).
 
 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
+(c) Copyright Guido van Rossum, 2000.
 
 """#"
 
@@ -70,6 +71,10 @@
     lines = f.readlines()
     f.close()
     enc2uni = {}
+    identity = []
+    unmapped = range(256)
+    for i in range(256):
+        unmapped[i] = i
     for line in lines:
         line = strip(line)
         if not line or line[0] == '#':
@@ -85,8 +90,22 @@
             comment = ''
         else:
             comment = comment[1:]
-        if enc != uni:
+        if enc < 256:
+            unmapped.remove(enc)
+            if enc == uni:
+                identity.append(enc)
+            else:
+                enc2uni[enc] = (uni,comment)
+        else:
             enc2uni[enc] = (uni,comment)
+    # If there are more identity-mapped entries than unmapped entries,
+    # it pays to generate an identity dictionary first, add add explicit
+    # mappings to None for the rest
+    if len(identity)>=len(unmapped):
+        for enc in unmapped:
+            enc2uni[enc] = (None, "")
+        enc2uni['IDENTITY'] = 256
+
     return enc2uni
 
 def hexrepr(t,
@@ -143,11 +162,12 @@
     """
     l = [
         '''\
-""" Python Character Mapping Codec generated from '%s'.
+""" Python Character Mapping Codec generated from '%s' with gencodec.py.
 
 Written by Marc-Andre Lemburg (mal@lemburg.com).
 
 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
+(c) Copyright 2000 Guido van Rossum.
 
 """#"
 
@@ -178,15 +198,23 @@
     return (Codec().encode,Codec().decode,StreamReader,StreamWriter)
 
 ### Decoding Map
-
-decoding_map = {
 ''' % name,
         ]
+
+    if map.has_key("IDENTITY"):
+        l.append("decoding_map = codecs.make_identity_dict(range(%d))"
+                 % map["IDENTITY"])
+        l.append("decoding_map.update({")
+        splits = 1
+        del map["IDENTITY"]
+    else:
+        l.append("decoding_map = {")
+        splits = 0
+        
     mappings = map.items()
     mappings.sort()
     append = l.append
     i = 0
-    splits = 0
     for e,value in mappings:
         try:
             (u,c) = value
@@ -198,7 +226,7 @@
             append('\t%s: %s,\t# %s' % (key,unicoderepr(u),c))
         else:
             append('\t%s: %s,' % (key,unicoderepr(u)))
-        i = i + 1
+        i += 1
         if i == 4096:
             # Split the definition into parts to that the Python
             # parser doesn't dump core
@@ -206,7 +234,7 @@
                 append('}')
             else:
                 append('})')
-            append('map.update({')
+            append('decoding_map.update({')
             i = 0
             splits = splits + 1
     if splits == 0:
@@ -265,7 +293,7 @@
     
     mapnames = os.listdir(dir)
     for mapname in mapnames:
-        if mapname[-len('.mapping'):] != '.mapping':
+        if not mapname.endswith('.mapping'):
             continue
         codefile = mapname[:-len('.mapping')] + '.py'
         print 'converting %s to %s' % (mapname,