aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/generator/shiboken/cppgenerator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'sources/shiboken6/generator/shiboken/cppgenerator.cpp')
-rw-r--r--sources/shiboken6/generator/shiboken/cppgenerator.cpp18
1 files changed, 16 insertions, 2 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
index 1734237ba..64c748ca5 100644
--- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp
+++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
@@ -872,7 +872,7 @@ void CppGenerator::generateSmartPointerClass(TextStream &s, const GeneratorConte
// Write tp_s/getattro function
const auto boolCastOpt = boolCast(metaClass);
- writeSmartPointerGetattroFunction(s, classContext);
+ writeSmartPointerGetattroFunction(s, classContext, boolCastOpt);
writeSmartPointerSetattroFunction(s, classContext);
if (boolCastOpt.has_value())
@@ -6305,7 +6305,9 @@ void CppGenerator::writeGetattroFunction(TextStream &s, AttroCheck attroCheck,
s << "return " << getattrFunc << ";\n" << outdent << "}\n\n";
}
-void CppGenerator::writeSmartPointerGetattroFunction(TextStream &s, const GeneratorContext &context)
+void CppGenerator::writeSmartPointerGetattroFunction(TextStream &s,
+ const GeneratorContext &context,
+ const BoolCastFunctionOptional &boolCast)
{
Q_ASSERT(context.forSmartPointer());
const AbstractMetaClass *metaClass = context.metaClass();
@@ -6316,6 +6318,18 @@ void CppGenerator::writeSmartPointerGetattroFunction(TextStream &s, const Genera
<< indent << "return nullptr;\n" << outdent
<< "PyErr_Clear();\n";
+ if (boolCast.has_value()) {
+ writeSmartPointerCppSelfDefinition(s, context);
+ s << "if (";
+ writeNbBoolExpression(s, boolCast.value(), true /* invert */);
+ s << ") {\n" << indent
+ << R"(PyTypeObject *tp = Py_TYPE(self);
+PyErr_Format(PyExc_AttributeError, "Attempt to retrieve '%s' from null object '%s'.",
+ Shiboken::String::toCString(name), tp->tp_name);
+return nullptr;
+)" << outdent << "}\n";
+ }
+
// This generates the code which dispatches access to member functions
// and fields from the smart pointer to its pointee.
s << smartPtrComment