forked from glix/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbgenModule.py
More file actions
94 lines (77 loc) · 2.68 KB
/
bgenModule.py
File metadata and controls
94 lines (77 loc) · 2.68 KB
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
from bgenOutput import *
from bgenGeneratorGroup import GeneratorGroup
class Module(GeneratorGroup):
def __init__(self, name, prefix = None,
includestuff = None,
finalstuff = None,
initstuff = None,
variablestuff = None,
longname = None):
GeneratorGroup.__init__(self, prefix or name)
self.name = name
if longname:
self.longname = longname
else:
self.longname = name
self.includestuff = includestuff
self.initstuff = initstuff
self.finalstuff = finalstuff
self.variablestuff = variablestuff
self.typeobjects = []
def addobject(self, od):
self.generators.append(od)
self.typeobjects.append(od)
od.setmodulename(self.longname)
def generate(self):
OutHeader1("Module " + self.name)
Output("#include \"Python.h\"")
Output()
if self.includestuff:
Output()
Output("%s", self.includestuff)
self.declareModuleVariables()
GeneratorGroup.generate(self)
if self.finalstuff:
Output()
Output("%s", self.finalstuff)
Output()
Output("void init%s(void)", self.name)
OutLbrace()
Output("PyObject *m;")
Output("PyObject *d;")
Output()
if self.initstuff:
Output("%s", self.initstuff)
Output()
Output("m = Py_InitModule(\"%s\", %s_methods);",
self.name, self.prefix)
Output("d = PyModule_GetDict(m);")
self.createModuleVariables()
OutRbrace()
OutHeader1("End module " + self.name)
def declareModuleVariables(self):
self.errorname = self.prefix + "_Error"
Output("static PyObject *%s;", self.errorname)
def createModuleVariables(self):
Output("""%s = %s;""", self.errorname, self.exceptionInitializer())
Output("""if (%s == NULL ||""", self.errorname)
Output(""" PyDict_SetItemString(d, "Error", %s) != 0)""",
self.errorname)
IndentLevel()
Output("""return;""")
DedentLevel()
for tp in self.typeobjects:
tp.outputTypeObjectInitializer()
if self.variablestuff:
Output("%s", self.variablestuff)
Output()
def exceptionInitializer(self):
return """PyErr_NewException("%s.Error", NULL, NULL)""" % self.name
def _test():
from bgenGenerator import FunctionGenerator
m = Module("spam", "", "#include <stdio.h>")
g = FunctionGenerator(None, "bacon")
m.add(g)
m.generate()
if __name__ == "__main__":
_test()