summaryrefslogtreecommitdiffstats
path: root/Tools/bgen
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1995-01-18 23:46:59 (GMT)
committerGuido van Rossum <guido@python.org>1995-01-18 23:46:59 (GMT)
commit8d6c180c8d32d5c3126a1d9a30512cdb69343c64 (patch)
tree24bd3efb6c031b60880410bc4a885b67403ed8ba /Tools/bgen
parent8cfc4bfb9d2c784c0901f8d31b24c49ab905b9ae (diff)
downloadcpython-8d6c180c8d32d5c3126a1d9a30512cdb69343c64.zip
cpython-8d6c180c8d32d5c3126a1d9a30512cdb69343c64.tar.gz
cpython-8d6c180c8d32d5c3126a1d9a30512cdb69343c64.tar.bz2
fully adapted to new naming scheme and added some features for AppleEvent generation
Diffstat (limited to 'Tools/bgen')
-rw-r--r--Tools/bgen/bgen/bgenGenerator.py11
-rw-r--r--Tools/bgen/bgen/bgenGeneratorGroup.py2
-rw-r--r--Tools/bgen/bgen/bgenModule.py32
-rw-r--r--Tools/bgen/bgen/bgenObjectDefinition.py122
-rw-r--r--Tools/bgen/bgen/bgenType.py53
5 files changed, 137 insertions, 83 deletions
diff --git a/Tools/bgen/bgen/bgenGenerator.py b/Tools/bgen/bgen/bgenGenerator.py
index ce38763..d2db9f1 100644
--- a/Tools/bgen/bgen/bgenGenerator.py
+++ b/Tools/bgen/bgen/bgenGenerator.py
@@ -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;");
diff --git a/Tools/bgen/bgen/bgenGeneratorGroup.py b/Tools/bgen/bgen/bgenGeneratorGroup.py
index b8cd72e..294828f 100644
--- a/Tools/bgen/bgen/bgenGeneratorGroup.py
+++ b/Tools/bgen/bgen/bgenGeneratorGroup.py
@@ -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()
diff --git a/Tools/bgen/bgen/bgenModule.py b/Tools/bgen/bgen/bgenModule.py
index 8f23004..8824ee9 100644
--- a/Tools/bgen/bgen/bgenModule.py
+++ b/Tools/bgen/bgen/bgenModule.py
@@ -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:
@@ -26,36 +28,40 @@ class Module(GeneratorGroup):
self.declareModuleVariables()
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 ||""",
- self.errorname, self.name)
- Output(""" dictinsert(d, "Error", %s) != 0)""",
- self.errorname)
- Output("""\tfatal("can't initialize %s.Error");""",
- self.name)
+ Output("""if ((%s = PyString_FromString("%s.Error")) == NULL ||""",
+ self.errorname, self.name)
+ Output(""" PyDict_SetItemString(d, "Error", %s) != 0)""",
+ self.errorname)
+ Output("""Py_FatalError("can't initialize %s.Error");""",
+ self.name)
def _test():
diff --git a/Tools/bgen/bgen/bgenObjectDefinition.py b/Tools/bgen/bgen/bgenObjectDefinition.py
index 6573fd4..d84dffe 100644
--- a/Tools/bgen/bgen/bgenObjectDefinition.py
+++ b/Tools/bgen/bgen/bgenObjectDefinition.py
@@ -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,87 +28,76 @@ 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)
+ self.outputNew()
+
+ self.outputConvert()
+
+ self.outputDealloc()
+
+ GeneratorGroup.generate(self)
+
+ self.outputGetattr()
+
+ 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("%s itself;", self.itselftype)
+ 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 = itself;")
+ 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()
-## Output("if (self->ob_itself != NULL)")
-## OutLbrace()
-## self.outputFreeIt("self->ob_itself")
-## OutRbrace()
- Output("DEL(self);")
+ self.outputFreeIt("self->ob_itself")
+ Output("PyMem_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)
-
- self.outputGetattr()
-
- self.outputSetattr()
-
- Output("static PyTypeObject %s = {", self.typename)
- IndentLevel()
- Output("PyObject_HEAD_INIT(&PyType_Type)")
- Output("0, /*ob_size*/")
- Output("\"%s\", /*tp_name*/", self.name)
- Output("sizeof(%s), /*tp_basicsize*/", self.objecttype)
- Output("0, /*tp_itemsize*/")
- Output("/* methods */")
- Output("(destructor) %s_dealloc, /*tp_dealloc*/", self.prefix)
- Output("0, /*tp_print*/")
- Output("(getattrfunc) %s_getattr, /*tp_getattr*/", self.prefix)
- 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)
+ Output("/* Cleanup of %s goes here */", name)
def outputGetattr(self):
Output("static PyObject *%s_getattr(self, name)", self.prefix)
@@ -123,8 +111,28 @@ class ObjectDefinition(GeneratorGroup):
Output()
def outputGetattrBody(self):
- Output("return findmethod(self, %s_methods, name);", self.prefix)
+ 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)")
+ Output("0, /*ob_size*/")
+ Output("\"%s\", /*tp_name*/", self.name)
+ Output("sizeof(%s), /*tp_basicsize*/", self.objecttype)
+ Output("0, /*tp_itemsize*/")
+ Output("/* methods */")
+ Output("(destructor) %s_dealloc, /*tp_dealloc*/", self.prefix)
+ Output("0, /*tp_print*/")
+ Output("(getattrfunc) %s_getattr, /*tp_getattr*/", self.prefix)
+ Output("(setattrfunc) %s_setattr, /*tp_setattr*/", self.prefix)
+ DedentLevel()
+ Output("};")
diff --git a/Tools/bgen/bgen/bgenType.py b/Tools/bgen/bgen/bgenType.py
index 66f78eb..a1a4ba5 100644
--- a/Tools/bgen/bgen/bgenType.py
+++ b/Tools/bgen/bgen/bgenType.py
@@ -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)