Fix invalid Python object error

Example below led to a crash:
import Path
v = Path.Tool(name='v', diameter=5.6)
tt = Path.Tooltable()
tt.addTools(v)
attrs = tt.templateAttrs()
uu = Path.Tooltable(tt.templateAttrs())
del attrs
This commit is contained in:
wmayer
2018-03-30 18:39:55 +02:00
parent c75d1cd777
commit 3ca2a408fb

View File

@@ -391,12 +391,18 @@ void TooltablePy::setTools(Py::Dict arg)
} else {
PyErr_Clear();
Path::Tool *tool = new Path::Tool;
Path::ToolPy pyTool(tool);
if (!pyTool.setFromTemplate(value)) {
PyErr_Print();
throw Py::Exception("something went wrong");
// The 'pyTool' object must be created on the heap otherwise Python
// will fail to properly track the reference counts and aborts
// in debug mode.
Path::ToolPy* pyTool = new Path::ToolPy(tool);
PyObject* success = pyTool->setFromTemplate(value);
if (!success) {
Py_DECREF(pyTool);
throw Py::Exception();
}
getTooltablePtr()->setTool(*tool, ckey);
Py_DECREF(pyTool);
Py_DECREF(success);
}
} else {
throw Py::Exception("The dictionary can only contain int:tool pairs");