diff options
Diffstat (limited to 'Tools/bgen/bgen/bgenObjectDefinition.py')
-rw-r--r-- | Tools/bgen/bgen/bgenObjectDefinition.py | 98 |
1 files changed, 48 insertions, 50 deletions
diff --git a/Tools/bgen/bgen/bgenObjectDefinition.py b/Tools/bgen/bgen/bgenObjectDefinition.py index 9c205e392b..72e2678bb0 100644 --- a/Tools/bgen/bgen/bgenObjectDefinition.py +++ b/Tools/bgen/bgen/bgenObjectDefinition.py @@ -9,14 +9,14 @@ class ObjectDefinition(GeneratorGroup): def __init__(self, name, prefix, itselftype): """ObjectDefinition constructor. May be extended, but do not override. - + - name: the object's official name, e.g. 'SndChannel'. - prefix: the prefix used for the object's functions and data, e.g. 'SndCh'. - itselftype: the C type actually contained in the object, e.g. 'SndChannelPtr'. - + XXX For official Python data types, rules for the 'Py' prefix are a problem. """ - + GeneratorGroup.__init__(self, prefix or name) self.name = name self.itselftype = itselftype @@ -35,7 +35,7 @@ class ObjectDefinition(GeneratorGroup): def reference(self): # In case we are referenced from a module pass - + def setmodulename(self, name): self.modulename = name @@ -58,7 +58,7 @@ class ObjectDefinition(GeneratorGroup): Output("} %s;", self.objecttype) self.outputNew() - + self.outputConvert() self.outputDealloc() @@ -71,19 +71,19 @@ class ObjectDefinition(GeneratorGroup): self.outputGetattr() self.outputSetattr() - + self.outputCompare() - + self.outputRepr() - + self.outputHash() - + self.outputPEP253Hooks() - + self.outputTypeObject() OutHeader2("End object type " + self.name) - + def outputMethodChain(self): Output("%sPyMethodChain %s_chain = { %s_methods, %s };", self.static, self.prefix, self.prefix, self.basechain) @@ -108,10 +108,10 @@ class ObjectDefinition(GeneratorGroup): def outputInitStructMembers(self): Output("it->ob_itself = %sitself;", self.argref) - + def outputCheckNewArg(self): - "Override this method to apply additional checks/conversions" - + "Override this method to apply additional checks/conversions" + def outputConvert(self): Output("%sint %s_Convert(PyObject *v, %s *p_itself)", self.static, self.prefix, self.itselftype) @@ -206,7 +206,7 @@ class ObjectDefinition(GeneratorGroup): Output("(hashfunc) %s_hash, /*tp_hash*/", self.prefix) DedentLevel() Output("};") - + def outputTypeObjectInitializer(self): Output("""%s.ob_type = &PyType_Type;""", self.typename) if self.basetype: @@ -220,10 +220,10 @@ class ObjectDefinition(GeneratorGroup): def outputPEP253Hooks(self): pass - + class PEP252Mixin: getsetlist = [] - + def assertions(self): # Check that various things aren't overridden. If they are it could # signify a bgen-client that has been partially converted to PEP252. @@ -232,21 +232,21 @@ class PEP252Mixin: assert self.outputGetattrBody == None assert self.outputGetattrHook == None assert self.basechain == "NULL" - + def outputGetattr(self): pass - + outputGetattrBody = None outputGetattrHook = None def outputSetattr(self): pass - + def outputMethodChain(self): # This is a good place to output the getters and setters self.outputGetSetList() - + def outputHook(self, name): methodname = "outputHook_" + name if hasattr(self, methodname): @@ -254,7 +254,7 @@ class PEP252Mixin: func() else: Output("0, /*%s*/", name) - + def outputTypeObject(self): sf = self.static and "static " Output() @@ -268,7 +268,7 @@ class PEP252Mixin: 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*/") @@ -276,17 +276,17 @@ class PEP252Mixin: Output("(setattrfunc)0, /*tp_setattr*/") Output("(cmpfunc) %s_compare, /*tp_compare*/", self.prefix) Output("(reprfunc) %s_repr, /*tp_repr*/", self.prefix) - + Output("(PyNumberMethods *)0, /* tp_as_number */") Output("(PySequenceMethods *)0, /* tp_as_sequence */") Output("(PyMappingMethods *)0, /* tp_as_mapping */") - + Output("(hashfunc) %s_hash, /*tp_hash*/", self.prefix) self.outputHook("tp_call") Output("0, /*tp_str*/") Output("PyObject_GenericGetAttr, /*tp_getattro*/") Output("PyObject_GenericSetAttr, /*tp_setattro */") - + self.outputHook("tp_as_buffer") Output("%s, /* tp_flags */", self.tp_flags) self.outputHook("tp_doc") @@ -310,7 +310,7 @@ class PEP252Mixin: self.outputHook("tp_free") DedentLevel() Output("};") - + def outputGetSetList(self): if self.getsetlist: for name, get, set, doc in self.getsetlist: @@ -324,7 +324,7 @@ class PEP252Mixin: else: Output("#define %s_set_%s NULL", self.prefix, name) Output() - + Output("static PyGetSetDef %s_getsetlist[] = {", self.prefix) IndentLevel() for name, get, set, doc in self.getsetlist: @@ -332,7 +332,7 @@ class PEP252Mixin: doc = '"' + doc + '"' else: doc = "NULL" - Output("{\"%s\", (getter)%s_get_%s, (setter)%s_set_%s, %s},", + Output("{\"%s\", (getter)%s_get_%s, (setter)%s_set_%s, %s},", name, self.prefix, name, self.prefix, name, doc) Output("{NULL, NULL, NULL, NULL},") DedentLevel() @@ -340,7 +340,7 @@ class PEP252Mixin: else: Output("#define %s_getsetlist NULL", self.prefix) Output() - + def outputGetter(self, name, code): Output("static PyObject *%s_get_%s(%s *self, void *closure)", self.prefix, name, self.objecttype) @@ -348,7 +348,7 @@ class PEP252Mixin: Output(code) OutRbrace() Output() - + def outputSetter(self, name, code): Output("static int %s_set_%s(%s *self, PyObject *v, void *closure)", self.prefix, name, self.objecttype) @@ -357,24 +357,24 @@ class PEP252Mixin: Output("return 0;") OutRbrace() Output() - + class PEP253Mixin(PEP252Mixin): tp_flags = "Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE" - + def outputHook_tp_init(self): Output("%s_tp_init, /* tp_init */", self.prefix) - + def outputHook_tp_alloc(self): Output("%s_tp_alloc, /* tp_alloc */", self.prefix) - + def outputHook_tp_new(self): Output("%s_tp_new, /* tp_new */", self.prefix) - + def outputHook_tp_free(self): Output("%s_tp_free, /* tp_free */", self.prefix) - + output_tp_initBody = None - + def output_tp_init(self): if self.output_tp_initBody: Output("static int %s_tp_init(PyObject *self, PyObject *args, PyObject *kwds)", self.prefix) @@ -384,9 +384,9 @@ class PEP253Mixin(PEP252Mixin): else: Output("#define %s_tp_init 0", self.prefix) Output() - + output_tp_allocBody = None - + def output_tp_alloc(self): if self.output_tp_allocBody: Output("static PyObject *%s_tp_alloc(PyTypeObject *type, int nitems)", @@ -397,7 +397,7 @@ class PEP253Mixin(PEP252Mixin): else: Output("#define %s_tp_alloc PyType_GenericAlloc", self.prefix) Output() - + def output_tp_newBody(self): Output("PyObject *self;"); Output("%s itself;", self.itselftype); @@ -408,7 +408,7 @@ class PEP253Mixin(PEP252Mixin): Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;") Output("((%s *)self)->ob_itself = itself;", self.objecttype) Output("return self;") - + def output_tp_new(self): if self.output_tp_newBody: Output("static PyObject *%s_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)", self.prefix) @@ -418,9 +418,9 @@ class PEP253Mixin(PEP252Mixin): else: Output("#define %s_tp_new PyType_GenericNew", self.prefix) Output() - + output_tp_freeBody = None - + def output_tp_free(self): if self.output_tp_freeBody: Output("static void %s_tp_free(PyObject *self)", self.prefix) @@ -430,7 +430,7 @@ class PEP253Mixin(PEP252Mixin): else: Output("#define %s_tp_free PyObject_Del", self.prefix) Output() - + def outputPEP253Hooks(self): self.output_tp_init() self.output_tp_alloc() @@ -439,7 +439,7 @@ class PEP253Mixin(PEP252Mixin): class GlobalObjectDefinition(ObjectDefinition): """Like ObjectDefinition but exports some parts. - + XXX Should also somehow generate a .h file for them. """ @@ -453,7 +453,7 @@ class ObjectIdentityMixin: be returned by library calls and it is difficult (or impossible) to find the corresponding Python objects. With this you can create Python object wrappers on the fly""" - + def outputCompare(self): Output() Output("static int %s_compare(%s *self, %s *other)", self.prefix, self.objecttype, @@ -475,12 +475,10 @@ class ObjectIdentityMixin: Output("if( v > w ) return 1;") Output("return 0;") OutRbrace() - + def outputHash(self): Output() Output("static long %s_hash(%s *self)", self.prefix, self.objecttype) OutLbrace() Output("return (long)self->ob_itself;") OutRbrace() - - |