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
|
from bgenOutput import *
from bgenType import *
Error = "bgenGenerator.Error"
# Strings to specify argument transfer modes in generator calls
IN = "in"
OUT = "out"
INOUT = IN_OUT = "in-out"
class Generator:
# XXX There are some funny things with the argument list.
# XXX It would be better to get rid of this and require
# XXX each argument to be a type object or a tuple of the form
# XXX (inoutmode, typeobject, argumentname)
# XXX or possibly even a Variable() instance...
def __init__(self, *argumentList):
apply(self.parseArguments, argumentList)
self.prefix = "XXX" # Will be changed by setprefix() call
self.objecttype = "object" # Type of _self argument to function
self.itselftype = None # Type of _self->ob_itself, if defined
def setprefix(self, prefix):
self.prefix = prefix
def setselftype(self, selftype, itselftype):
self.objecttype = selftype
self.itselftype = itselftype
def parseArguments(self, returnType, name, *argumentList):
if returnType:
self.rv = Variable(returnType, "_rv", OutMode)
else:
self.rv = None
self.name = name
self.argumentList = []
if self.rv:
self.argumentList.append(rv)
self.parseArgumentList(argumentList)
def parseArgumentList(self, argumentList):
from types import *
iarg = 0
for arg in argumentList:
# Arguments can either be:
# - a type
# - a tuple (type, [name, [mode]])
# - a variable
iarg = iarg + 1
if hasattr(arg, 'typeName'):
arg = Variable(arg)
elif type(arg) == TupleType:
arg = apply(Variable, arg)
if arg.name is None:
arg.name = "_arg%d" % iarg
self.argumentList.append(arg)
def reference(self, name = None):
if name is None:
name = self.name
Output("{\"%s\", (PyCFunction)%s_%s, 1},",
name, self.prefix, self.name)
def generate(self):
self.functionheader()
self.declarations()
self.getargs()
self.callit()
self.checkit()
self.returnvalue()
self.functiontrailer()
def functionheader(self):
Output()
Output("static PyObject *%s_%s(_self, _args)",
self.prefix, self.name)
IndentLevel()
Output("%s *_self;", self.objecttype)
Output("PyObject *_args;")
DedentLevel()
OutLbrace()
def declarations(self):
for arg in self.argumentList:
arg.declare()
def getargs(self):
fmt = ""
lst = ""
for arg in self.argumentList:
if arg.flags == SelfMode:
continue
if arg.mode in (InMode, InOutMode):
fmt = fmt + arg.getargsFormat()
lst = lst + ", " + arg.getargsArgs()
Output("if (!PyArg_ParseTuple(_args, \"%s\"%s))", fmt, lst)
IndentLevel()
Output("return NULL;")
DedentLevel()
for arg in self.argumentList:
if arg.flags == SelfMode:
continue
if arg.mode in (InMode, InOutMode):
arg.getargsCheck()
def callit(self):
args = ""
for arg in self.argumentList:
if arg is self.rv:
continue
s = arg.passArgument()
if args: s = ", " + s
args = args + s
if self.rv:
Output("%s = %s(%s);",
self.rv.name, self.name, args)
else:
Output("%s(%s);", self.name, args)
def checkit(self):
for arg in self.argumentList:
arg.errorCheck()
def returnvalue(self):
fmt = ""
lst = ""
for arg in self.argumentList:
if not arg: continue
if arg.flags == ErrorMode: continue
if arg.mode in (OutMode, InOutMode):
fmt = fmt + arg.mkvalueFormat()
lst = lst + ", " + arg.mkvalueArgs()
if fmt == "":
Output("Py_INCREF(Py_None);")
Output("return Py_None;");
else:
Output("return Py_BuildValue(\"%s\"%s);", fmt, lst)
def functiontrailer(self):
OutRbrace()
class ManualGenerator(Generator):
def __init__(self, name, body):
self.name = name
self.body = body
def definition(self):
self.functionheader()
Output("%s", self.body)
self.functiontrailer()
def _test():
void = None
eggs = Generator(void, "eggs",
Variable(stringptr, 'cmd'),
Variable(int, 'x'),
Variable(double, 'y', InOutMode),
Variable(int, 'status', ErrorMode),
)
eggs.setprefix("spam")
print "/* START */"
eggs.generate()
if __name__ == "__main__":
_test()
|