annotate Tools/scripts/make_ctype.py @ 71285:8bc9dbc61ba6 3.2

Issue #12326: document the recommended idiom for checking sys.platform on Unix systems. Also, point to the various alternatives.
author Antoine Pitrou <solipsis@pitrou.net>
date Sat, 09 Jul 2011 15:54:23 +0200
parents 4a72fc5dfb79
children 65d5ecefd50d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43509
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """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
2
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 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
4
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 print("""
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #define FLAG_LOWER 0x01
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #define FLAG_UPPER 0x02
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 #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
9 #define FLAG_DIGIT 0x04
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 #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
11 #define FLAG_SPACE 0x08
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 #define FLAG_XDIGIT 0x10
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 static unsigned int ctype_table[256] = {""")
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 for i in range(128):
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 c = chr(i)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 flags = []
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 for name in NAMES:
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 if name in ("ALPHA", "ALNUM"):
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 continue
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 if name == "XDIGIT":
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 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
24 else:
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 method = getattr(c, "is" + name.lower())
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 if method():
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 flags.append("FLAG_" + name)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 rc = repr(c)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 if c == '\v':
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 rc = "'\\v'"
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 elif c == '\f':
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 rc = "'\\f'"
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 if not flags:
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 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
35 else:
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 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
37
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 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
39 print(" %s," % ", ".join(16*["0"]))
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 print("};")
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
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 for name in NAMES:
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 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
46 (name, name))
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 print("")
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 for name in NAMES:
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 name = "is" + name.lower()
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 print("#undef %s" % name)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 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
54
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 print("""
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 static unsigned char ctype_tolower[256] = {""")
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 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
59 values = []
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 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
61 if i < 128:
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 c = chr(i)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 if c.isupper():
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 i = ord(c.lower())
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 values.append("0x%02x" % i)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 print(" %s," % ", ".join(values))
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 print("};")
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 print("""
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 static unsigned char ctype_toupper[256] = {""")
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 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
74 values = []
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 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
76 if i < 128:
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 c = chr(i)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 if c.islower():
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 i = ord(c.upper())
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 values.append("0x%02x" % i)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 print(" %s," % ", ".join(values))
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 print("};")
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 print("""
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 #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
87 #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
88
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 #undef tolower
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 #define tolower(c) undefined_tolower(c)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 #undef toupper
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 #define toupper(c) undefined_toupper(c)
4a72fc5dfb79 Silly script I'd rather not throw away.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 """)