1212INOUT = IN_OUT = "in-out"
1313
1414
15- class FunctionGenerator :
15+ class BaseFunctionGenerator :
1616
17- def __init__ (self , returntype , name , * argumentList ):
17+ def __init__ (self , name ):
1818 print "<--" , name
19- self .returntype = returntype
2019 self .name = name
20+ self .prefix = name
21+ self .objecttype = "PyObject" # Type of _self argument to function
22+
23+ def setprefix (self , prefix ):
24+ self .prefix = prefix
25+
26+ def generate (self ):
27+ print "-->" , self .name
28+ self .functionheader ()
29+ self .functionbody ()
30+ self .functiontrailer ()
31+
32+ def functionheader (self ):
33+ Output ()
34+ Output ("static PyObject *%s_%s(_self, _args)" ,
35+ self .prefix , self .name )
36+ IndentLevel ()
37+ Output ("%s *_self;" , self .objecttype )
38+ Output ("PyObject *_args;" )
39+ DedentLevel ()
40+ OutLbrace ()
41+ Output ("PyObject *_res = NULL;" )
42+
43+ def functionbody (self ):
44+ Output ("/* XXX To be provided */" )
45+
46+ def functiontrailer (self ):
47+ OutRbrace ()
48+
49+ def reference (self , name = None ):
50+ if name is None :
51+ name = self .name
52+ docstring = self .docstring ()
53+ Output ("{\" %s\" , (PyCFunction)%s_%s, 1," , name , self .prefix , self .name )
54+ Output (" %s}," , stringify (docstring ))
55+
56+ def docstring (self ):
57+ return None
58+
59+
60+ _stringify_map = {'\n ' : '\\ n' , '\t ' : '\\ t' , '\r ' : '\\ r' , '\b ' : '\\ b' ,
61+ '\e' : '\\ e' , '\a ' : '\\ a' , '\f ' : '\\ f' , '"' : '\\ "' }
62+ def stringify (str ):
63+ if str is None : return "NULL"
64+ res = '"'
65+ map = _stringify_map
66+ for c in str :
67+ if map .has_key (c ): res = res + map [c ]
68+ elif ' ' <= c <= '~' : res = res + c
69+ else : res = res + '\\ %03o' % ord (c )
70+ res = res + '"'
71+ return res
72+
73+
74+ class ManualGenerator (BaseFunctionGenerator ):
75+
76+ def __init__ (self , name , body ):
77+ BaseFunctionGenerator .__init__ (self , name )
78+ self .body = body
79+
80+ def functionbody (self ):
81+ Output ("%s" , self .body )
82+
83+
84+ class FunctionGenerator (BaseFunctionGenerator ):
85+
86+ def __init__ (self , returntype , name , * argumentList ):
87+ BaseFunctionGenerator .__init__ (self , name )
88+ self .returntype = returntype
2189 self .argumentList = []
2290 self .setreturnvar ()
2391 self .parseArgumentList (argumentList )
2492 self .prefix = "XXX" # Will be changed by setprefix() call
25- self .objecttype = "PyObject" # Type of _self argument to function
2693 self .itselftype = None # Type of _self->ob_itself, if defined
2794
2895 def setreturnvar (self ):
@@ -35,9 +102,6 @@ def setreturnvar(self):
35102 def makereturnvar (self ):
36103 return Variable (self .returntype , "_rv" , OutMode )
37104
38- def setprefix (self , prefix ):
39- self .prefix = prefix
40-
41105 def setselftype (self , selftype , itselftype ):
42106 self .objecttype = selftype
43107 self .itselftype = itselftype
@@ -49,13 +113,6 @@ def parseArgumentList(self, argumentList):
49113 if name is None : name = "_arg%d" % iarg
50114 arg = Variable (type , name , mode )
51115 self .argumentList .append (arg )
52-
53- def reference (self , name = None ):
54- if name is None :
55- name = self .name
56- docstring = self .docstring ()
57- Output ("{\" %s\" , (PyCFunction)%s_%s, 1," , name , self .prefix , self .name )
58- Output (" %s}," , stringify (docstring ))
59116
60117 def docstring (self ):
61118 import string
@@ -89,27 +146,13 @@ def docstring(self):
89146 else :
90147 outstr = "(%s)" % string .joinfields (output , ", " )
91148 return instr + " -> " + outstr
92-
93- def generate (self ):
94- print "-->" , self .name
95- self .functionheader ()
149+
150+ def functionbody (self ):
96151 self .declarations ()
97152 self .getargs ()
98153 self .callit ()
99154 self .checkit ()
100155 self .returnvalue ()
101- self .functiontrailer ()
102-
103- def functionheader (self ):
104- Output ()
105- Output ("static PyObject *%s_%s(_self, _args)" ,
106- self .prefix , self .name )
107- IndentLevel ()
108- Output ("%s *_self;" , self .objecttype )
109- Output ("PyObject *_args;" )
110- DedentLevel ()
111- OutLbrace ()
112- Output ("PyObject *_res = NULL;" )
113156
114157 def declarations (self ):
115158 for arg in self .argumentList :
@@ -182,9 +225,6 @@ def returnvalue(self):
182225 arg .cleanup ()
183226 Output ("return _res;" )
184227
185- def functiontrailer (self ):
186- OutRbrace ()
187-
188228
189229class MethodGenerator (FunctionGenerator ):
190230
@@ -197,29 +237,6 @@ def parseArgumentList(self, args):
197237 self .argumentList .append (self .itself )
198238 FunctionGenerator .parseArgumentList (self , args )
199239
200- class ManualGenerator (FunctionGenerator ):
201-
202- def __init__ (self , name , body ):
203- self .name = name
204- self .body = body
205-
206- def definition (self ):
207- self .functionheader ()
208- Output ("%s" , self .body )
209- self .functiontrailer ()
210-
211- _stringify_map = {'\n ' : '\\ n' , '\t ' : '\\ t' , '\r ' : '\\ r' , '\b ' : '\\ b' ,
212- '\e' : '\\ e' , '\a ' : '\\ a' , '\f ' : '\\ f' , '"' : '\\ "' }
213- def stringify (str ):
214- if str is None : return "None"
215- res = '"'
216- map = _stringify_map
217- for c in str :
218- if map .has_key (c ): res = res + map [c ]
219- elif ' ' <= c <= '~' : res = res + c
220- else : res = res + '\\ %03o' % ord (c )
221- res = res + '"'
222- return res
223240
224241def _test ():
225242 void = None
@@ -233,5 +250,6 @@ def _test():
233250 print "/* START */"
234251 eggs .generate ()
235252
253+
236254if __name__ == "__main__" :
237255 _test ()
0 commit comments