Gui: Check Python types using Base::PyTypeCheck

This commit is contained in:
marioalexis
2022-07-20 01:18:22 -03:00
committed by Uwe
parent 5cb31ece93
commit fa6cf56020
7 changed files with 64 additions and 64 deletions

View File

@@ -308,23 +308,23 @@ PyObject* LinkViewPy::getDetailPath(PyObject* args)
}PY_CATCH
}
PyObject* LinkViewPy::getBoundBox(PyObject* args) {
PyObject* LinkViewPy::getBoundBox(PyObject* args)
{
PyObject *vobj = Py_None;
if (!PyArg_ParseTuple(args, "O",&vobj))
return nullptr;
ViewProviderDocumentObject *vpd = nullptr;
if(vobj!=Py_None) {
if(!PyObject_TypeCheck(vobj,&ViewProviderDocumentObjectPy::Type)) {
PyErr_SetString(PyExc_TypeError, "exepcting a type of ViewProviderDocumentObject");
return nullptr;
}
vpd = static_cast<ViewProviderDocumentObjectPy*>(vobj)->getViewProviderDocumentObjectPtr();
}
PY_TRY {
Base::PyTypeCheck(&vobj, &ViewProviderDocumentObjectPy::Type);
ViewProviderDocumentObject *vpd = nullptr;
if (vobj)
vpd = static_cast<ViewProviderDocumentObjectPy*>(vobj)->getViewProviderDocumentObjectPtr();
auto bbox = getLinkViewPtr()->getBoundBox(vpd);
Py::Object ret(new Base::BoundBoxPy(new Base::BoundBox3d(bbox)));
return Py::new_reference_to(ret);
}PY_CATCH
}
PY_CATCH
}
PyObject *LinkViewPy::getCustomAttributes(const char*) const

View File

@@ -238,18 +238,18 @@ Py::Object MDIViewPy::setActiveObject(const Py::Tuple& args)
if (!PyArg_ParseTuple(args.ptr(), "s|Os", &name, &docObject, &subname))
throw Py::Exception();
if (_view) {
if (docObject == Py_None) {
_view->setActiveObject(nullptr, name);
}
else {
if (!PyObject_TypeCheck(docObject, &App::DocumentObjectPy::Type))
throw Py::TypeError("Expect the second argument to be a document object or None");
App::DocumentObject* obj = static_cast<App::DocumentObjectPy*>(docObject)->getDocumentObjectPtr();
try {
Base::PyTypeCheck(&docObject, &App::DocumentObjectPy::Type,
"Expect the second argument to be a document object or None");
if (_view) {
App::DocumentObject* obj = docObject ?
static_cast<App::DocumentObjectPy*>(docObject)->getDocumentObjectPtr() : nullptr;
_view->setActiveObject(obj, name, subname);
}
}
catch (const Base::Exception& e) {
throw Py::Exception(e.getPyExceptionType(), e.what());
}
return Py::None();
}

View File

@@ -2523,17 +2523,10 @@ PyObject *SelectionSingleton::sSetVisible(PyObject * /*self*/, PyObject *args)
return nullptr;
PY_TRY {
VisibleState vis;
if(visible == Py_None) {
vis = VisToggle;
}
else if (PyBool_Check(visible)) {
vis = Base::asBoolean(visible) ? VisShow : VisHide;
}
else {
PyErr_SetString(PyExc_ValueError, "Argument is neither None nor Bool");
return nullptr;
}
VisibleState vis = VisToggle;
Base::PyTypeCheck(&visible, &PyBool_Type);
if (visible)
vis = PyObject_IsTrue(visible) ? VisShow : VisHide;
Selection().setVisible(vis);
Py_Return;

View File

@@ -2566,17 +2566,18 @@ Py::Object View3DInventorPy::setActiveObject(const Py::Tuple& args)
if (!PyArg_ParseTuple(args.ptr(), "s|Os", &name, &docObject, &subname))
throw Py::Exception();
if (docObject == Py_None) {
getView3DIventorPtr()->setActiveObject(nullptr, name);
}
else {
if (!PyObject_TypeCheck(docObject, &App::DocumentObjectPy::Type))
throw Py::TypeError("Expect the second argument to be a document object or None");
App::DocumentObject* obj = static_cast<App::DocumentObjectPy*>(docObject)->getDocumentObjectPtr();
try {
Base::PyTypeCheck(&docObject, &App::DocumentObjectPy::Type,
"Expect the second argument to be a document object or None");
App::DocumentObject* obj = docObject ?
static_cast<App::DocumentObjectPy*>(docObject)->getDocumentObjectPtr() : nullptr;
getView3DIventorPtr()->setActiveObject(obj, name, subname);
}
return Py::None();
return Py::None();
}
catch (const Base::Exception& e) {
throw Py::Exception(e.getPyExceptionType(), e.what());
}
}
Py::Object View3DInventorPy::getActiveObject(const Py::Tuple& args)

View File

@@ -59,10 +59,10 @@ Check if the object is visible.</UserDocu>
</Methode>
<Methode Name="canDragObject">
<Documentation>
<UserDocu>canDragObject(obj) -> bool\n
<UserDocu>canDragObject(obj=None) -> bool\n
Check whether the child object can be removed by dragging.
If 'obj' is not given, check without filter by any particular object.\n
obj : App.DocumentObject\n Object to be dragged. Optional.</UserDocu>
obj : App.DocumentObject\n Object to be dragged.</UserDocu>
</Documentation>
</Methode>
<Methode Name="dragObject">
@@ -74,25 +74,25 @@ obj : App.DocumentObject\n Object to be dragged.</UserDocu>
</Methode>
<Methode Name="canDropObject" Keyword="true">
<Documentation>
<UserDocu>canDropObject(obj, owner, subname, elem) -> bool\n
<UserDocu>canDropObject(obj=None, owner=None, subname, elem=None) -> bool\n
Check whether the child object can be added by dropping.
If 'obj' is not given, check without filter by any particular object.\n
obj : App.DocumentObject\n Object to be dropped. Optional.
owner : App.DocumentObject\n Parent object of the dropping object. Optional.
obj : App.DocumentObject\n Object to be dropped.
owner : App.DocumentObject\n Parent object of the dropping object.
subname : str\n Subname reference to the dropping object. Optional.
elem : sequence of str\n Non-objects subelements selected when the object is
being dropped. Optional.</UserDocu>
being dropped.</UserDocu>
</Documentation>
</Methode>
<Methode Name="dropObject" Keyword="true">
<Documentation>
<UserDocu>dropObject(obj, owner, subname, elem) -> str\n
<UserDocu>dropObject(obj, owner=None, subname, elem=None) -> str\n
Add a child object by dropping.\n
obj : App.DocumentObject\n Object to be dropped.
owner : App.DocumentObject\n Parent object of the dropping object. Optional.
owner : App.DocumentObject\n Parent object of the dropping object.
subname : str\n Subname reference to the dropping object. Optional.
elem : sequence of str\n Non-objects subelements selected when the object is
being dropped. Optional.</UserDocu>
being dropped.</UserDocu>
</Documentation>
</Methode>
<Methode Name="canDragAndDropObject">

View File

@@ -161,11 +161,12 @@ PyObject* ViewProviderPy::isVisible(PyObject *args)
PyObject* ViewProviderPy::canDragObject(PyObject *args)
{
PyObject *obj = nullptr;
if (!PyArg_ParseTuple(args, "|O!", &App::DocumentObjectPy::Type, &obj))
PyObject *obj = Py_None;
if (!PyArg_ParseTuple(args, "|O", &obj))
return nullptr;
PY_TRY {
Base::PyTypeCheck(&obj, &App::DocumentObjectPy::Type);
bool ret;
if (!obj)
ret = getViewProviderPtr()->canDragObjects();
@@ -180,17 +181,20 @@ PyObject* ViewProviderPy::canDragObject(PyObject *args)
PyObject* ViewProviderPy::canDropObject(PyObject *args, PyObject *kw)
{
PyObject *obj = nullptr;
PyObject *owner = nullptr;
PyObject *pyElements = nullptr;
PyObject *obj = Py_None;
PyObject *owner = Py_None;
PyObject *pyElements = Py_None;
const char *subname = nullptr;
static char* kwlist[] = {"obj","owner","subname","elem",nullptr};
if (!PyArg_ParseTupleAndKeywords(args, kw, "|O!O!sO", kwlist,
&App::DocumentObjectPy::Type,&obj, &App::DocumentObjectPy::Type, &owner,
&subname, &pyElements))
if (!PyArg_ParseTupleAndKeywords(args, kw, "|OOsO", kwlist,
&obj, &owner, &subname, &pyElements))
return nullptr;
PY_TRY {
Base::PyTypeCheck(&obj, &App::DocumentObjectPy::Type, "expecting 'obj' to be of type App.DocumentObject or None");
Base::PyTypeCheck(&owner, &App::DocumentObjectPy::Type, "expecting 'owner' to be of type App.DocumentObject or None");
Base::PyTypeCheck(&pyElements, PySequence_Check, "expecting 'elem' to be sequence or None");
bool ret;
App::DocumentObject* pcObject;
App::DocumentObject* pcOwner = nullptr;
@@ -238,16 +242,18 @@ PyObject* ViewProviderPy::canDragAndDropObject(PyObject *args)
PyObject* ViewProviderPy::dropObject(PyObject *args, PyObject *kw)
{
PyObject *obj;
PyObject *owner = nullptr;
PyObject *pyElements = nullptr;
PyObject *owner = Py_None;
PyObject *pyElements = Py_None;
const char *subname = nullptr;
static char* kwlist[] = {"obj","owner","subname","elem",nullptr};
if (!PyArg_ParseTupleAndKeywords(args, kw, "O!|O!sO", kwlist,
&App::DocumentObjectPy::Type,&obj, &App::DocumentObjectPy::Type, &owner,
&subname, &pyElements))
if (!PyArg_ParseTupleAndKeywords(args, kw, "O!|OsO", kwlist,
&App::DocumentObjectPy::Type, &obj, &owner, &subname, &pyElements))
return nullptr;
PY_TRY {
Base::PyTypeCheck(&owner, &App::DocumentObjectPy::Type, "expecting 'owner' to be of type App.DocumentObject or None");
Base::PyTypeCheck(&pyElements, PySequence_Check, "expecting 'elem' to be sequence or None");
auto pcObject = static_cast<App::DocumentObjectPy*>(obj)->getDocumentObjectPtr();
App::DocumentObject *pcOwner = nullptr;
App::PropertyStringList elements;

View File

@@ -49,12 +49,12 @@ class PartGuiViewProviderTestCases(unittest.TestCase):
# https://github.com/FreeCAD/FreeCAD/pull/6850
box = self.Doc.addObject("Part::Box", "Box")
with self.assertRaises(TypeError):
box.ViewObject.canDragObject(None)
box.ViewObject.canDragObject(0)
with self.assertRaises(TypeError):
box.ViewObject.canDropObject(None)
box.ViewObject.canDropObject(0)
box.ViewObject.canDropObject()
with self.assertRaises(TypeError):
box.ViewObject.dropObject(box, None)
box.ViewObject.dropObject(box, 0)
def tearDown(self):
#closing doc