diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-04-30 09:28:52 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-05-04 12:33:57 +0200 |
commit | c782714ad3e47b8a74342c95d64b69b6dd4cc270 (patch) | |
tree | ad64e1012671ebce9983faf7d4d7cc4175adcba2 /sources/shiboken2/generator | |
parent | 89074f1295b5fb7c0c620350e26527860ee7f1e0 (diff) |
Enable injecting raw code for setattro/getattro
Setattro/getattro functions can be added. As shiboken
also uses these functions internally, the code blocks
are inserted into those implementations.
As those are special functions, only raw code is injected.
No macros are replaced for arguments or instances.
Fixes: PYSIDE-1280
Change-Id: I207dcf70b3a9f5edc51ff6566b31a2a8aa4776ed
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources/shiboken2/generator')
3 files changed, 74 insertions, 6 deletions
diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index 724b390e0..166a31dfd 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -5308,6 +5308,22 @@ void CppGenerator::writeSetattroFunction(QTextStream &s, AttroCheck attroCheck, Indentation indent(INDENT); s << INDENT << "return PySide::Property::setValue(reinterpret_cast<PySideProperty *>(pp.object()), self, value);\n"; } + + if (attroCheck.testFlag(AttroCheckFlag::SetattroUser)) { + auto func = AbstractMetaClass::queryFirstFunction(metaClass->functions(), + AbstractMetaClass::SetAttroFunction); + Q_ASSERT(func); + s << INDENT << "{\n"; + { + Indentation indent(INDENT); + s << INDENT << "auto " << CPP_SELF_VAR << " = " + << cpythonWrapperCPtr(metaClass, QLatin1String("self")) << ";\n"; + writeCodeSnips(s, func->injectedCodeSnips(), TypeSystem::CodeSnipPositionAny, + TypeSystem::TargetLangCode, metaClass); + } + s << INDENT << "}\n"; + } + writeSetattroDefaultReturn(s); } @@ -5412,6 +5428,21 @@ void CppGenerator::writeGetattroFunction(QTextStream &s, AttroCheck attroCheck, } } + if (attroCheck.testFlag(AttroCheckFlag::GetattroUser)) { + auto func = AbstractMetaClass::queryFirstFunction(metaClass->functions(), + AbstractMetaClass::GetAttroFunction); + Q_ASSERT(func); + s << INDENT << "{\n"; + { + Indentation indent(INDENT); + s << INDENT << "auto " << CPP_SELF_VAR << " = " + << cpythonWrapperCPtr(metaClass, QLatin1String("self")) << ";\n"; + writeCodeSnips(s, func->injectedCodeSnips(), TypeSystem::CodeSnipPositionAny, + TypeSystem::TargetLangCode, metaClass); + } + s << INDENT << "}\n"; + } + s << INDENT << "return " << getattrFunc << ";\n}\n\n"; } diff --git a/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp b/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp index 90ae4299d..47cca8173 100644 --- a/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp @@ -1555,16 +1555,34 @@ void ShibokenGenerator::writeUnusedVariableCast(QTextStream &s, const QString &v s << INDENT << "SBK_UNUSED(" << variableName<< ")\n"; } +static bool filterFunction(const AbstractMetaFunction *func, bool avoidProtectedHack) +{ + switch (func->functionType()) { + case AbstractMetaFunction::DestructorFunction: + case AbstractMetaFunction::SignalFunction: + case AbstractMetaFunction::GetAttroFunction: + case AbstractMetaFunction::SetAttroFunction: + return false; + default: + break; + } + if (func->usesRValueReferences()) + return false; + if (func->isModifiedRemoved() && !func->isAbstract() + && (!avoidProtectedHack || !func->isProtected())) { + return false; + } + return true; +} + AbstractMetaFunctionList ShibokenGenerator::filterFunctions(const AbstractMetaClass *metaClass) { AbstractMetaFunctionList result; const AbstractMetaFunctionList &funcs = metaClass->functions(); + result.reserve(funcs.size()); for (AbstractMetaFunction *func : funcs) { - if (func->isSignal() || func->isDestructor() || func->usesRValueReferences() - || (func->isModifiedRemoved() && !func->isAbstract() - && (!avoidProtectedHack() || !func->isProtected()))) - continue; - result << func; + if (filterFunction(func, avoidProtectedHack())) + result.append(func); } return result; } @@ -2199,10 +2217,18 @@ ShibokenGenerator::AttroCheck ShibokenGenerator::checkAttroFunctionNeeds(const A } else { if (getGeneratorClassInfo(metaClass).needsGetattroFunction) result |= AttroCheckFlag::GetattroOverloads; + if (metaClass->queryFirstFunction(metaClass->functions(), + AbstractMetaClass::GetAttroFunction)) { + result |= AttroCheckFlag::GetattroUser; + } if (usePySideExtensions() && metaClass->qualifiedCppName() == QLatin1String("QObject")) result |= AttroCheckFlag::SetattroQObject; if (useOverrideCaching(metaClass)) result |= AttroCheckFlag::SetattroMethodOverride; + if (metaClass->queryFirstFunction(metaClass->functions(), + AbstractMetaClass::SetAttroFunction)) { + result |= AttroCheckFlag::SetattroUser; + } // PYSIDE-1255: If setattro is generated for a class inheriting // QObject, the property code needs to be generated, too. if ((result & AttroCheckFlag::SetattroMask) != 0 @@ -2380,7 +2406,16 @@ static void dumpFunction(AbstractMetaFunctionList lst) static bool isGroupable(const AbstractMetaFunction *func) { - if (func->isSignal() || func->isDestructor() || (func->isModifiedRemoved() && !func->isAbstract())) + switch (func->functionType()) { + case AbstractMetaFunction::DestructorFunction: + case AbstractMetaFunction::SignalFunction: + case AbstractMetaFunction::GetAttroFunction: + case AbstractMetaFunction::SetAttroFunction: + return false; + default: + break; + } + if (func->isModifiedRemoved() && !func->isAbstract()) return false; // weird operator overloads if (func->name() == QLatin1String("operator[]") || func->name() == QLatin1String("operator->")) // FIXME: what about cast operators? diff --git a/sources/shiboken2/generator/shiboken2/shibokengenerator.h b/sources/shiboken2/generator/shiboken2/shibokengenerator.h index 2ad5ffbdf..0d4b1344a 100644 --- a/sources/shiboken2/generator/shiboken2/shibokengenerator.h +++ b/sources/shiboken2/generator/shiboken2/shibokengenerator.h @@ -69,10 +69,12 @@ public: None = 0x0, GetattroOverloads = 0x01, GetattroSmartPointer = 0x02, + GetattroUser = 0x04, // Injected code GetattroMask = 0x0F, SetattroQObject = 0x10, SetattroSmartPointer = 0x20, SetattroMethodOverride = 0x40, + SetattroUser = 0x80, // Injected code SetattroMask = 0xF0, }; Q_DECLARE_FLAGS(AttroCheck, AttroCheckFlag); |