From cebcb7f66c79e6976b5cc262f9b0e26bc856a6ed Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 7 Dec 2024 19:55:49 +0100 Subject: [PATCH] Sketch: Fix SketchObjectPy::addExternal Use PyArg_ParseTuple's support of optional arguments than doing it with several calls of PyArg_ParseTuple with different arguments. This is error-prone, leads to convoluted code logic and may even fail because once the error indicator is set PyArg_ParseTuple may unexpectedly fail. --- src/Mod/Sketcher/App/SketchObjectPyImp.cpp | 33 ++++++---------------- 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/src/Mod/Sketcher/App/SketchObjectPyImp.cpp b/src/Mod/Sketcher/App/SketchObjectPyImp.cpp index 67550b5cb5..f6dd98a1ce 100644 --- a/src/Mod/Sketcher/App/SketchObjectPyImp.cpp +++ b/src/Mod/Sketcher/App/SketchObjectPyImp.cpp @@ -555,39 +555,24 @@ PyObject* SketchObjectPy::carbonCopy(PyObject* args) PyObject* SketchObjectPy::addExternal(PyObject* args) { - char* ObjectName; - char* SubName; - PyObject* defining; // this is an optional argument default false - PyObject* intersection; // this is an optional argument default false - bool isDefining; - bool isIntersection; + char* ObjectName = nullptr; + char* SubName = nullptr; + PyObject* defining = Py_False; + PyObject* intersection = Py_False; if (!PyArg_ParseTuple(args, - "ssO!O!", + "ss|O!O!", &ObjectName, &SubName, &PyBool_Type, &defining, &PyBool_Type, &intersection)) { - if (!PyArg_ParseTuple(args, "ssO!", &ObjectName, &SubName, &PyBool_Type, &defining)) { - PyErr_Clear(); - if (!PyArg_ParseTuple(args, "ss", &ObjectName, &SubName)) { - return nullptr; - } - else { - isDefining = false; - } - } - else { - isDefining = Base::asBoolean(defining); - } - isIntersection = false; - } - else { - isDefining = Base::asBoolean(defining); - isIntersection = Base::asBoolean(intersection); + return nullptr; } + bool isDefining = Base::asBoolean(defining); + bool isIntersection = Base::asBoolean(intersection); + // get the target object for the external link Sketcher::SketchObject* skObj = this->getSketchObjectPtr(); App::DocumentObject* Obj = skObj->getDocument()->getObject(ObjectName);