mirror of
https://github.com/python/cpython.git
synced 2024-11-26 19:34:19 +08:00
fully adapted to new naming scheme and added some features for AppleEvent generation
This commit is contained in:
parent
8cfc4bfb9d
commit
8d6c180c8d
@ -22,7 +22,7 @@ class Generator:
|
||||
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.objecttype = "PyObject" # Type of _self argument to function
|
||||
self.itselftype = None # Type of _self->ob_itself, if defined
|
||||
|
||||
def setprefix(self, prefix):
|
||||
@ -92,12 +92,13 @@ class Generator:
|
||||
def getargs(self):
|
||||
fmt = ""
|
||||
lst = ""
|
||||
sep = ",\n" + ' '*len("if (!PyArg_ParseTuple(")
|
||||
for arg in self.argumentList:
|
||||
if arg.flags == SelfMode:
|
||||
continue
|
||||
if arg.mode in (InMode, InOutMode):
|
||||
fmt = fmt + arg.getargsFormat()
|
||||
lst = lst + ", " + arg.getargsArgs()
|
||||
lst = lst + sep + arg.getargsArgs()
|
||||
Output("if (!PyArg_ParseTuple(_args, \"%s\"%s))", fmt, lst)
|
||||
IndentLevel()
|
||||
Output("return NULL;")
|
||||
@ -110,11 +111,12 @@ class Generator:
|
||||
|
||||
def callit(self):
|
||||
args = ""
|
||||
sep = ",\n" + ' '*len("%s = %s(" % (self.rv.name, self.name))
|
||||
for arg in self.argumentList:
|
||||
if arg is self.rv:
|
||||
continue
|
||||
s = arg.passArgument()
|
||||
if args: s = ", " + s
|
||||
if args: s = sep + s
|
||||
args = args + s
|
||||
if self.rv:
|
||||
Output("%s = %s(%s);",
|
||||
@ -129,12 +131,13 @@ class Generator:
|
||||
def returnvalue(self):
|
||||
fmt = ""
|
||||
lst = ""
|
||||
sep = ",\n" + ' '*len("return Py_BuildValue(")
|
||||
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()
|
||||
lst = lst + sep + arg.mkvalueArgs()
|
||||
if fmt == "":
|
||||
Output("Py_INCREF(Py_None);")
|
||||
Output("return Py_None;");
|
||||
|
@ -14,7 +14,7 @@ class GeneratorGroup:
|
||||
for g in self.generators:
|
||||
g.generate()
|
||||
Output()
|
||||
Output("static struct methodlist %s_methods[] = {", self.prefix)
|
||||
Output("static PyMethodDef %s_methods[] = {", self.prefix)
|
||||
IndentLevel()
|
||||
for g in self.generators:
|
||||
g.reference()
|
||||
|
@ -5,18 +5,20 @@ class Module(GeneratorGroup):
|
||||
|
||||
def __init__(self, name, prefix = None,
|
||||
includestuff = None,
|
||||
initstuff = None):
|
||||
initstuff = None,
|
||||
preinitstuff = None):
|
||||
GeneratorGroup.__init__(self, prefix or name)
|
||||
self.name = name
|
||||
self.includestuff = includestuff
|
||||
self.initstuff = initstuff
|
||||
self.preinitstuff = preinitstuff
|
||||
|
||||
def addobject(self, od):
|
||||
self.generators.append(od)
|
||||
|
||||
def generate(self):
|
||||
OutHeader1("Module " + self.name)
|
||||
Output("#include <Python.h>")
|
||||
Output("#include \"Python.h\"")
|
||||
Output()
|
||||
|
||||
if self.includestuff:
|
||||
@ -27,34 +29,38 @@ class Module(GeneratorGroup):
|
||||
|
||||
GeneratorGroup.generate(self)
|
||||
|
||||
if self.preinitstuff:
|
||||
Output()
|
||||
Output("%s", self.preinitstuff)
|
||||
|
||||
Output()
|
||||
Output("void init%s()", self.name)
|
||||
OutLbrace()
|
||||
Output("object *m;")
|
||||
Output("object *d;")
|
||||
Output("PyObject *m;")
|
||||
Output("PyObject *d;")
|
||||
Output()
|
||||
|
||||
if self.initstuff:
|
||||
Output("%s", self.initstuff)
|
||||
Output()
|
||||
|
||||
Output("m = initmodule(\"%s\", %s_methods);",
|
||||
Output("m = Py_InitModule(\"%s\", %s_methods);",
|
||||
self.name, self.prefix)
|
||||
Output("d = getmoduledict(m);")
|
||||
Output("d = PyModule_GetDict(m);")
|
||||
self.createModuleVariables()
|
||||
OutRbrace()
|
||||
OutHeader1("End module " + self.name)
|
||||
|
||||
def declareModuleVariables(self):
|
||||
self.errorname = self.prefix + "_Error"
|
||||
Output("static object *%s;", self.errorname)
|
||||
Output("static PyObject *%s;", self.errorname)
|
||||
|
||||
def createModuleVariables(self):
|
||||
Output("""if ((%s = newstringobject("%s.Error")) == NULL ||""",
|
||||
Output("""if ((%s = PyString_FromString("%s.Error")) == NULL ||""",
|
||||
self.errorname, self.name)
|
||||
Output(""" dictinsert(d, "Error", %s) != 0)""",
|
||||
Output(""" PyDict_SetItemString(d, "Error", %s) != 0)""",
|
||||
self.errorname)
|
||||
Output("""\tfatal("can't initialize %s.Error");""",
|
||||
Output("""Py_FatalError("can't initialize %s.Error");""",
|
||||
self.name)
|
||||
|
||||
|
||||
|
@ -9,9 +9,8 @@ class ObjectDefinition(GeneratorGroup):
|
||||
self.name = name
|
||||
self.itselftype = itselftype
|
||||
self.objecttype = name + 'Object'
|
||||
self.typename = self.prefix + '_' + \
|
||||
string.upper(name[:1]) + \
|
||||
string.lower(name[1:]) + '_Type'
|
||||
self.typename = name + '_Type'
|
||||
self.argref = "" # set to "*" if arg to <type>_New should be pointer
|
||||
|
||||
def add(self, g):
|
||||
g.setselftype(self.objecttype, self.itselftype)
|
||||
@ -29,61 +28,23 @@ class ObjectDefinition(GeneratorGroup):
|
||||
Output("staticforward PyTypeObject %s;", self.typename)
|
||||
Output()
|
||||
|
||||
Output("#define is_%sobject(x) ((x)->ob_type == %s)",
|
||||
self.name, self.typename)
|
||||
Output("#define %s_Check(x) ((x)->ob_type == &%s)",
|
||||
self.prefix, self.typename)
|
||||
Output()
|
||||
|
||||
Output("typedef struct {")
|
||||
IndentLevel()
|
||||
Output("OB_HEAD")
|
||||
Output("PyObject_HEAD")
|
||||
Output("%s ob_itself;", self.itselftype)
|
||||
DedentLevel()
|
||||
Output("} %s;", self.objecttype)
|
||||
Output()
|
||||
|
||||
Output("static %s *new%s(itself)", self.objecttype, self.objecttype)
|
||||
IndentLevel()
|
||||
Output("%s itself;", self.itselftype)
|
||||
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()
|
||||
self.outputNew()
|
||||
|
||||
Output("static void %s_dealloc(self)", self.prefix)
|
||||
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()
|
||||
self.outputConvert()
|
||||
|
||||
## 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()
|
||||
self.outputDealloc()
|
||||
|
||||
GeneratorGroup.generate(self)
|
||||
|
||||
@ -91,6 +52,76 @@ class ObjectDefinition(GeneratorGroup):
|
||||
|
||||
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)
|
||||
IndentLevel()
|
||||
Output("PyObject_HEAD_INIT(&PyType_Type)")
|
||||
@ -105,26 +136,3 @@ class ObjectDefinition(GeneratorGroup):
|
||||
Output("(setattrfunc) %s_setattr, /*tp_setattr*/", self.prefix)
|
||||
DedentLevel()
|
||||
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()
|
||||
|
@ -39,6 +39,9 @@ class Type:
|
||||
"""
|
||||
Output("%s %s;", self.typeName, name)
|
||||
|
||||
def getargs(self):
|
||||
return self.getargsFormat(), self.getargsArgs()
|
||||
|
||||
def getargsFormat(self):
|
||||
"""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)
|
||||
|
||||
def mkvalue(self):
|
||||
return self.mkvalueFormat(), self.mkvalueArgs()
|
||||
|
||||
def mkvalueFormat(self):
|
||||
"""Return the format for this type for use with mkvalue().
|
||||
|
||||
@ -113,8 +119,8 @@ char = Type("char", "c")
|
||||
|
||||
|
||||
# Some Python related types.
|
||||
objectptr = Type("object*", "O")
|
||||
stringobjectptr = Type("stringobject*", "S")
|
||||
objectptr = Type("PyObject*", "O")
|
||||
stringobjectptr = Type("PyStringObject*", "S")
|
||||
# Etc.
|
||||
|
||||
|
||||
@ -125,7 +131,7 @@ class SizedInputBuffer:
|
||||
|
||||
"Sized input buffer -- buffer size is an input parameter"
|
||||
|
||||
def __init__(self, size):
|
||||
def __init__(self, size = ''):
|
||||
self.size = size
|
||||
|
||||
def declare(self, name):
|
||||
@ -136,7 +142,7 @@ class SizedInputBuffer:
|
||||
return "s#"
|
||||
|
||||
def getargsArgs(self, name):
|
||||
return "%s, %s__len__" % (name, name)
|
||||
return "&%s, &%s__len__" % (name, name)
|
||||
|
||||
def getargsCheck(self, name):
|
||||
pass
|
||||
@ -151,14 +157,28 @@ class FixedInputBuffer(SizedInputBuffer):
|
||||
|
||||
def getargsCheck(self, name):
|
||||
Output("if (%s__len__ != %s)", name, str(self.size))
|
||||
IndentLevel()
|
||||
Output('err_setstr(TypeError, "bad string length");')
|
||||
DedentLevel()
|
||||
OutLbrace()
|
||||
Output('PyErr_SetString(PyExc_TypeError, "bad string length");')
|
||||
Output('return NULL;')
|
||||
OutRbrace()
|
||||
|
||||
def passInput(self, 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:
|
||||
|
||||
"Sized output buffer -- buffer size is an input-output parameter"
|
||||
@ -183,6 +203,23 @@ class SizedOutputBuffer:
|
||||
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:
|
||||
|
||||
"Fixed output buffer -- buffer size is a constant"
|
||||
@ -266,7 +303,7 @@ class StructureByValue:
|
||||
def getargsCheck(self, name):
|
||||
Output("if (%s__len__ != sizeof %s)", name, name)
|
||||
IndentLevel()
|
||||
Output('err_setstr(TypeError, "bad structure length");')
|
||||
Output('PyErr_SetString(PyExc_TypeError, "bad structure length");')
|
||||
DedentLevel()
|
||||
Output("memcpy(&%s, %s__str__, %s__len__);",
|
||||
name, name, name)
|
||||
|
Loading…
Reference in New Issue
Block a user