-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmakeStructs.py
executable file
·248 lines (197 loc) · 7.63 KB
/
makeStructs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# Copyright 2004-2010 Grant T. Olson.
# See license.txt for terms.
import re, sys, glob, os, time
#These files are in the 'include' directory, but are modules.
#They won't get built properly
skipFiles = ('datetime.h','py_curses.h','structseq.h','symtable.h')
def emitFileHeader():
print """/* Copyright 2004-2006 Grant T. Olson. See license.txt for terms.*/
#include <Python.h>
/* file autogenerated by pyasm's makeStructs script on """ + time.ctime() + """ %/
/* Preprocessor abuse at it's finest.
We could probably do all of this in a straight python file, but then
it wouldn't be in sync with a particular build. This insures we have
the right offsets for our structs in a way we can't in pure python
*/
#define OFFSET_STRING(f) #f
#define OFFSET(m,s,f) \
offset = PyInt_FromLong((long)&(((s*)0)->f)); \
Py_INCREF(offset); \
PyModule_AddObject(m, OFFSET_STRING(f), offset);
/* Py_DEBUG implies Py_TRACE_REFS. */
#if defined(Py_DEBUG) && !defined(Py_TRACE_REFS)
#define Py_TRACE_REFS
#endif
/* Py_TRACE_REFS implies Py_REF_DEBUG. */
#if defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG)
#define Py_REF_DEBUG
#endif
static PyObject *StructsError;
static PyObject *offset;
static PyMethodDef StructsMethods[] = {
{NULL, NULL, 0, NULL} /* Sentinel */
};
"""
def emitFileBody():
print """
static void
load_PyObject(PyObject* module)
{
#ifdef Py_TRACE_REFS
OFFSET(module,PyObject,_ob_next);
OFFSET(module,PyObject,_ob_prev);
#endif
OFFSET(module,PyObject,ob_refcnt);
OFFSET(module,PyObject,ob_type);
}
static void
load_PyVarObject(PyObject* module)
{
load_PyObject(module);
OFFSET(module,PyVarObject,ob_size);
}
"""
def emitFileFooter(modules):
print """
PyMODINIT_FUNC
initstructs(void)
{
PyObject *m, *n, *o;
/*PyObject *offset;*/
m = Py_InitModule("structs", StructsMethods);
n = Py_InitModule("PyObject", StructsMethods);
o = Py_InitModule("PyVarObject", StructsMethods);
StructsError = PyErr_NewException("structs.StructsError", NULL, NULL);
Py_INCREF(StructsError);
PyModule_AddObject(m, "StructsError", StructsError);
load_PyObject(n);
Py_INCREF(n);
PyModule_AddObject(m, "PyObject", n);
load_PyVarObject(o);
Py_INCREF(o);
PyModule_AddObject(m, "PyVarObject", o);
%s
}""" % ''.join([" load_%s(m);\n" % x for x in modules])
def emitModuleHeader(moduleName):
print """static void
load_%(funcname)s(PyObject *structs)
{
PyObject *sm = Py_InitModule("%(funcname)s",StructsMethods);
""" % {'funcname':moduleName}
def emitModuleFooter(moduleName):
print """
Py_INCREF(sm);
PyModule_AddObject(structs,"%(funcname)s",sm);
}""" % {'funcname':moduleName}
structsRe = re.compile("typedef\s+struct\s*\w*\s*{(.*?)}\s*(\w+)",re.DOTALL)
typeofRe = re.compile(r"(?P<type>\w+)\s*(?P<rest>[^;]+);")
variablesRe = re.compile(r"(\(|\)|\*\*|\*|\[|\]|\w+)[,\s]*")
names = []
def emitComment(commentText):
print "/* %s */" % commentText
def emitRaw(rawText):
print rawText
def emitOffset(name,val):
print " OFFSET(sm,%s,%s);" % (name,val)
def parse_filetext(filetext):
global names
for struct in structsRe.findall(filetext):
body,name = struct
if name in ('PyObject','PyVarObject', 'PyFrameObject'):
emitComment("Skipping object %s" % name)
continue
print >> sys.stderr, "NAME", name
startComment = body.find("/*")
while startComment >= 0: #strip multiline comments
endComment = body.find("*/",startComment) + 2
body = body[:startComment] + body[endComment:]
startComment = body.find("/*")
lines = body.split("\n")
isPyObject = False
for line in lines:
line = line.strip()
if not line:
continue
print >> sys.stderr, "LINE:" , line
if line.startswith("#"):
print >> sys.stderr, "PREPROCESSOR DIRECTIVE"
emitRaw(line)
elif line == 'PyObject_HEAD':
print >> sys.stderr, "HEADER" , line
isPyObject = True
emitModuleHeader(name)
names.append(name)
emitRaw(" load_PyObject(sm);")
elif line == 'PyObject_VAR_HEAD':
print >> sys.stderr, "HEADER" , line
isPyObject = True
emitModuleHeader(name)
names.append(name)
emitRaw(" load_PyVarObject(sm);")
elif line:
if isPyObject == False:
print >> sys.stderr, "NOT A PyObject: SKIPPING" , name
emitComment("Skipping struct %s, not a PyObject based struct" % name)
break
typeof,rest = typeofRe.match(line).groups()
print >> sys.stderr, "TYPE", typeof
vars = variablesRe.findall(rest)
vars.reverse()
if typeof == "struct": # skip struct def
print >> sys.stderr, "STRUCT", vars
vars.pop()
while vars:
var = vars.pop()
if var in ('*', '**'):
var = vars.pop()
if var == "(":
#function pointer
print >> sys.stderr, "FUNCTION POINTER", vars
var = vars.pop()
if var != "*":
print >> sys.stderr, var, vars
raise RuntimeError("Invalid Function Pointer "
"format: %s. Expected '*' got %s from %s" % (line,var,vars))
var = vars.pop()
emitOffset(name, var)
vars = None
else:
print >> sys.stderr, "POINTER", var
emitOffset(name, var)
elif var == '(':
print >> sys.stderr, "FUNCTION POINTER", vars
var = vars.pop()
print >> sys.stderr, "NAME VAR" , name, var
if var != "*":
print >> sys.stderr, var, vars
raise RuntimeError("Invalid Function Pointer "
"format: %s. Expected '*' got %s from %s" % (line,var,vars))
var = vars.pop()
emitOffset(name, var)
vars = None
elif var == "[":
print >> sys.stderr, "SKIPPING ARRAY STUB" , vars
var = vars.pop()
var = vars.pop()
else:
print >> sys.stderr, "normal", var
emitOffset(name,var)
if isPyObject == True:
emitModuleFooter(name)
def parse_headers():
headerDir = os.path.join(sys.exec_prefix, "include")
headerFiles = glob.glob(os.path.join(headerDir,"*.h"))
headerFiles = [x for x in headerFiles if os.path.split(x)[1] not in skipFiles]
for filename in headerFiles:
print >> sys.stderr, "PROCESSING FILE", filename
print "\n\n/* Generated from file %s */\n\n" % filename
f = file(filename)
filetext = f.read()
f.close()
parse_filetext(filetext)
def make_struct_c():
emitFileHeader()
emitFileBody()
parse_headers()
emitFileFooter(names)
make_struct_c()