Skip to content

Commit

Permalink
Merge pull request #859 from commontk/python3-fix-python-autocomplete
Browse files Browse the repository at this point in the history
Fix Python console auto-completion
  • Loading branch information
jcfr authored Apr 15, 2019
2 parents 400ad15 + 5dc3396 commit 2e1c6b0
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 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 All @@ -206,7 +212,11 @@ int ctkPythonConsoleCompleter::parameterCountUserDefinedFunction(const QString&
PyObject* pFunction = this->PythonManager.pythonModule(pythonFunctionName);
if (PyCallable_Check(pFunction))
{
#if PY_MAJOR_VERSION >= 3
PyObject* fc = PyObject_GetAttrString(pFunction, "__code__");
#else
PyObject* fc = PyObject_GetAttrString(pFunction, "func_code");
#endif
if (fc)
{
PyObject* ac = PyObject_GetAttrString(fc, "co_argcount");
Expand All @@ -228,7 +238,11 @@ int ctkPythonConsoleCompleter::parameterCountUserDefinedClassFunction(const QStr
PyObject* pFunction = this->PythonManager.pythonObject(pythonFunctionName);
if (PyCallable_Check(pFunction))
{
#if PY_MAJOR_VERSION >= 3
PyObject* fc = PyObject_GetAttrString(pFunction, "__code__");
#else
PyObject* fc = PyObject_GetAttrString(pFunction, "func_code");
#endif
if (fc)
{
PyObject* ac = PyObject_GetAttrString(fc, "co_argcount");
Expand Down Expand Up @@ -260,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 2e1c6b0

Please sign in to comment.