annotate Tools/scripts/make_ctype.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 65d5ecefd50d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
71557
65d5ecefd50d Fix missing or wrong shebangs and missing executable bits for scripts (#10318)
Éric Araujo <merwok@netwok.org>
parents: 43509
diff changeset
1 #!/usr/bin/env python3
43509
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 """Script that generates the ctype.h-replacement in stringobject.c."""
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 NAMES = ("LOWER", "UPPER", "ALPHA", "DIGIT", "XDIGIT", "ALNUM", "SPACE")
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 print("""
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #define FLAG_LOWER 0x01
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 #define FLAG_UPPER 0x02
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 #define FLAG_ALPHA (FLAG_LOWER|FLAG_UPPER)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 #define FLAG_DIGIT 0x04
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 #define FLAG_ALNUM (FLAG_ALPHA|FLAG_DIGIT)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 #define FLAG_SPACE 0x08
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 #define FLAG_XDIGIT 0x10
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 static unsigned int ctype_table[256] = {""")
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 for i in range(128):
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 c = chr(i)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 flags = []
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 for name in NAMES:
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 if name in ("ALPHA", "ALNUM"):
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 continue
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 if name == "XDIGIT":
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 method = lambda: c.isdigit() or c.upper() in "ABCDEF"
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 else:
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 method = getattr(c, "is" + name.lower())
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 if method():
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 flags.append("FLAG_" + name)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 rc = repr(c)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 if c == '\v':
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 rc = "'\\v'"
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 elif c == '\f':
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 rc = "'\\f'"
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 if not flags:
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 print(" 0, /* 0x%x %s */" % (i, rc))
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 else:
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 print(" %s, /* 0x%x %s */" % ("|".join(flags), i, rc))
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 for i in range(128, 256, 16):
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 print(" %s," % ", ".join(16*["0"]))
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 print("};")
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 print("")
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 for name in NAMES:
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 print("#define IS%s(c) (ctype_table[Py_CHARMASK(c)] & FLAG_%s)" %
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 (name, name))
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 print("")
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 for name in NAMES:
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 name = "is" + name.lower()
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 print("#undef %s" % name)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 print("#define %s(c) undefined_%s(c)" % (name, name))
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 print("""
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 static unsigned char ctype_tolower[256] = {""")
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 for i in range(0, 256, 8):
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 values = []
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 for i in range(i, i+8):
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 if i < 128:
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 c = chr(i)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 if c.isupper():
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 i = ord(c.lower())
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 values.append("0x%02x" % i)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 print(" %s," % ", ".join(values))
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 print("};")
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 print("""
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 static unsigned char ctype_toupper[256] = {""")
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 for i in range(0, 256, 8):
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 values = []
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 for i in range(i, i+8):
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 if i < 128:
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 c = chr(i)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 if c.islower():
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 i = ord(c.upper())
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 values.append("0x%02x" % i)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 print(" %s," % ", ".join(values))
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 print("};")
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 print("""
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 #define TOLOWER(c) (ctype_tolower[Py_CHARMASK(c)])
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 #define TOUPPER(c) (ctype_toupper[Py_CHARMASK(c)])
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 #undef tolower
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 #define tolower(c) undefined_tolower(c)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 #undef toupper
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 #define toupper(c) undefined_toupper(c)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 """)