From 228f89638d6ea36bd18035920d4b3398d7f496a8 Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 19 Dec 2019 17:55:12 +0100 Subject: [PATCH] Python: change generation of callback functions for class or static methods --- src/Base/TypePyImp.cpp | 10 ++-- .../templateClassPyExport.py | 56 ++++++++++++++++--- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/Base/TypePyImp.cpp b/src/Base/TypePyImp.cpp index d103fdaf3d..69806d32ae 100644 --- a/src/Base/TypePyImp.cpp +++ b/src/Base/TypePyImp.cpp @@ -37,7 +37,7 @@ std::string TypePy::representation(void) const return str.str(); } -PyObject* TypePy::staticCallback_fromName (PyObject * /*self*/, PyObject *args) +PyObject* TypePy::fromName (PyObject *args) { const char *name; if (!PyArg_ParseTuple(args, "s", &name)) @@ -47,7 +47,7 @@ PyObject* TypePy::staticCallback_fromName (PyObject * /*self*/, PyObject *args) return new TypePy(new Base::Type(type)); } -PyObject* TypePy::staticCallback_fromKey (PyObject * /*self*/, PyObject *args) +PyObject* TypePy::fromKey (PyObject *args) { unsigned int index; if (!PyArg_ParseTuple(args, "I", &index)) @@ -57,7 +57,7 @@ PyObject* TypePy::staticCallback_fromKey (PyObject * /*self*/, PyObject *args) return new TypePy(new Base::Type(type)); } -PyObject* TypePy::staticCallback_getNumTypes (PyObject * /*self*/, PyObject *args) +PyObject* TypePy::getNumTypes (PyObject *args) { if (!PyArg_ParseTuple(args, "")) return nullptr; @@ -66,7 +66,7 @@ PyObject* TypePy::staticCallback_getNumTypes (PyObject * /*self*/, PyObject *arg return PyLong_FromLong(num); } -PyObject* TypePy::staticCallback_getBadType (PyObject * /*self*/, PyObject *args) +PyObject* TypePy::getBadType (PyObject *args) { if (!PyArg_ParseTuple(args, "")) return nullptr; @@ -120,7 +120,7 @@ PyObject* TypePy::isDerivedFrom(PyObject *args) return PyBool_FromLong(v ? 1 : 0); } -PyObject* TypePy::staticCallback_getAllDerivedFrom(PyObject* /*self*/, PyObject *args) +PyObject* TypePy::getAllDerivedFrom(PyObject *args) { Base::Type type; diff --git a/src/Tools/generateTemplates/templateClassPyExport.py b/src/Tools/generateTemplates/templateClassPyExport.py index bc845daf21..b8934f0bdd 100644 --- a/src/Tools/generateTemplates/templateClassPyExport.py +++ b/src/Tools/generateTemplates/templateClassPyExport.py @@ -101,14 +101,26 @@ public: + if i.Keyword: /// callback for the @i.Name@() method static PyObject * staticCallback_@i.Name@ (PyObject *self, PyObject *args, PyObject *kwd); -+ if not i.Static and not i.Class: ++ if i.Static: + /// implementer for the @i.Name@() method + static PyObject* @i.Name@(PyObject *args, PyObject *kwd); += elif i.Class: + /// implementer for the @i.Name@() method + static PyObject* @i.Name@(PyObject *self, PyObject *args, PyObject *kwd); += else: /// implementer for the @i.Name@() method PyObject* @i.Name@(PyObject *args, PyObject *kwd); - = else: /// callback for the @i.Name@() method static PyObject * staticCallback_@i.Name@ (PyObject *self, PyObject *args); -+ if not i.Static and not i.Class: ++ if i.Static: + /// implementer for the @i.Name@() method + static PyObject* @i.Name@(PyObject *args); += elif i.Class: + /// implementer for the @i.Name@() method + static PyObject* @i.Name@(PyObject *self, PyObject *args); += else: /// implementer for the @i.Name@() method PyObject* @i.Name@(PyObject *args); - @@ -533,13 +545,13 @@ PyGetSetDef @self.export.Name@::GetterSetter[] = { // @i.Name@() callback and implementer // PyObject* @self.export.Name@::@i.Name@(PyObject *args){}; // has to be implemented in @self.export.Name@Imp.cpp -+ if not i.Static and not i.Class: + if i.Keyword: PyObject * @self.export.Name@::staticCallback_@i.Name@ (PyObject *self, PyObject *args, PyObject * kwd) = else: PyObject * @self.export.Name@::staticCallback_@i.Name@ (PyObject *self, PyObject *args) - { ++ if not i.Static and not i.Class: // make sure that not a null pointer is passed if (!self) { PyErr_SetString(PyExc_TypeError, "descriptor '@i.Name@' of '@self.export.Namespace@.@self.export.Twin@' object needs an argument"); @@ -560,15 +572,32 @@ PyObject * @self.export.Name@::staticCallback_@i.Name@ (PyObject *self, PyObject } - +- try { // catches all exceptions coming up from c++ and generate a python exception + if i.Keyword: ++ if i.Static: + (void)self; + PyObject* ret = @self.export.Name@::@i.Name@(args, kwd); += elif i.Class: + PyObject* ret = @self.export.Name@::@i.Name@(self, args, kwd); += else: PyObject* ret = static_cast<@self.export.Name@*>(self)->@i.Name@(args, kwd); +- += else: ++ if i.Static: + (void)self; + PyObject* ret = @self.export.Name@::@i.Name@(args); += elif i.Class: + PyObject* ret = @self.export.Name@::@i.Name@(self, args); = else: PyObject* ret = static_cast<@self.export.Name@*>(self)->@i.Name@(args); - +- ++ if not i.Static and not i.Class: + if (not i.Const): if (ret != 0) static_cast<@self.export.Name@*>(self)->startNotify(); +- - return ret; } // Please sync the following catch implementation with PY_CATCH @@ -611,7 +640,6 @@ PyObject * @self.export.Name@::staticCallback_@i.Name@ (PyObject *self, PyObject #endif } -- - + for i in self.export.Attribute: // @i.Name@() callback and implementer @@ -894,18 +922,24 @@ std::string @self.export.Name@::representation(void) const } + for i in self.export.Methode: -+ if not i.Static and not i.Class: + if i.Keyword: ++ if i.Class: +PyObject* @self.export.Name@::@i.Name@(PyObject *self, PyObject *args, PyObject *kwds) += else: PyObject* @self.export.Name@::@i.Name@(PyObject *args, PyObject *kwds) +- += else: ++ if i.Class: +PyObject* @self.export.Name@::@i.Name@(PyObject *self, PyObject *args) = else: PyObject* @self.export.Name@::@i.Name@(PyObject *args) - +- { PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented"); return 0; } - -- + if (self.export.NumberProtocol): PyObject* @self.export.Name@::number_add_handler(PyObject* /*self*/, PyObject* /*other*/) @@ -1213,18 +1247,24 @@ int @self.export.Name@::finalization() + for i in self.export.Methode: -+ if not i.Static and not i.Class: + if i.Keyword: ++ if i.Class: +PyObject* @self.export.Name@::@i.Name@(PyObject * /*self*/, PyObject * /*args*/, PyObject * /*kwds*/) += else: PyObject* @self.export.Name@::@i.Name@(PyObject * /*args*/, PyObject * /*kwds*/) +- += else: ++ if i.Class: +PyObject* @self.export.Name@::@i.Name@(PyObject * /*self*/, PyObject * /*args*/) = else: PyObject* @self.export.Name@::@i.Name@(PyObject * /*args*/) - +- { PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented"); return 0; } - -- + if (self.export.NumberProtocol): PyObject* @self.export.Name@::number_add_handler(PyObject *self, PyObject *other)