aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/generator/shiboken/cppgenerator.cpp
diff options
context:
space:
mode:
authorChristian Tismer <tismer@stackless.com>2021-02-12 19:13:43 +0100
committerChristian Tismer <tismer@stackless.com>2021-02-22 13:13:37 +0100
commit45f37d5e16fff807d612f8eedb5496f4579481cc (patch)
tree5a90431d3bff9ad99709cf8e2813b8b203b5b1b4 /sources/shiboken6/generator/shiboken/cppgenerator.cpp
parent846f8a6b8606c6ae1cead76b8ba5e47e0bea5355 (diff)
cppgenerator: simplify code further using AutoDecref
Shiboken::AutoDecref is an increadible handy tool to simplify reference counted code. One missing spot was to be able to declare an AutoDecref variable without specifying a value for later use by reset(). A workaround was this code: static PyObject *_dummy{}; Shiboken::AutoDecref errCode(_dummy); ... errCode.reset(someVar); We now allow AutoDecref without an expression and can write Shiboken::AutoDecref errCode{}; ... errCode.reset(someVar); That construct is used to further simplify the generated code quite much. Change-Id: I2ae8f2214c0fbaae0a935aa0b8d69b4a7922a6c8 Task-number: PYSIDE-1499 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'sources/shiboken6/generator/shiboken/cppgenerator.cpp')
-rw-r--r--sources/shiboken6/generator/shiboken/cppgenerator.cpp39
1 files changed, 14 insertions, 25 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
index 97e61168a..426f7f6fd 100644
--- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp
+++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
@@ -1814,8 +1814,7 @@ void CppGenerator::writeMethodWrapperPreamble(TextStream &s, OverloadData &overl
initPythonArguments = minArgs != maxArgs || maxArgs > 1;
}
- s << R"(PyObject *errInfo{};
-SBK_UNUSED(errInfo)
+ s << R"(Shiboken::AutoDecRef errInfo{};
static const char *fullName = ")" << fullPythonFunctionName(rfunc, true)
<< "\";\nSBK_UNUSED(fullName)\n";
if (maxArgs > 0) {
@@ -1901,8 +1900,6 @@ void CppGenerator::writeConstructorWrapper(TextStream &s, const AbstractMetaFunc
{
Indentation indent(s);
s << "delete cptr;\n";
- if (overloadData.maxArgs() > 0)
- s << "Py_XDECREF(errInfo);\n";
s << returnStatement(m_currentErrorCode) << '\n';
}
s << "}\n";
@@ -1933,10 +1930,9 @@ void CppGenerator::writeConstructorWrapper(TextStream &s, const AbstractMetaFunc
s << "\n// QObject setup\n"
<< "PySide::Signal::updateSourceObject(self);\n"
<< "metaObject = cptr->metaObject(); // <- init python qt properties\n"
- << "if (errInfo && PyDict_Check(errInfo)) {\n" << indent
- << "if (!PySide::fillQtProperties(self, metaObject, errInfo))\n" << indent
- << "goto " << cpythonFunctionName(rfunc) << "_TypeError;\n" << outdent
- << "Py_DECREF(errInfo);\n" << outdent
+ << "if (!errInfo.isNull() && PyDict_Check(errInfo.object())) {\n" << indent
+ << "if (!PySide::fillQtProperties(self, metaObject, errInfo))\n" << indent
+ << "goto " << cpythonFunctionName(rfunc) << "_TypeError;\n" << outdent << outdent
<< "};\n";
}
@@ -2127,8 +2123,8 @@ void CppGenerator::writeArgumentsInitializer(TextStream &s, OverloadData &overlo
Indentation indent(s);
s << "static PyObject *const too_many = "
"Shiboken::String::createStaticString(\">\");\n"
- << "errInfo = too_many;\n"
- << "Py_INCREF(errInfo);\n"
+ << "errInfo.reset(too_many);\n"
+ << "Py_INCREF(errInfo.object());\n"
<< "goto " << cpythonFunctionName(rfunc) << "_TypeError;\n";
}
s << '}';
@@ -2141,8 +2137,8 @@ void CppGenerator::writeArgumentsInitializer(TextStream &s, OverloadData &overlo
Indentation indent(s);
s << "static PyObject *const too_few = "
"Shiboken::String::createStaticString(\"<\");\n"
- << "errInfo = too_few;\n"
- << "Py_INCREF(errInfo);\n"
+ << "errInfo.reset(too_few);\n"
+ << "Py_INCREF(errInfo.object());\n"
<< "goto " << cpythonFunctionName(rfunc) << "_TypeError;\n";
}
s << '}';
@@ -2283,9 +2279,7 @@ void CppGenerator::writeErrorSection(TextStream &s, OverloadData &overloadData)
Indentation indentation(s);
QString argsVar = pythonFunctionWrapperUsesListOfArguments(overloadData)
? QLatin1String("args") : QLatin1String(PYTHON_ARG);
- s << "Shiboken::setErrorAboutWrongArguments(" << argsVar
- << ", fullName, errInfo);\n"
- << "Py_XDECREF(errInfo);\n"
+ s << "Shiboken::setErrorAboutWrongArguments(" << argsVar << ", fullName, errInfo);\n"
<< "return " << m_currentErrorCode << ";\n";
}
@@ -3248,8 +3242,8 @@ void CppGenerator::writeNamedArgumentResolution(TextStream &s, const AbstractMet
s << "if (kwds) {\n";
{
Indentation indent(s);
- s << "errInfo = kwds;\n"
- << "Py_INCREF(errInfo);\n"
+ s << "errInfo.reset(kwds);\n"
+ << "Py_INCREF(errInfo.object());\n"
<< "goto " << cpythonFunctionName(func) << "_TypeError;\n";
}
s << "}\n";
@@ -3276,8 +3270,8 @@ void CppGenerator::writeNamedArgumentResolution(TextStream &s, const AbstractMet
<< "if (value && " << pyArgName << ") {\n";
{
Indentation indent(s);
- s << "errInfo = " << pyKeyName << ";\n"
- << "Py_INCREF(errInfo);\n"
+ s << "errInfo.reset(" << pyKeyName << ");\n"
+ << "Py_INCREF(errInfo.object());\n"
<< "goto " << cpythonFunctionName(func) << "_TypeError;\n";
}
s << "}\nif (value) {\n";
@@ -3303,17 +3297,12 @@ void CppGenerator::writeNamedArgumentResolution(TextStream &s, const AbstractMet
s << "if (PyDict_Size(kwds_dup) > 0) {\n";
{
Indentation indent(s);
- s << "errInfo = kwds_dup;\n";
+ s << "errInfo.reset(kwds_dup);\n";
if (!(func->isConstructor() && func->ownerClass()->isQObject()))
s << "goto " << cpythonFunctionName(func) << "_TypeError;\n";
else
s << "// fall through to handle extra keyword signals and properties\n";
}
- s << "} else {\n";
- {
- Indentation indent(s);
- s << "Py_DECREF(kwds_dup);\n";
- }
s << "}\n";
}
s << "}\n";