App: do not hide actual exception type in DocumentObjectPy::addProperty/DocumentObjectPy::removeProperty

and throw TypeError in DynamicProperty::addDynamicProperty() if the based type id is invalid
This commit is contained in:
wmayer
2022-01-19 10:48:00 +01:00
parent 59b4f8d6d8
commit b9280ae760
3 changed files with 5 additions and 21 deletions

View File

@@ -88,19 +88,8 @@ PyObject* DocumentObjectPy::addProperty(PyObject *args)
PyMem_Free(sDoc);
}
App::Property* prop=0;
try {
prop = getDocumentObjectPtr()->addDynamicProperty(sType,sName,sGroup,sDocStr.c_str(),attr,
getDocumentObjectPtr()->addDynamicProperty(sType,sName,sGroup,sDocStr.c_str(),attr,
PyObject_IsTrue(ro) ? true : false, PyObject_IsTrue(hd) ? true : false);
}
catch (const Base::Exception& e) {
throw Py::RuntimeError(e.what());
}
if (!prop) {
std::stringstream str;
str << "No property found of type '" << sType << "'" << std::ends;
throw Py::Exception(Base::BaseExceptionFreeCADError,str.str());
}
return Py::new_reference_to(this);
}
@@ -111,13 +100,8 @@ PyObject* DocumentObjectPy::removeProperty(PyObject *args)
if (!PyArg_ParseTuple(args, "s", &sName))
return NULL;
try {
bool ok = getDocumentObjectPtr()->removeDynamicProperty(sName);
return Py_BuildValue("O", (ok ? Py_True : Py_False));
}
catch (const Base::Exception& e) {
throw Py::RuntimeError(e.what());
}
bool ok = getDocumentObjectPtr()->removeDynamicProperty(sName);
return Py_BuildValue("O", (ok ? Py_True : Py_False));
}
PyObject* DocumentObjectPy::supportedProperties(PyObject *args)

View File

@@ -183,7 +183,7 @@ Property* DynamicProperty::addDynamicProperty(PropertyContainer &pc, const char*
Base::Type propType = Base::Type::getTypeIfDerivedFrom(type, App::Property::getClassTypeId(), true);
if (propType.isBad()) {
FC_THROWM(Base::ValueError, "Invalid type "
FC_THROWM(Base::TypeError, "Invalid type "
<< type << " for property " << pc.getFullName() << '.' << name);
}

View File

@@ -205,7 +205,7 @@ class DocumentBasicCases(unittest.TestCase):
self.assertEqual(ext.createInstance(), None)
obj = self.Doc.addObject("App::FeaturePython", "Object")
with self.assertRaises(RuntimeError):
with self.assertRaises(TypeError):
obj.addProperty("App::DocumentObjectExtension", "Property")
with self.assertRaises(TypeError):