1 | n/a | #!/usr/bin/env python3 |
---|
2 | n/a | # This script converts a C file to use the PEP 384 type definition API |
---|
3 | n/a | # Usage: abitype.py < old_code > new_code |
---|
4 | n/a | import re, sys |
---|
5 | n/a | |
---|
6 | n/a | ###### Replacement of PyTypeObject static instances ############## |
---|
7 | n/a | |
---|
8 | n/a | # classify each token, giving it a one-letter code: |
---|
9 | n/a | # S: static |
---|
10 | n/a | # T: PyTypeObject |
---|
11 | n/a | # I: ident |
---|
12 | n/a | # W: whitespace |
---|
13 | n/a | # =, {, }, ; : themselves |
---|
14 | n/a | def classify(): |
---|
15 | n/a | res = [] |
---|
16 | n/a | for t,v in tokens: |
---|
17 | n/a | if t == 'other' and v in "={};": |
---|
18 | n/a | res.append(v) |
---|
19 | n/a | elif t == 'ident': |
---|
20 | n/a | if v == 'PyTypeObject': |
---|
21 | n/a | res.append('T') |
---|
22 | n/a | elif v == 'static': |
---|
23 | n/a | res.append('S') |
---|
24 | n/a | else: |
---|
25 | n/a | res.append('I') |
---|
26 | n/a | elif t == 'ws': |
---|
27 | n/a | res.append('W') |
---|
28 | n/a | else: |
---|
29 | n/a | res.append('.') |
---|
30 | n/a | return ''.join(res) |
---|
31 | n/a | |
---|
32 | n/a | # Obtain a list of fields of a PyTypeObject, in declaration order, |
---|
33 | n/a | # skipping ob_base |
---|
34 | n/a | # All comments are dropped from the variable (which are typically |
---|
35 | n/a | # just the slot names, anyway), and information is discarded whether |
---|
36 | n/a | # the original type was static. |
---|
37 | n/a | def get_fields(start, real_end): |
---|
38 | n/a | pos = start |
---|
39 | n/a | # static? |
---|
40 | n/a | if tokens[pos][1] == 'static': |
---|
41 | n/a | pos += 2 |
---|
42 | n/a | # PyTypeObject |
---|
43 | n/a | pos += 2 |
---|
44 | n/a | # name |
---|
45 | n/a | name = tokens[pos][1] |
---|
46 | n/a | pos += 1 |
---|
47 | n/a | while tokens[pos][1] != '{': |
---|
48 | n/a | pos += 1 |
---|
49 | n/a | pos += 1 |
---|
50 | n/a | # PyVarObject_HEAD_INIT |
---|
51 | n/a | while tokens[pos][0] in ('ws', 'comment'): |
---|
52 | n/a | pos += 1 |
---|
53 | n/a | if tokens[pos][1] != 'PyVarObject_HEAD_INIT': |
---|
54 | n/a | raise Exception('%s has no PyVarObject_HEAD_INIT' % name) |
---|
55 | n/a | while tokens[pos][1] != ')': |
---|
56 | n/a | pos += 1 |
---|
57 | n/a | pos += 1 |
---|
58 | n/a | # field definitions: various tokens, comma-separated |
---|
59 | n/a | fields = [] |
---|
60 | n/a | while True: |
---|
61 | n/a | while tokens[pos][0] in ('ws', 'comment'): |
---|
62 | n/a | pos += 1 |
---|
63 | n/a | end = pos |
---|
64 | n/a | while tokens[end][1] not in ',}': |
---|
65 | n/a | if tokens[end][1] == '(': |
---|
66 | n/a | nesting = 1 |
---|
67 | n/a | while nesting: |
---|
68 | n/a | end += 1 |
---|
69 | n/a | if tokens[end][1] == '(': nesting+=1 |
---|
70 | n/a | if tokens[end][1] == ')': nesting-=1 |
---|
71 | n/a | end += 1 |
---|
72 | n/a | assert end < real_end |
---|
73 | n/a | # join field, excluding separator and trailing ws |
---|
74 | n/a | end1 = end-1 |
---|
75 | n/a | while tokens[end1][0] in ('ws', 'comment'): |
---|
76 | n/a | end1 -= 1 |
---|
77 | n/a | fields.append(''.join(t[1] for t in tokens[pos:end1+1])) |
---|
78 | n/a | if tokens[end][1] == '}': |
---|
79 | n/a | break |
---|
80 | n/a | pos = end+1 |
---|
81 | n/a | return name, fields |
---|
82 | n/a | |
---|
83 | n/a | # List of type slots as of Python 3.2, omitting ob_base |
---|
84 | n/a | typeslots = [ |
---|
85 | n/a | 'tp_name', |
---|
86 | n/a | 'tp_basicsize', |
---|
87 | n/a | 'tp_itemsize', |
---|
88 | n/a | 'tp_dealloc', |
---|
89 | n/a | 'tp_print', |
---|
90 | n/a | 'tp_getattr', |
---|
91 | n/a | 'tp_setattr', |
---|
92 | n/a | 'tp_reserved', |
---|
93 | n/a | 'tp_repr', |
---|
94 | n/a | 'tp_as_number', |
---|
95 | n/a | 'tp_as_sequence', |
---|
96 | n/a | 'tp_as_mapping', |
---|
97 | n/a | 'tp_hash', |
---|
98 | n/a | 'tp_call', |
---|
99 | n/a | 'tp_str', |
---|
100 | n/a | 'tp_getattro', |
---|
101 | n/a | 'tp_setattro', |
---|
102 | n/a | 'tp_as_buffer', |
---|
103 | n/a | 'tp_flags', |
---|
104 | n/a | 'tp_doc', |
---|
105 | n/a | 'tp_traverse', |
---|
106 | n/a | 'tp_clear', |
---|
107 | n/a | 'tp_richcompare', |
---|
108 | n/a | 'tp_weaklistoffset', |
---|
109 | n/a | 'tp_iter', |
---|
110 | n/a | 'iternextfunc', |
---|
111 | n/a | 'tp_methods', |
---|
112 | n/a | 'tp_members', |
---|
113 | n/a | 'tp_getset', |
---|
114 | n/a | 'tp_base', |
---|
115 | n/a | 'tp_dict', |
---|
116 | n/a | 'tp_descr_get', |
---|
117 | n/a | 'tp_descr_set', |
---|
118 | n/a | 'tp_dictoffset', |
---|
119 | n/a | 'tp_init', |
---|
120 | n/a | 'tp_alloc', |
---|
121 | n/a | 'tp_new', |
---|
122 | n/a | 'tp_free', |
---|
123 | n/a | 'tp_is_gc', |
---|
124 | n/a | 'tp_bases', |
---|
125 | n/a | 'tp_mro', |
---|
126 | n/a | 'tp_cache', |
---|
127 | n/a | 'tp_subclasses', |
---|
128 | n/a | 'tp_weaklist', |
---|
129 | n/a | 'tp_del', |
---|
130 | n/a | 'tp_version_tag', |
---|
131 | n/a | ] |
---|
132 | n/a | |
---|
133 | n/a | # Generate a PyType_Spec definition |
---|
134 | n/a | def make_slots(name, fields): |
---|
135 | n/a | res = [] |
---|
136 | n/a | res.append('static PyType_Slot %s_slots[] = {' % name) |
---|
137 | n/a | # defaults for spec |
---|
138 | n/a | spec = { 'tp_itemsize':'0' } |
---|
139 | n/a | for i, val in enumerate(fields): |
---|
140 | n/a | if val.endswith('0'): |
---|
141 | n/a | continue |
---|
142 | n/a | if typeslots[i] in ('tp_name', 'tp_doc', 'tp_basicsize', |
---|
143 | n/a | 'tp_itemsize', 'tp_flags'): |
---|
144 | n/a | spec[typeslots[i]] = val |
---|
145 | n/a | continue |
---|
146 | n/a | res.append(' {Py_%s, %s},' % (typeslots[i], val)) |
---|
147 | n/a | res.append('};') |
---|
148 | n/a | res.append('static PyType_Spec %s_spec = {' % name) |
---|
149 | n/a | res.append(' %s,' % spec['tp_name']) |
---|
150 | n/a | res.append(' %s,' % spec['tp_basicsize']) |
---|
151 | n/a | res.append(' %s,' % spec['tp_itemsize']) |
---|
152 | n/a | res.append(' %s,' % spec['tp_flags']) |
---|
153 | n/a | res.append(' %s_slots,' % name) |
---|
154 | n/a | res.append('};\n') |
---|
155 | n/a | return '\n'.join(res) |
---|
156 | n/a | |
---|
157 | n/a | |
---|
158 | n/a | if __name__ == '__main__': |
---|
159 | n/a | |
---|
160 | n/a | ############ Simplistic C scanner ################################## |
---|
161 | n/a | tokenizer = re.compile( |
---|
162 | n/a | r"(?P<preproc>#.*\n)" |
---|
163 | n/a | r"|(?P<comment>/\*.*?\*/)" |
---|
164 | n/a | r"|(?P<ident>[a-zA-Z_][a-zA-Z0-9_]*)" |
---|
165 | n/a | r"|(?P<ws>[ \t\n]+)" |
---|
166 | n/a | r"|(?P<other>.)", |
---|
167 | n/a | re.MULTILINE) |
---|
168 | n/a | |
---|
169 | n/a | tokens = [] |
---|
170 | n/a | source = sys.stdin.read() |
---|
171 | n/a | pos = 0 |
---|
172 | n/a | while pos != len(source): |
---|
173 | n/a | m = tokenizer.match(source, pos) |
---|
174 | n/a | tokens.append([m.lastgroup, m.group()]) |
---|
175 | n/a | pos += len(tokens[-1][1]) |
---|
176 | n/a | if tokens[-1][0] == 'preproc': |
---|
177 | n/a | # continuation lines are considered |
---|
178 | n/a | # only in preprocess statements |
---|
179 | n/a | while tokens[-1][1].endswith('\\\n'): |
---|
180 | n/a | nl = source.find('\n', pos) |
---|
181 | n/a | if nl == -1: |
---|
182 | n/a | line = source[pos:] |
---|
183 | n/a | else: |
---|
184 | n/a | line = source[pos:nl+1] |
---|
185 | n/a | tokens[-1][1] += line |
---|
186 | n/a | pos += len(line) |
---|
187 | n/a | |
---|
188 | n/a | # Main loop: replace all static PyTypeObjects until |
---|
189 | n/a | # there are none left. |
---|
190 | n/a | while 1: |
---|
191 | n/a | c = classify() |
---|
192 | n/a | m = re.search('(SW)?TWIW?=W?{.*?};', c) |
---|
193 | n/a | if not m: |
---|
194 | n/a | break |
---|
195 | n/a | start = m.start() |
---|
196 | n/a | end = m.end() |
---|
197 | n/a | name, fields = get_fields(start, end) |
---|
198 | n/a | tokens[start:end] = [('',make_slots(name, fields))] |
---|
199 | n/a | |
---|
200 | n/a | # Output result to stdout |
---|
201 | n/a | for t, v in tokens: |
---|
202 | n/a | sys.stdout.write(v) |
---|