Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d6c180

Browse files
committed
fully adapted to new naming scheme and added some features for AppleEvent generation
1 parent 8cfc4bf commit 8d6c180

5 files changed

Lines changed: 137 additions & 83 deletions

File tree

Tools/bgen/bgen/bgenGenerator.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Generator:
2222
def __init__(self, *argumentList):
2323
apply(self.parseArguments, argumentList)
2424
self.prefix = "XXX" # Will be changed by setprefix() call
25-
self.objecttype = "object" # Type of _self argument to function
25+
self.objecttype = "PyObject" # Type of _self argument to function
2626
self.itselftype = None # Type of _self->ob_itself, if defined
2727

2828
def setprefix(self, prefix):
@@ -92,12 +92,13 @@ def declarations(self):
9292
def getargs(self):
9393
fmt = ""
9494
lst = ""
95+
sep = ",\n" + ' '*len("if (!PyArg_ParseTuple(")
9596
for arg in self.argumentList:
9697
if arg.flags == SelfMode:
9798
continue
9899
if arg.mode in (InMode, InOutMode):
99100
fmt = fmt + arg.getargsFormat()
100-
lst = lst + ", " + arg.getargsArgs()
101+
lst = lst + sep + arg.getargsArgs()
101102
Output("if (!PyArg_ParseTuple(_args, \"%s\"%s))", fmt, lst)
102103
IndentLevel()
103104
Output("return NULL;")
@@ -110,11 +111,12 @@ def getargs(self):
110111

111112
def callit(self):
112113
args = ""
114+
sep = ",\n" + ' '*len("%s = %s(" % (self.rv.name, self.name))
113115
for arg in self.argumentList:
114116
if arg is self.rv:
115117
continue
116118
s = arg.passArgument()
117-
if args: s = ", " + s
119+
if args: s = sep + s
118120
args = args + s
119121
if self.rv:
120122
Output("%s = %s(%s);",
@@ -129,12 +131,13 @@ def checkit(self):
129131
def returnvalue(self):
130132
fmt = ""
131133
lst = ""
134+
sep = ",\n" + ' '*len("return Py_BuildValue(")
132135
for arg in self.argumentList:
133136
if not arg: continue
134137
if arg.flags == ErrorMode: continue
135138
if arg.mode in (OutMode, InOutMode):
136139
fmt = fmt + arg.mkvalueFormat()
137-
lst = lst + ", " + arg.mkvalueArgs()
140+
lst = lst + sep + arg.mkvalueArgs()
138141
if fmt == "":
139142
Output("Py_INCREF(Py_None);")
140143
Output("return Py_None;");

Tools/bgen/bgen/bgenGeneratorGroup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def generate(self):
1414
for g in self.generators:
1515
g.generate()
1616
Output()
17-
Output("static struct methodlist %s_methods[] = {", self.prefix)
17+
Output("static PyMethodDef %s_methods[] = {", self.prefix)
1818
IndentLevel()
1919
for g in self.generators:
2020
g.reference()

Tools/bgen/bgen/bgenModule.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ class Module(GeneratorGroup):
55

66
def __init__(self, name, prefix = None,
77
includestuff = None,
8-
initstuff = None):
8+
initstuff = None,
9+
preinitstuff = None):
910
GeneratorGroup.__init__(self, prefix or name)
1011
self.name = name
1112
self.includestuff = includestuff
1213
self.initstuff = initstuff
14+
self.preinitstuff = preinitstuff
1315

1416
def addobject(self, od):
1517
self.generators.append(od)
1618

1719
def generate(self):
1820
OutHeader1("Module " + self.name)
19-
Output("#include <Python.h>")
21+
Output("#include \"Python.h\"")
2022
Output()
2123

2224
if self.includestuff:
@@ -26,36 +28,40 @@ def generate(self):
2628
self.declareModuleVariables()
2729

2830
GeneratorGroup.generate(self)
31+
32+
if self.preinitstuff:
33+
Output()
34+
Output("%s", self.preinitstuff)
2935

3036
Output()
3137
Output("void init%s()", self.name)
3238
OutLbrace()
33-
Output("object *m;")
34-
Output("object *d;")
39+
Output("PyObject *m;")
40+
Output("PyObject *d;")
3541
Output()
3642

3743
if self.initstuff:
3844
Output("%s", self.initstuff)
3945
Output()
4046

41-
Output("m = initmodule(\"%s\", %s_methods);",
47+
Output("m = Py_InitModule(\"%s\", %s_methods);",
4248
self.name, self.prefix)
43-
Output("d = getmoduledict(m);")
49+
Output("d = PyModule_GetDict(m);")
4450
self.createModuleVariables()
4551
OutRbrace()
4652
OutHeader1("End module " + self.name)
4753

4854
def declareModuleVariables(self):
4955
self.errorname = self.prefix + "_Error"
50-
Output("static object *%s;", self.errorname)
56+
Output("static PyObject *%s;", self.errorname)
5157

5258
def createModuleVariables(self):
53-
Output("""if ((%s = newstringobject("%s.Error")) == NULL ||""",
54-
self.errorname, self.name)
55-
Output(""" dictinsert(d, "Error", %s) != 0)""",
56-
self.errorname)
57-
Output("""\tfatal("can't initialize %s.Error");""",
58-
self.name)
59+
Output("""if ((%s = PyString_FromString("%s.Error")) == NULL ||""",
60+
self.errorname, self.name)
61+
Output(""" PyDict_SetItemString(d, "Error", %s) != 0)""",
62+
self.errorname)
63+
Output("""Py_FatalError("can't initialize %s.Error");""",
64+
self.name)
5965

6066

6167
def _test():

Tools/bgen/bgen/bgenObjectDefinition.py

Lines changed: 65 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ def __init__(self, name, prefix, itselftype):
99
self.name = name
1010
self.itselftype = itselftype
1111
self.objecttype = name + 'Object'
12-
self.typename = self.prefix + '_' + \
13-
string.upper(name[:1]) + \
14-
string.lower(name[1:]) + '_Type'
12+
self.typename = name + '_Type'
13+
self.argref = "" # set to "*" if arg to <type>_New should be pointer
1514

1615
def add(self, g):
1716
g.setselftype(self.objecttype, self.itselftype)
@@ -29,87 +28,76 @@ def generate(self):
2928
Output("staticforward PyTypeObject %s;", self.typename)
3029
Output()
3130

32-
Output("#define is_%sobject(x) ((x)->ob_type == %s)",
33-
self.name, self.typename)
31+
Output("#define %s_Check(x) ((x)->ob_type == &%s)",
32+
self.prefix, self.typename)
3433
Output()
3534

3635
Output("typedef struct {")
3736
IndentLevel()
38-
Output("OB_HEAD")
37+
Output("PyObject_HEAD")
3938
Output("%s ob_itself;", self.itselftype)
4039
DedentLevel()
4140
Output("} %s;", self.objecttype)
4241
Output()
4342

44-
Output("static %s *new%s(itself)", self.objecttype, self.objecttype)
43+
self.outputNew()
44+
45+
self.outputConvert()
46+
47+
self.outputDealloc()
48+
49+
GeneratorGroup.generate(self)
50+
51+
self.outputGetattr()
52+
53+
self.outputSetattr()
54+
55+
self.outputTypeObject()
56+
57+
OutHeader2("End object type " + self.name)
58+
59+
def outputNew(self):
60+
Output("static %s *%s_New(itself)", self.objecttype, self.prefix)
4561
IndentLevel()
46-
Output("%s itself;", self.itselftype)
62+
Output("const %s %sitself;", self.itselftype, self.argref)
4763
DedentLevel()
4864
OutLbrace()
4965
Output("%s *it;", self.objecttype)
5066
Output("it = PyObject_NEW(%s, &%s);", self.objecttype, self.typename)
5167
Output("if (it == NULL) return NULL;")
52-
Output("it->ob_itself = itself;")
68+
Output("it->ob_itself = %sitself;", self.argref)
5369
Output("return it;")
5470
OutRbrace()
5571
Output()
5672

73+
def outputConvert(self):
74+
Output("""\
75+
static int %(prefix)s_Convert(v, p_itself)
76+
PyObject *v;
77+
%(itselftype)s *p_itself;
78+
{
79+
if (v == NULL || !%(prefix)s_Check(v)) {
80+
PyErr_SetString(PyExc_TypeError, "%(name)s required");
81+
return 0;
82+
}
83+
*p_itself = ((%(objecttype)s *)v)->ob_itself;
84+
return 1;
85+
}
86+
""" % self.__dict__)
87+
88+
def outputDealloc(self):
5789
Output("static void %s_dealloc(self)", self.prefix)
5890
IndentLevel()
5991
Output("%s *self;", self.objecttype)
6092
DedentLevel()
6193
OutLbrace()
62-
## Output("if (self->ob_itself != NULL)")
63-
## OutLbrace()
64-
## self.outputFreeIt("self->ob_itself")
65-
## OutRbrace()
66-
Output("DEL(self);")
94+
self.outputFreeIt("self->ob_itself")
95+
Output("PyMem_DEL(self);")
6796
OutRbrace()
6897
Output()
6998

70-
## Output("static int %s_converter(p_itself, p_it)", self.prefix)
71-
## IndentLevel()
72-
## Output("%s *p_itself;", self.itselftype)
73-
## Output("%s **p_it;", self.objecttype)
74-
## DedentLevel()
75-
## OutLbrace()
76-
## Output("if (**p_it == NULL)")
77-
## OutLbrace()
78-
## Output("*p_it = new%s(*p_itself);", self.objecttype)
79-
## OutRbrace()
80-
## Output("else")
81-
## OutLbrace()
82-
## Output("*p_itself = (*p_it)->ob_itself;")
83-
## OutRbrace()
84-
## Output("return 1;")
85-
## OutRbrace()
86-
## Output()
87-
88-
GeneratorGroup.generate(self)
89-
90-
self.outputGetattr()
91-
92-
self.outputSetattr()
93-
94-
Output("static PyTypeObject %s = {", self.typename)
95-
IndentLevel()
96-
Output("PyObject_HEAD_INIT(&PyType_Type)")
97-
Output("0, /*ob_size*/")
98-
Output("\"%s\", /*tp_name*/", self.name)
99-
Output("sizeof(%s), /*tp_basicsize*/", self.objecttype)
100-
Output("0, /*tp_itemsize*/")
101-
Output("/* methods */")
102-
Output("(destructor) %s_dealloc, /*tp_dealloc*/", self.prefix)
103-
Output("0, /*tp_print*/")
104-
Output("(getattrfunc) %s_getattr, /*tp_getattr*/", self.prefix)
105-
Output("(setattrfunc) %s_setattr, /*tp_setattr*/", self.prefix)
106-
DedentLevel()
107-
Output("};")
108-
109-
OutHeader2("End object type " + self.name)
110-
11199
def outputFreeIt(self, name):
112-
Output("DEL(%s); /* XXX */", name)
100+
Output("/* Cleanup of %s goes here */", name)
113101

114102
def outputGetattr(self):
115103
Output("static PyObject *%s_getattr(self, name)", self.prefix)
@@ -123,8 +111,28 @@ def outputGetattr(self):
123111
Output()
124112

125113
def outputGetattrBody(self):
126-
Output("return findmethod(self, %s_methods, name);", self.prefix)
114+
self.outputGetattrHook()
115+
Output("return Py_FindMethod(%s_methods, (PyObject *)self, name);", self.prefix)
116+
117+
def outputGetattrHook(self):
118+
pass
127119

128120
def outputSetattr(self):
129121
Output("#define %s_setattr NULL", self.prefix)
130122
Output()
123+
124+
def outputTypeObject(self):
125+
Output("static PyTypeObject %s = {", self.typename)
126+
IndentLevel()
127+
Output("PyObject_HEAD_INIT(&PyType_Type)")
128+
Output("0, /*ob_size*/")
129+
Output("\"%s\", /*tp_name*/", self.name)
130+
Output("sizeof(%s), /*tp_basicsize*/", self.objecttype)
131+
Output("0, /*tp_itemsize*/")
132+
Output("/* methods */")
133+
Output("(destructor) %s_dealloc, /*tp_dealloc*/", self.prefix)
134+
Output("0, /*tp_print*/")
135+
Output("(getattrfunc) %s_getattr, /*tp_getattr*/", self.prefix)
136+
Output("(setattrfunc) %s_setattr, /*tp_setattr*/", self.prefix)
137+
DedentLevel()
138+
Output("};")

0 commit comments

Comments
 (0)