fully adapted to new naming scheme and added some features for AppleEvent generation

This commit is contained in:
Guido van Rossum 1995-01-18 23:46:59 +00:00
parent 8cfc4bfb9d
commit 8d6c180c8d
5 changed files with 151 additions and 97 deletions

View File

@ -22,7 +22,7 @@ class Generator:
def __init__(self, *argumentList): def __init__(self, *argumentList):
apply(self.parseArguments, argumentList) apply(self.parseArguments, argumentList)
self.prefix = "XXX" # Will be changed by setprefix() call self.prefix = "XXX" # Will be changed by setprefix() call
self.objecttype = "object" # Type of _self argument to function self.objecttype = "PyObject" # Type of _self argument to function
self.itselftype = None # Type of _self->ob_itself, if defined self.itselftype = None # Type of _self->ob_itself, if defined
def setprefix(self, prefix): def setprefix(self, prefix):
@ -92,12 +92,13 @@ class Generator:
def getargs(self): def getargs(self):
fmt = "" fmt = ""
lst = "" lst = ""
sep = ",\n" + ' '*len("if (!PyArg_ParseTuple(")
for arg in self.argumentList: for arg in self.argumentList:
if arg.flags == SelfMode: if arg.flags == SelfMode:
continue continue
if arg.mode in (InMode, InOutMode): if arg.mode in (InMode, InOutMode):
fmt = fmt + arg.getargsFormat() fmt = fmt + arg.getargsFormat()
lst = lst + ", " + arg.getargsArgs() lst = lst + sep + arg.getargsArgs()
Output("if (!PyArg_ParseTuple(_args, \"%s\"%s))", fmt, lst) Output("if (!PyArg_ParseTuple(_args, \"%s\"%s))", fmt, lst)
IndentLevel() IndentLevel()
Output("return NULL;") Output("return NULL;")
@ -110,11 +111,12 @@ class Generator:
def callit(self): def callit(self):
args = "" args = ""
sep = ",\n" + ' '*len("%s = %s(" % (self.rv.name, self.name))
for arg in self.argumentList: for arg in self.argumentList:
if arg is self.rv: if arg is self.rv:
continue continue
s = arg.passArgument() s = arg.passArgument()
if args: s = ", " + s if args: s = sep + s
args = args + s args = args + s
if self.rv: if self.rv:
Output("%s = %s(%s);", Output("%s = %s(%s);",
@ -129,12 +131,13 @@ class Generator:
def returnvalue(self): def returnvalue(self):
fmt = "" fmt = ""
lst = "" lst = ""
sep = ",\n" + ' '*len("return Py_BuildValue(")
for arg in self.argumentList: for arg in self.argumentList:
if not arg: continue if not arg: continue
if arg.flags == ErrorMode: continue if arg.flags == ErrorMode: continue
if arg.mode in (OutMode, InOutMode): if arg.mode in (OutMode, InOutMode):
fmt = fmt + arg.mkvalueFormat() fmt = fmt + arg.mkvalueFormat()
lst = lst + ", " + arg.mkvalueArgs() lst = lst + sep + arg.mkvalueArgs()
if fmt == "": if fmt == "":
Output("Py_INCREF(Py_None);") Output("Py_INCREF(Py_None);")
Output("return Py_None;"); Output("return Py_None;");

View File

@ -14,7 +14,7 @@ class GeneratorGroup:
for g in self.generators: for g in self.generators:
g.generate() g.generate()
Output() Output()
Output("static struct methodlist %s_methods[] = {", self.prefix) Output("static PyMethodDef %s_methods[] = {", self.prefix)
IndentLevel() IndentLevel()
for g in self.generators: for g in self.generators:
g.reference() g.reference()

View File

@ -5,18 +5,20 @@ class Module(GeneratorGroup):
def __init__(self, name, prefix = None, def __init__(self, name, prefix = None,
includestuff = None, includestuff = None,
initstuff = None): initstuff = None,
preinitstuff = None):
GeneratorGroup.__init__(self, prefix or name) GeneratorGroup.__init__(self, prefix or name)
self.name = name self.name = name
self.includestuff = includestuff self.includestuff = includestuff
self.initstuff = initstuff self.initstuff = initstuff
self.preinitstuff = preinitstuff
def addobject(self, od): def addobject(self, od):
self.generators.append(od) self.generators.append(od)
def generate(self): def generate(self):
OutHeader1("Module " + self.name) OutHeader1("Module " + self.name)
Output("#include <Python.h>") Output("#include \"Python.h\"")
Output() Output()
if self.includestuff: if self.includestuff:
@ -26,36 +28,40 @@ class Module(GeneratorGroup):
self.declareModuleVariables() self.declareModuleVariables()
GeneratorGroup.generate(self) GeneratorGroup.generate(self)
if self.preinitstuff:
Output()
Output("%s", self.preinitstuff)
Output() Output()
Output("void init%s()", self.name) Output("void init%s()", self.name)
OutLbrace() OutLbrace()
Output("object *m;") Output("PyObject *m;")
Output("object *d;") Output("PyObject *d;")
Output() Output()
if self.initstuff: if self.initstuff:
Output("%s", self.initstuff) Output("%s", self.initstuff)
Output() Output()
Output("m = initmodule(\"%s\", %s_methods);", Output("m = Py_InitModule(\"%s\", %s_methods);",
self.name, self.prefix) self.name, self.prefix)
Output("d = getmoduledict(m);") Output("d = PyModule_GetDict(m);")
self.createModuleVariables() self.createModuleVariables()
OutRbrace() OutRbrace()
OutHeader1("End module " + self.name) OutHeader1("End module " + self.name)
def declareModuleVariables(self): def declareModuleVariables(self):
self.errorname = self.prefix + "_Error" self.errorname = self.prefix + "_Error"
Output("static object *%s;", self.errorname) Output("static PyObject *%s;", self.errorname)
def createModuleVariables(self): def createModuleVariables(self):
Output("""if ((%s = newstringobject("%s.Error")) == NULL ||""", Output("""if ((%s = PyString_FromString("%s.Error")) == NULL ||""",
self.errorname, self.name) self.errorname, self.name)
Output(""" dictinsert(d, "Error", %s) != 0)""", Output(""" PyDict_SetItemString(d, "Error", %s) != 0)""",
self.errorname) self.errorname)
Output("""\tfatal("can't initialize %s.Error");""", Output("""Py_FatalError("can't initialize %s.Error");""",
self.name) self.name)
def _test(): def _test():

View File

@ -9,9 +9,8 @@ class ObjectDefinition(GeneratorGroup):
self.name = name self.name = name
self.itselftype = itselftype self.itselftype = itselftype
self.objecttype = name + 'Object' self.objecttype = name + 'Object'
self.typename = self.prefix + '_' + \ self.typename = name + '_Type'
string.upper(name[:1]) + \ self.argref = "" # set to "*" if arg to <type>_New should be pointer
string.lower(name[1:]) + '_Type'
def add(self, g): def add(self, g):
g.setselftype(self.objecttype, self.itselftype) g.setselftype(self.objecttype, self.itselftype)
@ -29,68 +28,100 @@ class ObjectDefinition(GeneratorGroup):
Output("staticforward PyTypeObject %s;", self.typename) Output("staticforward PyTypeObject %s;", self.typename)
Output() Output()
Output("#define is_%sobject(x) ((x)->ob_type == %s)", Output("#define %s_Check(x) ((x)->ob_type == &%s)",
self.name, self.typename) self.prefix, self.typename)
Output() Output()
Output("typedef struct {") Output("typedef struct {")
IndentLevel() IndentLevel()
Output("OB_HEAD") Output("PyObject_HEAD")
Output("%s ob_itself;", self.itselftype) Output("%s ob_itself;", self.itselftype)
DedentLevel() DedentLevel()
Output("} %s;", self.objecttype) Output("} %s;", self.objecttype)
Output() Output()
Output("static %s *new%s(itself)", self.objecttype, self.objecttype) self.outputNew()
IndentLevel()
Output("%s itself;", self.itselftype) self.outputConvert()
DedentLevel()
OutLbrace()
Output("%s *it;", self.objecttype)
Output("it = PyObject_NEW(%s, &%s);", self.objecttype, self.typename)
Output("if (it == NULL) return NULL;")
Output("it->ob_itself = itself;")
Output("return it;")
OutRbrace()
Output()
Output("static void %s_dealloc(self)", self.prefix) self.outputDealloc()
IndentLevel()
Output("%s *self;", self.objecttype)
DedentLevel()
OutLbrace()
## Output("if (self->ob_itself != NULL)")
## OutLbrace()
## self.outputFreeIt("self->ob_itself")
## OutRbrace()
Output("DEL(self);")
OutRbrace()
Output()
## Output("static int %s_converter(p_itself, p_it)", self.prefix)
## IndentLevel()
## Output("%s *p_itself;", self.itselftype)
## Output("%s **p_it;", self.objecttype)
## DedentLevel()
## OutLbrace()
## Output("if (**p_it == NULL)")
## OutLbrace()
## Output("*p_it = new%s(*p_itself);", self.objecttype)
## OutRbrace()
## Output("else")
## OutLbrace()
## Output("*p_itself = (*p_it)->ob_itself;")
## OutRbrace()
## Output("return 1;")
## OutRbrace()
## Output()
GeneratorGroup.generate(self) GeneratorGroup.generate(self)
self.outputGetattr() self.outputGetattr()
self.outputSetattr() self.outputSetattr()
self.outputTypeObject()
OutHeader2("End object type " + self.name)
def outputNew(self):
Output("static %s *%s_New(itself)", self.objecttype, self.prefix)
IndentLevel()
Output("const %s %sitself;", self.itselftype, self.argref)
DedentLevel()
OutLbrace()
Output("%s *it;", self.objecttype)
Output("it = PyObject_NEW(%s, &%s);", self.objecttype, self.typename)
Output("if (it == NULL) return NULL;")
Output("it->ob_itself = %sitself;", self.argref)
Output("return it;")
OutRbrace()
Output()
def outputConvert(self):
Output("""\
static int %(prefix)s_Convert(v, p_itself)
PyObject *v;
%(itselftype)s *p_itself;
{
if (v == NULL || !%(prefix)s_Check(v)) {
PyErr_SetString(PyExc_TypeError, "%(name)s required");
return 0;
}
*p_itself = ((%(objecttype)s *)v)->ob_itself;
return 1;
}
""" % self.__dict__)
def outputDealloc(self):
Output("static void %s_dealloc(self)", self.prefix)
IndentLevel()
Output("%s *self;", self.objecttype)
DedentLevel()
OutLbrace()
self.outputFreeIt("self->ob_itself")
Output("PyMem_DEL(self);")
OutRbrace()
Output()
def outputFreeIt(self, name):
Output("/* Cleanup of %s goes here */", name)
def outputGetattr(self):
Output("static PyObject *%s_getattr(self, name)", self.prefix)
IndentLevel()
Output("%s *self;", self.objecttype)
Output("char *name;")
DedentLevel()
OutLbrace()
self.outputGetattrBody()
OutRbrace()
Output()
def outputGetattrBody(self):
self.outputGetattrHook()
Output("return Py_FindMethod(%s_methods, (PyObject *)self, name);", self.prefix)
def outputGetattrHook(self):
pass
def outputSetattr(self):
Output("#define %s_setattr NULL", self.prefix)
Output()
def outputTypeObject(self):
Output("static PyTypeObject %s = {", self.typename) Output("static PyTypeObject %s = {", self.typename)
IndentLevel() IndentLevel()
Output("PyObject_HEAD_INIT(&PyType_Type)") Output("PyObject_HEAD_INIT(&PyType_Type)")
@ -105,26 +136,3 @@ class ObjectDefinition(GeneratorGroup):
Output("(setattrfunc) %s_setattr, /*tp_setattr*/", self.prefix) Output("(setattrfunc) %s_setattr, /*tp_setattr*/", self.prefix)
DedentLevel() DedentLevel()
Output("};") Output("};")
OutHeader2("End object type " + self.name)
def outputFreeIt(self, name):
Output("DEL(%s); /* XXX */", name)
def outputGetattr(self):
Output("static PyObject *%s_getattr(self, name)", self.prefix)
IndentLevel()
Output("%s *self;", self.objecttype)
Output("char *name;")
DedentLevel()
OutLbrace()
self.outputGetattrBody()
OutRbrace()
Output()
def outputGetattrBody(self):
Output("return findmethod(self, %s_methods, name);", self.prefix)
def outputSetattr(self):
Output("#define %s_setattr NULL", self.prefix)
Output()

View File

@ -39,6 +39,9 @@ class Type:
""" """
Output("%s %s;", self.typeName, name) Output("%s %s;", self.typeName, name)
def getargs(self):
return self.getargsFormat(), self.getargsArgs()
def getargsFormat(self): def getargsFormat(self):
"""Return the format for this type for use with [new]getargs(). """Return the format for this type for use with [new]getargs().
@ -85,6 +88,9 @@ class Type:
""" """
Output("/* XXX no err check for %s %s */", self.typeName, name) Output("/* XXX no err check for %s %s */", self.typeName, name)
def mkvalue(self):
return self.mkvalueFormat(), self.mkvalueArgs()
def mkvalueFormat(self): def mkvalueFormat(self):
"""Return the format for this type for use with mkvalue(). """Return the format for this type for use with mkvalue().
@ -113,8 +119,8 @@ char = Type("char", "c")
# Some Python related types. # Some Python related types.
objectptr = Type("object*", "O") objectptr = Type("PyObject*", "O")
stringobjectptr = Type("stringobject*", "S") stringobjectptr = Type("PyStringObject*", "S")
# Etc. # Etc.
@ -125,7 +131,7 @@ class SizedInputBuffer:
"Sized input buffer -- buffer size is an input parameter" "Sized input buffer -- buffer size is an input parameter"
def __init__(self, size): def __init__(self, size = ''):
self.size = size self.size = size
def declare(self, name): def declare(self, name):
@ -136,7 +142,7 @@ class SizedInputBuffer:
return "s#" return "s#"
def getargsArgs(self, name): def getargsArgs(self, name):
return "%s, %s__len__" % (name, name) return "&%s, &%s__len__" % (name, name)
def getargsCheck(self, name): def getargsCheck(self, name):
pass pass
@ -151,14 +157,28 @@ class FixedInputBuffer(SizedInputBuffer):
def getargsCheck(self, name): def getargsCheck(self, name):
Output("if (%s__len__ != %s)", name, str(self.size)) Output("if (%s__len__ != %s)", name, str(self.size))
IndentLevel() OutLbrace()
Output('err_setstr(TypeError, "bad string length");') Output('PyErr_SetString(PyExc_TypeError, "bad string length");')
DedentLevel() Output('return NULL;')
OutRbrace()
def passInput(self, name): def passInput(self, name):
return name return name
class RecordBuffer(FixedInputBuffer):
"Like fixed input buffer -- but declared as a record type pointer"
def __init__(self, type):
self.type = type
self.size = "sizeof(%s)" % type
def declare(self, name):
Output("%s *%s;", self.type, name)
Output("int %s__len__;", name)
class SizedOutputBuffer: class SizedOutputBuffer:
"Sized output buffer -- buffer size is an input-output parameter" "Sized output buffer -- buffer size is an input-output parameter"
@ -183,6 +203,23 @@ class SizedOutputBuffer:
return "%s, %s__len__" % (name, name) return "%s, %s__len__" % (name, name)
class VarSizedOutputBuffer(SizedOutputBuffer):
"Variable Sized output buffer -- buffer size is an input and an output parameter"
def getargsFormat(self):
return "i"
def getargsArgs(self, name):
return "&%s__len__" % name
def getargsCheck(self, name):
pass
def passOutput(self, name):
return "%s, %s__len__, &%s__len__" % (name, name, name)
class FixedOutputBuffer: class FixedOutputBuffer:
"Fixed output buffer -- buffer size is a constant" "Fixed output buffer -- buffer size is a constant"
@ -266,7 +303,7 @@ class StructureByValue:
def getargsCheck(self, name): def getargsCheck(self, name):
Output("if (%s__len__ != sizeof %s)", name, name) Output("if (%s__len__ != sizeof %s)", name, name)
IndentLevel() IndentLevel()
Output('err_setstr(TypeError, "bad structure length");') Output('PyErr_SetString(PyExc_TypeError, "bad structure length");')
DedentLevel() DedentLevel()
Output("memcpy(&%s, %s__str__, %s__len__);", Output("memcpy(&%s, %s__str__, %s__len__);",
name, name, name) name, name, name)