aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-04-04 11:04:11 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2022-04-04 15:14:41 +0200
commit04544e58cdf32049579b8081cac44e8b3ad17171 (patch)
treecb1a577b19a66a788885d38a10474efa34c9f95e
parentf9460f25543135e9b436285cbf25e30dc22c57a8 (diff)
shiboken6: Refactor the smart pointer get/setattro methods
Streamline the CppGenerator functions as well as the code they generate. Pick-to: 6.2 Task-number: PYSIDE-454 Change-Id: If789b432b3edcdedac0b062241d86b8a8dc9b332 Reviewed-by: Christian Tismer <tismer@stackless.com>
-rw-r--r--sources/shiboken6/generator/shiboken/cppgenerator.cpp75
1 files changed, 27 insertions, 48 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
index f972771e3..a8a16d29a 100644
--- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp
+++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
@@ -807,7 +807,7 @@ void CppGenerator::generateSmartPointerClass(TextStream &s, const GeneratorConte
// Create string literal for smart pointer getter method.
QString rawGetter = typeEntry->getter();
- s << "static const char * " << SMART_POINTER_GETTER << " = \"" << rawGetter << "\";";
+ s << "static const char " << SMART_POINTER_GETTER << "[] = \"" << rawGetter << "\";";
// class inject-code native/beginning
if (!typeEntry->codeSnips().isEmpty()) {
@@ -6154,26 +6154,22 @@ void CppGenerator::writeSetattroFunction(TextStream &s, AttroCheck attroCheck,
writeSetattroDefaultReturn(s);
}
+static const char smartPtrComment[] =
+ "// Try to find the 'name' attribute, by retrieving the PyObject for "
+ "the corresponding C++ object held by the smart pointer.\n";
+
void CppGenerator::writeSmartPointerSetattroFunction(TextStream &s,
const GeneratorContext &context) const
{
Q_ASSERT(context.forSmartPointer());
writeSetattroDefinition(s, context.metaClass());
- s << "// Try to find the 'name' attribute, by retrieving the PyObject for the corresponding C++ object held by the smart pointer.\n"
- << "PyObject *rawObj = PyObject_CallMethod(self, "
- << SMART_POINTER_GETTER << ", 0);\n";
- s << "if (rawObj) {\n";
- {
- Indentation indent(s);
- s << "int hasAttribute = PyObject_HasAttr(rawObj, name);\n"
- << "if (hasAttribute) {\n";
- {
- Indentation indent(s);
- s << "return PyObject_GenericSetAttr(rawObj, name, value);\n";
- }
- s << "}\nPy_DECREF(rawObj);\n";
- }
- s << "}\n";
+ s << smartPtrComment
+ << "if (auto *rawObj = PyObject_CallMethod(self, " << SMART_POINTER_GETTER
+ << ", 0)) {\n" << indent
+ << "if (PyObject_HasAttr(rawObj, name) != 0)\n" << indent
+ << "return PyObject_GenericSetAttr(rawObj, name, value);\n" << outdent
+ << "Py_DECREF(rawObj);\n" << outdent
+ << "}\n";
writeSetattroDefaultReturn(s);
}
@@ -6285,44 +6281,27 @@ void CppGenerator::writeSmartPointerGetattroFunction(TextStream &s, const Genera
const AbstractMetaClass *metaClass = context.metaClass();
writeGetattroDefinition(s, metaClass);
s << "PyObject *tmp = PyObject_GenericGetAttr(self, name);\n"
- << "if (tmp)\n";
- {
- Indentation indent(s);
- s << "return tmp;\n";
- }
- s << "if (!PyErr_ExceptionMatches(PyExc_AttributeError))\n";
- {
- Indentation indent(s);
- s << "return nullptr;\n";
- }
- s << "PyErr_Clear();\n";
+ << "if (tmp)\n" << indent << "return tmp;\n" << outdent
+ << "if (PyErr_ExceptionMatches(PyExc_AttributeError) == 0)\n"
+ << indent << "return nullptr;\n" << outdent
+ << "PyErr_Clear();\n";
// This generates the code which dispatches access to member functions
// and fields from the smart pointer to its pointee.
- s << "// Try to find the 'name' attribute, by retrieving the PyObject for "
- "the corresponding C++ object held by the smart pointer.\n"
- << "if (auto rawObj = PyObject_CallMethod(self, "
- << SMART_POINTER_GETTER << ", 0)) {\n";
- {
- Indentation indent(s);
- s << "if (auto attribute = PyObject_GetAttr(rawObj, name))\n";
- {
- Indentation indent(s);
- s << "tmp = attribute;\n";
- }
- s << "Py_DECREF(rawObj);\n";
- }
- s << "}\n"
- << "if (!tmp) {\n";
- {
- Indentation indent(s);
- s << R"(PyTypeObject *tp = Py_TYPE(self);
+ s << smartPtrComment
+ << "if (auto *rawObj = PyObject_CallMethod(self, "
+ << SMART_POINTER_GETTER << ", 0)) {\n" << indent
+ << "if (auto *attribute = PyObject_GetAttr(rawObj, name))\n"
+ << indent << "tmp = attribute;\n" << outdent
+ << "Py_DECREF(rawObj);\n" << outdent
+ << "}\n"
+ << "if (!tmp) {\n" << indent
+ << R"(PyTypeObject *tp = Py_TYPE(self);
PyErr_Format(PyExc_AttributeError,
"'%.50s' object has no attribute '%.400s'",
tp->tp_name, Shiboken::String::toCString(name));
-)";
- }
- s << "}\n"
+)" << outdent
+ << "}\n"
<< "return tmp;\n" << outdent << "}\n\n";
}