App: Check Python types using Base::PyTypeCheck

This commit is contained in:
marioalexis
2022-07-20 01:16:27 -03:00
committed by Uwe
parent 2185b8086d
commit 5cb31ece93
4 changed files with 41 additions and 59 deletions

View File

@@ -845,21 +845,20 @@ PyObject *Application::sGetLinksTo(PyObject * /*self*/, PyObject *args)
return nullptr;
PY_TRY {
Base::PyTypeCheck(&pyobj, &DocumentObjectPy::Type, "Expect the first argument of type App.DocumentObject or None");
DocumentObject *obj = nullptr;
if(pyobj!=Py_None) {
if(!PyObject_TypeCheck(pyobj,&DocumentObjectPy::Type)) {
PyErr_SetString(PyExc_TypeError, "Expect the first argument of type document object");
return nullptr;
}
if (pyobj)
obj = static_cast<DocumentObjectPy*>(pyobj)->getDocumentObjectPtr();
}
auto links = GetApplication().getLinksTo(obj,options,count);
Py::Tuple ret(links.size());
int i=0;
for(auto o : links)
ret.setItem(i++,Py::Object(o->getPyObject(),true));
return Py::new_reference_to(ret);
}PY_CATCH;
}
PY_CATCH;
}
PyObject *Application::sGetDependentObjects(PyObject * /*self*/, PyObject *args)

View File

@@ -581,31 +581,30 @@ PyObject* DocumentObjectPy::getLinkedObject(PyObject *args, PyObject *keywds)
&PyBool_Type,&recursive,&pyMat,&PyBool_Type,&transform,&depth))
return nullptr;
Base::Matrix4D _mat;
Base::Matrix4D *mat = nullptr;
if(pyMat!=Py_None) {
if(!PyObject_TypeCheck(pyMat,&Base::MatrixPy::Type)) {
PyErr_SetString(PyExc_TypeError, "expect argument 'matrix' to be of type Base.Matrix");
return nullptr;
}
_mat = *static_cast<Base::MatrixPy*>(pyMat)->getMatrixPtr();
mat = &_mat;
}
PY_TRY {
Base::PyTypeCheck(&pyMat, &Base::MatrixPy::Type, "expect argument 'matrix' to be of type Base.Matrix");
Base::Matrix4D _mat;
Base::Matrix4D *mat = nullptr;
if (pyMat) {
_mat = *static_cast<Base::MatrixPy*>(pyMat)->getMatrixPtr();
mat = &_mat;
}
auto linked = getDocumentObjectPtr()->getLinkedObject(
Base::asBoolean(recursive), mat, Base::asBoolean(transform), depth);
if(!linked)
if (!linked)
linked = getDocumentObjectPtr();
auto pyObj = Py::Object(linked->getPyObject(),true);
if(mat) {
if (mat) {
Py::Tuple ret(2);
ret.setItem(0,pyObj);
ret.setItem(1,Py::asObject(new Base::MatrixPy(*mat)));
return Py::new_reference_to(ret);
}
return Py::new_reference_to(pyObj);
} PY_CATCH;
}
PY_CATCH;
}
PyObject* DocumentObjectPy::isElementVisible(PyObject *args)

View File

@@ -363,18 +363,13 @@ PyObject* DocumentPy::importLinks(PyObject *args)
objs.push_back(static_cast<DocumentObjectPy*>(seq[i].ptr())->getDocumentObjectPtr());
}
}
else if (obj == Py_None) {
// do nothing here
}
else if (!PyObject_TypeCheck(obj,&DocumentObjectPy::Type)) {
PyErr_SetString(PyExc_TypeError,
"Expect first argument to be either a document object or sequence of document objects");
return nullptr;
}
else {
objs.push_back(static_cast<DocumentObjectPy*>(obj)->getDocumentObjectPtr());
}
else {
Base::PyTypeCheck(&obj, &DocumentObjectPy::Type,
"Expect first argument to be either a document object, sequence of document objects or None");
if (obj)
objs.push_back(static_cast<DocumentObjectPy*>(obj)->getDocumentObjectPtr());
}
if (objs.empty())
objs = getDocumentPtr()->getObjects();
@@ -385,7 +380,8 @@ PyObject* DocumentPy::importLinks(PyObject *args)
for (size_t i=0;i<ret.size();++i)
tuple.setItem(i,Py::Object(ret[i]->getPyObject(),true));
return Py::new_reference_to(tuple);
} PY_CATCH
}
PY_CATCH
}
PyObject* DocumentPy::moveObject(PyObject *args)
@@ -861,22 +857,21 @@ PyObject* DocumentPy::getLinksTo(PyObject *args)
return nullptr;
PY_TRY {
Base::PyTypeCheck(&pyobj, &DocumentObjectPy::Type, "Expect the first argument of type document object");
DocumentObject *obj = nullptr;
if (pyobj!=Py_None) {
if (!PyObject_TypeCheck(pyobj,&DocumentObjectPy::Type)) {
PyErr_SetString(PyExc_TypeError, "Expect the first argument of type document object");
return nullptr;
}
obj = static_cast<DocumentObjectPy*>(pyobj)->getDocumentObjectPtr();
}
if (pyobj)
obj = static_cast<DocumentObjectPy*>(pyobj)->getDocumentObjectPtr();
std::set<DocumentObject *> links;
getDocumentPtr()->getLinksTo(links,obj,options,count);
Py::Tuple ret(links.size());
int i=0;
for (auto o : links)
ret.setItem(i++,Py::Object(o->getPyObject(),true));
return Py::new_reference_to(ret);
} PY_CATCH
}
PY_CATCH
}
Py::List DocumentPy::getInList() const

View File

@@ -460,17 +460,13 @@ PyObject *PropertyLink::getPyObject()
void PropertyLink::setPyObject(PyObject *value)
{
if (PyObject_TypeCheck(value, &(DocumentObjectPy::Type))) {
DocumentObjectPy *pcObject = static_cast<DocumentObjectPy*>(value);
Base::PyTypeCheck(&value, &DocumentObjectPy::Type);
if (value) {
DocumentObjectPy *pcObject = static_cast<DocumentObjectPy*>(value);
setValue(pcObject->getDocumentObjectPtr());
}
else if (Py_None == value) {
setValue(nullptr);
}
else {
std::string error = std::string("type must be 'DocumentObject' or 'NoneType', not ");
error += value->ob_type->tp_name;
throw Base::TypeError(error);
setValue(nullptr);
}
}
@@ -708,16 +704,9 @@ PyObject *PropertyLinkList::getPyObject()
DocumentObject *PropertyLinkList::getPyValue(PyObject *item) const
{
if (item == Py_None) {
return nullptr;
}
else if (PyObject_TypeCheck(item, &(DocumentObjectPy::Type))) {
return static_cast<DocumentObjectPy*>(item)->getDocumentObjectPtr();
}
Base::PyTypeCheck(&item, &DocumentObjectPy::Type);
std::string error = std::string("type must be 'DocumentObject', list of 'DocumentObject', or NoneType, not ");
error += item->ob_type->tp_name;
throw Base::TypeError(error);
return item ? static_cast<DocumentObjectPy*>(item)->getDocumentObjectPtr() : nullptr;
}
void PropertyLinkList::Save(Base::Writer &writer) const