ยปCore Development>Code coverage>Tools/scripts/make_ctype.py

Python code coverage for Tools/scripts/make_ctype.py

#countcontent
1n/a#!/usr/bin/env python3
2n/a"""Script that generates the ctype.h-replacement in stringobject.c."""
3n/a
4n/aNAMES = ("LOWER", "UPPER", "ALPHA", "DIGIT", "XDIGIT", "ALNUM", "SPACE")
5n/a
6n/aprint("""
7n/a#define FLAG_LOWER 0x01
8n/a#define FLAG_UPPER 0x02
9n/a#define FLAG_ALPHA (FLAG_LOWER|FLAG_UPPER)
10n/a#define FLAG_DIGIT 0x04
11n/a#define FLAG_ALNUM (FLAG_ALPHA|FLAG_DIGIT)
12n/a#define FLAG_SPACE 0x08
13n/a#define FLAG_XDIGIT 0x10
14n/a
15n/astatic unsigned int ctype_table[256] = {""")
16n/a
17n/afor i in range(128):
18n/a c = chr(i)
19n/a flags = []
20n/a for name in NAMES:
21n/a if name in ("ALPHA", "ALNUM"):
22n/a continue
23n/a if name == "XDIGIT":
24n/a method = lambda: c.isdigit() or c.upper() in "ABCDEF"
25n/a else:
26n/a method = getattr(c, "is" + name.lower())
27n/a if method():
28n/a flags.append("FLAG_" + name)
29n/a rc = repr(c)
30n/a if c == '\v':
31n/a rc = "'\\v'"
32n/a elif c == '\f':
33n/a rc = "'\\f'"
34n/a if not flags:
35n/a print(" 0, /* 0x%x %s */" % (i, rc))
36n/a else:
37n/a print(" %s, /* 0x%x %s */" % ("|".join(flags), i, rc))
38n/a
39n/afor i in range(128, 256, 16):
40n/a print(" %s," % ", ".join(16*["0"]))
41n/a
42n/aprint("};")
43n/aprint("")
44n/a
45n/afor name in NAMES:
46n/a print("#define IS%s(c) (ctype_table[Py_CHARMASK(c)] & FLAG_%s)" %
47n/a (name, name))
48n/a
49n/aprint("")
50n/a
51n/afor name in NAMES:
52n/a name = "is" + name.lower()
53n/a print("#undef %s" % name)
54n/a print("#define %s(c) undefined_%s(c)" % (name, name))
55n/a
56n/aprint("""
57n/astatic unsigned char ctype_tolower[256] = {""")
58n/a
59n/afor i in range(0, 256, 8):
60n/a values = []
61n/a for i in range(i, i+8):
62n/a if i < 128:
63n/a c = chr(i)
64n/a if c.isupper():
65n/a i = ord(c.lower())
66n/a values.append("0x%02x" % i)
67n/a print(" %s," % ", ".join(values))
68n/a
69n/aprint("};")
70n/a
71n/aprint("""
72n/astatic unsigned char ctype_toupper[256] = {""")
73n/a
74n/afor i in range(0, 256, 8):
75n/a values = []
76n/a for i in range(i, i+8):
77n/a if i < 128:
78n/a c = chr(i)
79n/a if c.islower():
80n/a i = ord(c.upper())
81n/a values.append("0x%02x" % i)
82n/a print(" %s," % ", ".join(values))
83n/a
84n/aprint("};")
85n/a
86n/aprint("""
87n/a#define TOLOWER(c) (ctype_tolower[Py_CHARMASK(c)])
88n/a#define TOUPPER(c) (ctype_toupper[Py_CHARMASK(c)])
89n/a
90n/a#undef tolower
91n/a#define tolower(c) undefined_tolower(c)
92n/a#undef toupper
93n/a#define toupper(c) undefined_toupper(c)
94n/a""")