App: Fix ExtensionPython macro

This commit is contained in:
marioalexis
2025-05-25 18:58:48 -03:00
committed by Benjamin Nauck
parent 32ddaff649
commit 0cdf0af0c6
5 changed files with 24 additions and 15 deletions

View File

@@ -69,6 +69,7 @@ using namespace App;
Extension::~Extension()
{
if (!ExtensionPythonObject.is(Py::_None())) {
Base::PyGILStateLocker lock;
// Remark: The API of Py::Object has been changed to set whether the wrapper owns the passed
// Python object or not. In the constructor we forced the wrapper to own the object so we
// need not to dec'ref the Python object any more. But we must still invalidate the Python

View File

@@ -1,3 +1,4 @@
from typing import Final, Any
from Base.PyObjectBase import PyObjectBase
@@ -7,4 +8,6 @@ class Extension(PyObjectBase):
Author: Stefan Troeger (stefantroeger@gmx.net)
Licence: LGPL
"""
...
ExtendedObject: Final[Any] = None
"""Get extended container object"""

View File

@@ -23,9 +23,9 @@
#include "PreCompiled.h"
#include "Application.h"
#include "ExtensionContainer.h"
// inclusion of the generated files (generated out of PropertyContainerPy.xml)
// inclusion of the generated files (generated out of ExtensionPy.pyi)
#include <App/ExtensionPy.h>
#include <App/ExtensionPy.cpp>
@@ -37,6 +37,11 @@ std::string ExtensionPy::representation() const
return {"<extension>"};
}
Py::Object ExtensionPy::getExtendedObject() const
{
return Py::Object(getExtensionPtr()->getExtendedContainer()->getPyObject(), true);
}
PyObject* ExtensionPy::getCustomAttributes(const char* /*attr*/) const
{
return nullptr;

View File

@@ -63,12 +63,12 @@ using ExtensionPython = ExtensionPythonT<App::Extension>;
Base::PyGILStateLocker lock; \
Py::Object result; \
try { \
Property* proxy = this->getExtendedContainer()->getPropertyByName("Proxy"); \
if (proxy && proxy->is<PropertyPythonObject>()) { \
Py::Object feature = static_cast<PropertyPythonObject*>(proxy)->getValue(); \
if (feature.hasAttr(std::string("function"))) { \
App::Property* proxy = this->getExtendedContainer()->getPropertyByName("Proxy"); \
if (proxy && proxy->is<App::PropertyPythonObject>()) { \
Py::Object feature = static_cast<App::PropertyPythonObject*>(proxy)->getValue(); \
if (feature.hasAttr(std::string(#function))) { \
if (feature.hasAttr("__object__")) { \
Py::Callable method(feature.getAttr(std::string("function")));
Py::Callable method(feature.getAttr(std::string(#function)));
#define EXTENSION_PROXY_SECOND(function) \
@@ -76,7 +76,7 @@ using ExtensionPython = ExtensionPythonT<App::Extension>;
} \
else \
{ \
Py::Callable method(feature.getAttr(std::string("function")));
Py::Callable method(feature.getAttr(std::string(#function)));
#define EXTENSION_PROXY_THIRD() \
result = method.apply(args); \
@@ -109,13 +109,13 @@ using ExtensionPython = ExtensionPythonT<App::Extension>;
EXTENSION_PROXY_THIRD()
#define EXTENSION_PYTHON_OVERRIDE_VOID_NOARGS(function) \
virtual void function() override { EXTENSION_PROXY_NOARGS(function) };
virtual void function() override { EXTENSION_PROXY_NOARG(function) };
#define EXTENSION_PYTHON_OVERRIDE_OBJECT_NOARGS(function) \
virtual PyObject* function() override \
{ \
EXTENSION_PROXY_NOARGS(function) \
return res.ptr(); \
EXTENSION_PROXY_NOARG(function) \
return result.ptr(); \
};
} // namespace App

View File

@@ -431,7 +431,7 @@ class DocumentBasicCases(unittest.TestCase):
# test if the method override works
class SpecialGroup:
def allowObject(self, obj):
def allowObject(self, ext, obj):
return False
callback = SpecialGroup()
@@ -444,7 +444,7 @@ class DocumentBasicCases(unittest.TestCase):
grp2.addObject(obj)
self.assertTrue(len(grp2.Group) == 0)
except Exception:
self.assertTrue(True)
self.assertTrue(False)
self.Doc.removeObject(grp.Name)
self.Doc.removeObject(grp2.Name)
@@ -721,7 +721,7 @@ class SaveRestoreSpecialGroup:
obj.addExtension("App::GroupExtensionPython")
obj.Proxy = self
def allowObject(self, obj):
def allowObject(self, ext, obj):
return False