From ac7750fab6074fe5de0e43c3121fe9aea24248c5 Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 20 Jan 2017 13:17:11 +0100 Subject: [PATCH] issue #0002867: AttachExtension crash --- src/App/FeaturePythonPyImp.h | 31 +++++++++++++++++++++++++++ src/Mod/Part/App/PrimitiveFeature.cpp | 15 +++++++++++++ src/Mod/Part/App/PrimitiveFeature.h | 1 + 3 files changed, 47 insertions(+) diff --git a/src/App/FeaturePythonPyImp.h b/src/App/FeaturePythonPyImp.h index 4e72f013d..3926bfff7 100644 --- a/src/App/FeaturePythonPyImp.h +++ b/src/App/FeaturePythonPyImp.h @@ -26,6 +26,37 @@ #include #include +#define PYTHON_TYPE_DEF(_class_, _subclass_) \ + class _class_ : public _subclass_ \ + { \ + public: \ + static PyTypeObject Type; \ + public: \ + _class_(Base::BaseClass *pcObject, PyTypeObject *T = &Type); \ + virtual ~_class_(); \ + }; + +#define PYTHON_TYPE_IMP(_class_, _subclass_) \ + PyTypeObject _class_::Type = { \ + PyObject_HEAD_INIT(&PyType_Type) \ + 0, \ + ""#_class_"", \ + sizeof(_class_), \ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ + Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_CLASS, \ + ""#_class_"", \ + 0, 0, 0, 0, 0, 0, 0, 0, 0, \ + &_subclass_::Type, \ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \ + }; \ + _class_::_class_(Base::BaseClass *pcObject, PyTypeObject *T) \ + : _subclass_(reinterpret_cast<_subclass_::PointerType>(pcObject), T) \ + { \ + } \ + _class_::~_class_() \ + { \ + } + namespace App { diff --git a/src/Mod/Part/App/PrimitiveFeature.cpp b/src/Mod/Part/App/PrimitiveFeature.cpp index fef056f72..fda178701 100644 --- a/src/Mod/Part/App/PrimitiveFeature.cpp +++ b/src/Mod/Part/App/PrimitiveFeature.cpp @@ -63,6 +63,8 @@ #include "PrimitiveFeature.h" +#include +#include #include #include #include @@ -105,6 +107,19 @@ App::DocumentObjectExecReturn* Primitive::execute(void) { return Part::Feature::execute(); } +namespace Part { + PYTHON_TYPE_DEF(PrimitivePy, PartFeaturePy) + PYTHON_TYPE_IMP(PrimitivePy, PartFeaturePy) +} + +PyObject* Primitive::getPyObject() +{ + if (PythonObject.is(Py::_None())){ + // ref counter is set to 1 + PythonObject = Py::Object(new PrimitivePy(this),true); + } + return Py::new_reference_to(PythonObject); +} void Primitive::Restore(Base::XMLReader &reader) { diff --git a/src/Mod/Part/App/PrimitiveFeature.h b/src/Mod/Part/App/PrimitiveFeature.h index f430b1d62..f73f8c41d 100644 --- a/src/Mod/Part/App/PrimitiveFeature.h +++ b/src/Mod/Part/App/PrimitiveFeature.h @@ -44,6 +44,7 @@ public: /// recalculate the feature App::DocumentObjectExecReturn *execute(void); short mustExecute() const; + PyObject* getPyObject(); //@} protected: