App: Use PyObject_IsTrue in combination with conditional ternary operator
This commit is contained in:
committed by
Chris Hennes
parent
69932742d2
commit
22763c9e4f
@@ -243,14 +243,14 @@ PyObject* Application::sOpenDocument(PyObject * /*self*/, PyObject *args, PyObje
|
||||
char* Name;
|
||||
PyObject *hidden = Py_False;
|
||||
static char *kwlist[] = {"name","hidden",nullptr};
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwd, "et|O", kwlist,
|
||||
"utf-8", &Name, &hidden))
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwd, "et|O!", kwlist,
|
||||
"utf-8", &Name, &PyBool_Type, &hidden))
|
||||
return nullptr;
|
||||
std::string EncodedName = std::string(Name);
|
||||
PyMem_Free(Name);
|
||||
try {
|
||||
// return new document
|
||||
return (GetApplication().openDocument(EncodedName.c_str(),!PyObject_IsTrue(hidden))->getPyObject());
|
||||
return (GetApplication().openDocument(EncodedName.c_str(),PyObject_IsTrue(hidden) ? false : true)->getPyObject());
|
||||
}
|
||||
catch (const Base::Exception& e) {
|
||||
PyErr_SetString(PyExc_IOError, e.what());
|
||||
@@ -270,14 +270,14 @@ PyObject* Application::sNewDocument(PyObject * /*self*/, PyObject *args, PyObjec
|
||||
PyObject *hidden = Py_False;
|
||||
PyObject *temp = Py_False;
|
||||
static char *kwlist[] = {"name","label","hidden","temp",nullptr};
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwd, "|etetOO", kwlist,
|
||||
"utf-8", &docName, "utf-8", &usrName, &hidden, &temp))
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwd, "|etetO!O!", kwlist,
|
||||
"utf-8", &docName, "utf-8", &usrName, &PyBool_Type, &hidden, &PyBool_Type, &temp))
|
||||
return nullptr;
|
||||
|
||||
PY_TRY {
|
||||
App::Document* doc = GetApplication().newDocument(docName, usrName,
|
||||
!PyObject_IsTrue(hidden),
|
||||
PyObject_IsTrue(temp));
|
||||
PyObject_IsTrue(hidden) ? false : true,
|
||||
PyObject_IsTrue(temp) ? true : false);
|
||||
PyMem_Free(docName);
|
||||
PyMem_Free(usrName);
|
||||
return doc->getPyObject();
|
||||
@@ -701,7 +701,7 @@ PyObject* Application::sGetUserMacroPath(PyObject * /*self*/, PyObject *args)
|
||||
return nullptr;
|
||||
|
||||
std::string macroDir = Application::getUserMacroDir();
|
||||
if (PyObject_IsTrue(actual)) {
|
||||
if (PyObject_IsTrue(actual) ? true : false) {
|
||||
macroDir = App::GetApplication().
|
||||
GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro")
|
||||
->GetASCII("MacroPath",macroDir.c_str());
|
||||
@@ -732,7 +732,7 @@ PyObject* Application::sGetHomePath(PyObject * /*self*/, PyObject *args)
|
||||
PyObject* Application::sListDocuments(PyObject * /*self*/, PyObject *args)
|
||||
{
|
||||
PyObject *sort = Py_False;
|
||||
if (!PyArg_ParseTuple(args, "|O",&sort))
|
||||
if (!PyArg_ParseTuple(args, "|O!", &PyBool_Type, &sort))
|
||||
return nullptr;
|
||||
PY_TRY {
|
||||
PyObject *pDict = PyDict_New();
|
||||
@@ -740,7 +740,7 @@ PyObject* Application::sListDocuments(PyObject * /*self*/, PyObject *args)
|
||||
Base::PyObjectBase* pValue;
|
||||
|
||||
std::vector<Document*> docs = GetApplication().getDocuments();;
|
||||
if(PyObject_IsTrue(sort))
|
||||
if(PyObject_IsTrue(sort) ? true : false)
|
||||
docs = Document::getDependentDocuments(docs,true);
|
||||
|
||||
for (auto doc : docs) {
|
||||
@@ -942,11 +942,11 @@ PyObject *Application::sSetActiveTransaction(PyObject * /*self*/, PyObject *args
|
||||
{
|
||||
char *name;
|
||||
PyObject *persist = Py_False;
|
||||
if (!PyArg_ParseTuple(args, "s|O", &name,&persist))
|
||||
if (!PyArg_ParseTuple(args, "s|O!", &name, &PyBool_Type, &persist))
|
||||
return nullptr;
|
||||
|
||||
PY_TRY {
|
||||
Py::Int ret(GetApplication().setActiveTransaction(name,PyObject_IsTrue(persist)));
|
||||
Py::Int ret(GetApplication().setActiveTransaction(name,PyObject_IsTrue(persist) ? true : false));
|
||||
return Py::new_reference_to(ret);
|
||||
}PY_CATCH;
|
||||
}
|
||||
@@ -972,11 +972,11 @@ PyObject *Application::sCloseActiveTransaction(PyObject * /*self*/, PyObject *ar
|
||||
{
|
||||
PyObject *abort = Py_False;
|
||||
int id = 0;
|
||||
if (!PyArg_ParseTuple(args, "|Oi", &abort,&id))
|
||||
if (!PyArg_ParseTuple(args, "|O!i", &PyBool_Type, &abort,&id))
|
||||
return nullptr;
|
||||
|
||||
PY_TRY {
|
||||
GetApplication().closeActiveTransaction(PyObject_IsTrue(abort),id);
|
||||
GetApplication().closeActiveTransaction(PyObject_IsTrue(abort) ? true : false, id);
|
||||
Py_Return;
|
||||
} PY_CATCH;
|
||||
}
|
||||
|
||||
@@ -374,12 +374,12 @@ PyObject* DocumentObjectPy::evalExpression(PyObject *self, PyObject * args)
|
||||
|
||||
PyObject* DocumentObjectPy::recompute(PyObject *args)
|
||||
{
|
||||
PyObject *recursive=Py_False;
|
||||
if (!PyArg_ParseTuple(args, "|O",&recursive))
|
||||
PyObject *recursive = Py_False;
|
||||
if (!PyArg_ParseTuple(args, "|O!", &PyBool_Type, &recursive))
|
||||
return nullptr;
|
||||
|
||||
try {
|
||||
bool ok = getDocumentObjectPtr()->recomputeFeature(PyObject_IsTrue(recursive));
|
||||
bool ok = getDocumentObjectPtr()->recomputeFeature(PyObject_IsTrue(recursive) ? true : false);
|
||||
return Py_BuildValue("O", (ok ? Py_True : Py_False));
|
||||
}
|
||||
catch (const Base::Exception& e) {
|
||||
@@ -434,8 +434,8 @@ PyObject* DocumentObjectPy::getSubObject(PyObject *args, PyObject *keywds)
|
||||
short depth = 0;
|
||||
|
||||
static char *kwlist[] = {"subname", "retType", "matrix", "transform", "depth", nullptr};
|
||||
if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|hO!Oh", kwlist,
|
||||
&obj, &retType, &Base::MatrixPy::Type, &pyMat, &doTransform, &depth))
|
||||
if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|hO!O!h", kwlist,
|
||||
&obj, &retType, &Base::MatrixPy::Type, &pyMat, &PyBool_Type, &doTransform, &depth))
|
||||
return nullptr;
|
||||
|
||||
if (retType < 0 || retType > 6) {
|
||||
@@ -469,7 +469,7 @@ PyObject* DocumentObjectPy::getSubObject(PyObject *args, PyObject *keywds)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool transform = PyObject_IsTrue(doTransform);
|
||||
bool transform = PyObject_IsTrue(doTransform) ? true : false;
|
||||
|
||||
struct SubInfo {
|
||||
App::DocumentObject *sobj;
|
||||
@@ -578,8 +578,8 @@ PyObject* DocumentObjectPy::getLinkedObject(PyObject *args, PyObject *keywds)
|
||||
PyObject *transform = Py_True;
|
||||
short depth = 0;
|
||||
static char *kwlist[] = {"recursive","matrix","transform","depth", nullptr};
|
||||
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|OOOh", kwlist,
|
||||
&recursive,&pyMat,&transform,&depth))
|
||||
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|O!OO!h", kwlist,
|
||||
&PyBool_Type,&recursive,&pyMat,&PyBool_Type,&transform,&depth))
|
||||
return nullptr;
|
||||
|
||||
Base::Matrix4D _mat;
|
||||
@@ -595,7 +595,7 @@ PyObject* DocumentObjectPy::getLinkedObject(PyObject *args, PyObject *keywds)
|
||||
|
||||
PY_TRY {
|
||||
auto linked = getDocumentObjectPtr()->getLinkedObject(
|
||||
PyObject_IsTrue(recursive), mat, PyObject_IsTrue(transform),depth);
|
||||
PyObject_IsTrue(recursive) ? true : false, mat, PyObject_IsTrue(transform) ? true : false, depth);
|
||||
if(!linked)
|
||||
linked = getDocumentObjectPtr();
|
||||
auto pyObj = Py::Object(linked->getPyObject(),true);
|
||||
@@ -623,10 +623,10 @@ PyObject* DocumentObjectPy::setElementVisible(PyObject *args)
|
||||
{
|
||||
char *element = nullptr;
|
||||
PyObject *visible = Py_True;
|
||||
if (!PyArg_ParseTuple(args, "s|O", &element,&visible))
|
||||
if (!PyArg_ParseTuple(args, "s|O!", &element, &PyBool_Type, &visible))
|
||||
return nullptr;
|
||||
PY_TRY {
|
||||
return Py_BuildValue("h", getDocumentObjectPtr()->setElementVisible(element,PyObject_IsTrue(visible)));
|
||||
return Py_BuildValue("h", getDocumentObjectPtr()->setElementVisible(element,PyObject_IsTrue(visible) ? true : false));
|
||||
} PY_CATCH;
|
||||
}
|
||||
|
||||
@@ -830,13 +830,13 @@ PyObject *DocumentObjectPy::resolveSubElement(PyObject *args)
|
||||
const char *subname;
|
||||
PyObject *append = Py_False;
|
||||
int type = 0;
|
||||
if (!PyArg_ParseTuple(args, "s|Oi",&subname,&append,&type))
|
||||
if (!PyArg_ParseTuple(args, "s|O!i",&subname,&PyBool_Type,&append,&type))
|
||||
return nullptr;
|
||||
|
||||
PY_TRY {
|
||||
std::pair<std::string,std::string> elementName;
|
||||
auto obj = GeoFeature::resolveElement(getDocumentObjectPtr(), subname,elementName,
|
||||
PyObject_IsTrue(append),(GeoFeature::ElementNameType)type);
|
||||
PyObject_IsTrue(append) ? true : false,(GeoFeature::ElementNameType)type);
|
||||
Py::Tuple ret(3);
|
||||
ret.setItem(0,obj?Py::Object(obj->getPyObject(),true):Py::None());
|
||||
ret.setItem(1,Py::String(elementName.first));
|
||||
@@ -866,7 +866,7 @@ PyObject *DocumentObjectPy::adjustRelativeLinks(PyObject *args) {
|
||||
std::set<App::DocumentObject *> visited;
|
||||
return Py::new_reference_to(Py::Boolean(
|
||||
getDocumentObjectPtr()->adjustRelativeLinks(inList,
|
||||
PyObject_IsTrue(recursive)?&visited:nullptr)));
|
||||
PyObject_IsTrue(recursive) ? &visited : nullptr)));
|
||||
}PY_CATCH
|
||||
}
|
||||
|
||||
|
||||
@@ -212,13 +212,13 @@ PyObject* DocumentPy::addObject(PyObject *args, PyObject *kwd)
|
||||
PyObject* view=nullptr;
|
||||
PyObject *attach=Py_False;
|
||||
static char *kwlist[] = {"type","name","objProxy","viewProxy","attach","viewType",nullptr};
|
||||
if (!PyArg_ParseTupleAndKeywords(args,kwd,"s|sOOOs",
|
||||
kwlist, &sType,&sName,&obj,&view,&attach,&sViewType))
|
||||
if (!PyArg_ParseTupleAndKeywords(args,kwd,"s|sOOO!s",
|
||||
kwlist, &sType,&sName,&obj,&view,&PyBool_Type,&attach,&sViewType))
|
||||
return nullptr;
|
||||
|
||||
DocumentObject *pcFtr = nullptr;
|
||||
|
||||
if (!obj || !PyObject_IsTrue(attach)) {
|
||||
if (!obj || (PyObject_IsTrue(attach) ? false : true)) {
|
||||
pcFtr = getDocumentPtr()->addObject(sType,sName,true,sViewType);
|
||||
}
|
||||
else {
|
||||
@@ -248,7 +248,7 @@ PyObject* DocumentPy::addObject(PyObject *args, PyObject *kwd)
|
||||
}
|
||||
pyftr.setAttr("Proxy", pyobj);
|
||||
|
||||
if (PyObject_IsTrue(attach)) {
|
||||
if (PyObject_IsTrue(attach) ? true : false) {
|
||||
getDocumentPtr()->addObject(pcFtr,sName);
|
||||
|
||||
try {
|
||||
@@ -307,7 +307,7 @@ PyObject* DocumentPy::removeObject(PyObject *args)
|
||||
PyObject* DocumentPy::copyObject(PyObject *args)
|
||||
{
|
||||
PyObject *obj, *rec=Py_False, *retAll=Py_False;
|
||||
if (!PyArg_ParseTuple(args, "O|OO",&obj,&rec,&retAll))
|
||||
if (!PyArg_ParseTuple(args, "O|O!O!",&obj,&PyBool_Type,&rec,&PyBool_Type,&retAll))
|
||||
return nullptr;
|
||||
|
||||
std::vector<App::DocumentObject*> objs;
|
||||
@@ -333,7 +333,7 @@ PyObject* DocumentPy::copyObject(PyObject *args)
|
||||
}
|
||||
|
||||
PY_TRY {
|
||||
auto ret = getDocumentPtr()->copyObject(objs, PyObject_IsTrue(rec), PyObject_IsTrue(retAll));
|
||||
auto ret = getDocumentPtr()->copyObject(objs, PyObject_IsTrue(rec) ? true : false, PyObject_IsTrue(retAll) ? true : false);
|
||||
if (ret.size()==1 && single)
|
||||
return ret[0]->getPyObject();
|
||||
|
||||
@@ -525,10 +525,10 @@ PyObject* DocumentPy::recompute(PyObject * args)
|
||||
}
|
||||
|
||||
int options = 0;
|
||||
if (PyObject_IsTrue(checkCycle))
|
||||
if (PyObject_IsTrue(checkCycle) ? true : false)
|
||||
options = Document::DepNoCycle;
|
||||
|
||||
int objectCount = getDocumentPtr()->recompute(objs, PyObject_IsTrue(force), nullptr, options);
|
||||
int objectCount = getDocumentPtr()->recompute(objs, PyObject_IsTrue(force) ? true : false, nullptr, options);
|
||||
|
||||
// Document::recompute() hides possibly raised Python exceptions by its features
|
||||
// So, check if an error is set and return null if yes
|
||||
@@ -901,10 +901,10 @@ Py::List DocumentPy::getOutList(void) const
|
||||
|
||||
PyObject *DocumentPy::getDependentDocuments(PyObject *args) {
|
||||
PyObject *sort = Py_True;
|
||||
if (!PyArg_ParseTuple(args, "|O", &sort))
|
||||
if (!PyArg_ParseTuple(args, "|O!", &PyBool_Type, &sort))
|
||||
return nullptr;
|
||||
PY_TRY {
|
||||
auto docs = getDocumentPtr()->getDependentDocuments(PyObject_IsTrue(sort));
|
||||
auto docs = getDocumentPtr()->getDependentDocuments(PyObject_IsTrue(sort) ? true : false);
|
||||
Py::List ret;
|
||||
for (auto doc : docs)
|
||||
ret.append(Py::Object(doc->getPyObject(), true));
|
||||
|
||||
@@ -174,11 +174,11 @@ PyObject* ExtensionContainerPy::hasExtension(PyObject *args) {
|
||||
|
||||
char *type;
|
||||
PyObject *deriv = Py_True;
|
||||
if (!PyArg_ParseTuple(args, "s|O", &type, &deriv))
|
||||
if (!PyArg_ParseTuple(args, "s|O!", &type, &PyBool_Type, &deriv))
|
||||
return nullptr;
|
||||
|
||||
//get the extension type asked for
|
||||
bool derived = PyObject_IsTrue(deriv);
|
||||
bool derived = PyObject_IsTrue(deriv) ? true : false;
|
||||
Base::Type extension = Base::Type::fromName(type);
|
||||
if (extension.isBad() || !extension.isDerivedFrom(App::Extension::getExtensionClassTypeId())) {
|
||||
std::stringstream str;
|
||||
|
||||
@@ -256,12 +256,12 @@ PyObject* GroupExtensionPy::getObject(PyObject *args)
|
||||
PyObject* GroupExtensionPy::hasObject(PyObject *args)
|
||||
{
|
||||
PyObject *object;
|
||||
PyObject *recursivePy = nullptr;
|
||||
int recursive = 0;
|
||||
if (!PyArg_ParseTuple(args, "O!|O", &(DocumentObjectPy::Type), &object, &recursivePy))
|
||||
PyObject *recursivePy = Py_False;
|
||||
if (!PyArg_ParseTuple(args, "O!|O!", &(DocumentObjectPy::Type), &object, &PyBool_Type, &recursivePy))
|
||||
return nullptr;
|
||||
|
||||
DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
|
||||
bool recursive = PyObject_IsTrue(recursivePy) ? true : false;
|
||||
if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) {
|
||||
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot check an invalid object");
|
||||
return nullptr;
|
||||
@@ -270,14 +270,6 @@ PyObject* GroupExtensionPy::hasObject(PyObject *args)
|
||||
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot check an object from another document with this group");
|
||||
return nullptr;
|
||||
}
|
||||
if (recursivePy) {
|
||||
recursive = PyObject_IsTrue(recursivePy);
|
||||
if ( recursive == -1) {
|
||||
// Note: shouldn't happen
|
||||
PyErr_SetString(PyExc_ValueError, "The recursive parameter should be of boolean type");
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool v = getGroupExtensionPtr()->hasObject(docObj->getDocumentObjectPtr(), recursive);
|
||||
return PyBool_FromLong(v ? 1 : 0);
|
||||
|
||||
@@ -2065,10 +2065,9 @@ PyObject *PropertyBool::getPyObject()
|
||||
|
||||
void PropertyBool::setPyObject(PyObject *value)
|
||||
{
|
||||
if (PyBool_Check(value))
|
||||
setValue(PyObject_IsTrue(value)!=0);
|
||||
else if(PyLong_Check(value))
|
||||
setValue(PyLong_AsLong(value)!=0);
|
||||
if (PyBool_Check(value) || PyLong_Check(value)) {
|
||||
setValue(PyObject_IsTrue(value) ? true : false);
|
||||
}
|
||||
else {
|
||||
std::string error = std::string("type must be bool, not ");
|
||||
error += value->ob_type->tp_name;
|
||||
|
||||
Reference in New Issue
Block a user