Skip to content

Commit

Permalink
BUG: Python3: Fix python console auto-completion
Browse files Browse the repository at this point in the history
This commit fixes ctkPythonConsoleCompleter. The failure to check the
value returned by PyObject_GetAttrString caused the error flag to be set
and led to an inconsistent completer state. This was a problem only when
built against Python3.

Instead of explicitly checking the type of value returned by "PyObject_GetAttrString()",
the function "PyErr_Print()" could have been called after the call to
"PyObject_GetAttrString". This would have also cleared the internal error
flag was not cleared and printed the following error:

  TypeError: bad argument type for built-in operation
  • Loading branch information
jcfr committed Apr 15, 2019
1 parent e06346e commit 5350a2c
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions Libs/Scripting/Python/Widgets/ctkPythonConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,20 @@ int ctkPythonConsoleCompleter::parameterCountBuiltInFunction(const QString& pyth
{
int parameterCount = 0;
PyObject* pFunction = this->PythonManager.pythonModule(pythonFunctionName);
if (pFunction && PyObject_HasAttrString(pFunction, "__doc__"))
if (pFunction)
{
PyObject* pDoc = PyObject_GetAttrString(pFunction, "__doc__");
QString docString = PyString_AsString(pDoc);
QString argumentExtract = docString.mid(docString.indexOf("(")+1, docString.indexOf(")") - docString.indexOf("(")-1);
QStringList arguments = argumentExtract.split(",", QString::SkipEmptyParts);
parameterCount = arguments.count();
Py_DECREF(pDoc);
if (PyObject_HasAttrString(pFunction, "__doc__"))
{
PyObject* pDoc = PyObject_GetAttrString(pFunction, "__doc__");
if (PyString_Check(pDoc))
{
QString docString = PyString_AsString(pDoc);
QString argumentExtract = docString.mid(docString.indexOf("(")+1, docString.indexOf(")") - docString.indexOf("(")-1);
QStringList arguments = argumentExtract.split(",", QString::SkipEmptyParts);
parameterCount = arguments.count();
}
Py_DECREF(pDoc);
}
Py_DECREF(pFunction);
}
return parameterCount;
Expand Down Expand Up @@ -268,6 +274,7 @@ int ctkPythonConsoleCompleter::parameterCountFromDocumentation(const QString& py
QStringList arguments = argumentExtract.split(",", QString::SkipEmptyParts);
parameterCount = arguments.count();
}
Py_DECREF(pDoc);
}
Py_DECREF(pFunction);
}
Expand Down

0 comments on commit 5350a2c

Please sign in to comment.