aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken2/generator/shiboken2/cppgenerator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'sources/shiboken2/generator/shiboken2/cppgenerator.cpp')
-rw-r--r--sources/shiboken2/generator/shiboken2/cppgenerator.cpp82
1 files changed, 25 insertions, 57 deletions
diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp
index c1f1b21db..a6e9a00b6 100644
--- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp
+++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp
@@ -293,7 +293,7 @@ static bool isStdSetterName(QString setterName, QString propertyName)
{
return setterName.size() == propertyName.size() + 3
&& setterName.startsWith(QLatin1String("set"))
- && setterName.endsWith(propertyName.rightRef(propertyName.size() - 1))
+ && setterName.endsWith(QStringView{propertyName}.right(propertyName.size() - 1))
&& setterName.at(3) == propertyName.at(0).toUpper();
}
@@ -905,11 +905,7 @@ QString CppGenerator::virtualMethodReturn(QTextStream &s,
const QRegularExpressionMatch match = regex.match(expr, offset);
if (!match.hasMatch())
break;
-#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
const int argId = match.capturedView(1).toInt() - 1;
-#else
- const int argId = match.capturedRef(1).toInt() - 1;
-#endif
if (argId < 0 || argId > func->arguments().count()) {
qCWarning(lcShiboken, "The expression used in return value contains an invalid index.");
break;
@@ -2383,6 +2379,8 @@ void CppGenerator::writeTypeCheck(QTextStream &s, const OverloadData *overloadDa
// This condition trusts that the OverloadData object will arrange for
// PyInt type to come after the more precise numeric types (e.g. float and bool)
const AbstractMetaType *argType = overloadData->argType();
+ if (auto viewOn = argType->viewOn())
+ argType = viewOn;
bool numberType = numericTypes.count() == 1 || ShibokenGenerator::isPyInt(argType);
QString customType = (overloadData->hasArgumentTypeReplace() ? overloadData->argumentTypeReplaced() : QString());
bool rejectNull = shouldRejectNullPointerArgument(overloadData->referenceFunction(), overloadData->argPos());
@@ -2413,12 +2411,15 @@ const AbstractMetaType *CppGenerator::getArgumentType(const AbstractMetaFunction
return nullptr;
}
- const AbstractMetaType *argType = nullptr;
QString typeReplaced = func->typeReplaced(argPos);
- if (typeReplaced.isEmpty())
- argType = (argPos == 0) ? func->type() : func->arguments().at(argPos-1)->type();
- else
- argType = buildAbstractMetaTypeFromString(typeReplaced);
+ if (typeReplaced.isEmpty()) {
+ if (argPos == 0)
+ return func->type();
+ auto argType = func->arguments().at(argPos - 1)->type();
+ return argType->viewOn() ? argType->viewOn() : argType;
+ }
+
+ auto argType = buildAbstractMetaTypeFromString(typeReplaced);
if (!argType && !m_knownPythonTypes.contains(typeReplaced)) {
qCWarning(lcShiboken).noquote().nospace()
<< QString::fromLatin1("Unknown type '%1' used as argument type replacement "\
@@ -4264,11 +4265,7 @@ void CppGenerator::writeTypeAsSequenceDefinition(QTextStream &s, const AbstractM
const QString &sqName = it.key();
if (funcs[sqName].isEmpty())
continue;
- if (it.value() == QLatin1String("sq_slice"))
- s << "#ifndef IS_PY3K\n";
s << INDENT << "{Py_" << it.value() << ", (void *)" << funcs[sqName] << "},\n";
- if (it.value() == QLatin1String("sq_slice"))
- s << "#endif\n";
}
}
@@ -4349,26 +4346,17 @@ void CppGenerator::writeTypeAsNumberDefinition(QTextStream &s, const AbstractMet
if (nb[nbName].isEmpty())
continue;
- // bool is special because the field name differs on Python 2 and 3 (nb_nonzero vs nb_bool)
- // so a shiboken macro is used.
if (nbName == QLatin1String("bool")) {
- s << "#ifdef IS_PY3K\n";
s << INDENT << "{Py_nb_bool, (void *)" << nb[nbName] << "},\n";
- s << "#else\n";
- s << INDENT << "{Py_nb_nonzero, (void *)" << nb[nbName] << "},\n";
- s << "#endif\n";
} else {
bool excludeFromPy3K = nbName == QLatin1String("__div__") || nbName == QLatin1String("__idiv__");
if (!excludeFromPy3K)
s << INDENT << "{Py_" << it.value() << ", (void *)" << nb[nbName] << "},\n";
}
}
- if (!nb[QLatin1String("__div__")].isEmpty()) {
+ if (!nb[QLatin1String("__div__")].isEmpty())
s << INDENT << "{Py_nb_true_divide, (void *)" << nb[QLatin1String("__div__")] << "},\n";
- s << "#ifndef IS_PY3K\n";
- s << INDENT << "{Py_nb_divide, (void *)" << nb[QLatin1String("__div__")] << "},\n";
- s << "#endif\n";
- }
+
if (!nb[QLatin1String("__idiv__")].isEmpty()) {
s << INDENT << "// This function is unused in Python 3. We reference it here.\n";
s << INDENT << "{0, (void *)" << nb[QLatin1String("__idiv__")] << "},\n";
@@ -4817,7 +4805,10 @@ void CppGenerator::writeSignatureInfo(QTextStream &s, const AbstractMetaFunction
args << QLatin1String("self");
const AbstractMetaArgumentList &arguments = f->arguments();
for (const AbstractMetaArgument *arg : arguments) {
- QString strArg = arg->type()->pythonSignature();
+ const auto *metaType = arg->type();
+ if (auto viewOn = metaType->viewOn())
+ metaType = viewOn;
+ QString strArg = metaType->pythonSignature();
if (!arg->defaultValueExpression().isEmpty()) {
strArg += QLatin1Char('=');
QString e = arg->defaultValueExpression();
@@ -5056,21 +5047,13 @@ void CppGenerator::writeFlagsNumberMethodsDefinition(QTextStream &s, const Abstr
QString cpythonName = cpythonEnumName(cppEnum);
s << "static PyType_Slot " << cpythonName << "_number_slots[] = {\n";
- s << "#ifdef IS_PY3K\n";
s << INDENT << "{Py_nb_bool, reinterpret_cast<void *>(" << cpythonName << "__nonzero)},\n";
- s << "#else\n";
- s << INDENT << "{Py_nb_nonzero, reinterpret_cast<void *>(" << cpythonName << "__nonzero)},\n";
- s << INDENT << "{Py_nb_long, reinterpret_cast<void *>(" << cpythonName << "_long)},\n";
- s << "#endif\n";
s << INDENT << "{Py_nb_invert, reinterpret_cast<void *>(" << cpythonName << "___invert__)},\n";
s << INDENT << "{Py_nb_and, reinterpret_cast<void *>(" << cpythonName << "___and__)},\n";
s << INDENT << "{Py_nb_xor, reinterpret_cast<void *>(" << cpythonName << "___xor__)},\n";
s << INDENT << "{Py_nb_or, reinterpret_cast<void *>(" << cpythonName << "___or__)},\n";
s << INDENT << "{Py_nb_int, reinterpret_cast<void *>(" << cpythonName << "_long)},\n";
s << INDENT << "{Py_nb_index, reinterpret_cast<void *>(" << cpythonName << "_long)},\n";
- s << "#ifndef IS_PY3K\n";
- s << INDENT << "{Py_nb_long, reinterpret_cast<void *>(" << cpythonName << "_long)},\n";
- s << "#endif\n";
s << INDENT << "{0, " << NULL_PTR << "} // sentinel\n";
s << "};\n\n";
}
@@ -5097,17 +5080,10 @@ void CppGenerator::writeFlagsBinaryOperator(QTextStream &s, const AbstractMetaEn
AbstractMetaType *flagsType = buildAbstractMetaTypeFromTypeEntry(flagsEntry);
s << INDENT << "::" << flagsEntry->originalName() << " cppResult, " << CPP_SELF_VAR << ", cppArg;\n";
- s << "#ifdef IS_PY3K\n";
s << INDENT << CPP_SELF_VAR << " = static_cast<::" << flagsEntry->originalName()
<< ">(int(PyLong_AsLong(self)));\n";
s << INDENT << "cppArg = static_cast<" << flagsEntry->originalName() << ">(int(PyLong_AsLong("
- << PYTHON_ARG << ")));\n";
- s << "#else\n";
- s << INDENT << CPP_SELF_VAR << " = static_cast<::" << flagsEntry->originalName()
- << ">(int(PyInt_AsLong(self)));\n";
- s << INDENT << "cppArg = static_cast<" << flagsEntry->originalName()
- << ">(int(PyInt_AsLong(" << PYTHON_ARG << ")));\n";
- s << "#endif\n\n";
+ << PYTHON_ARG << ")));\n\n";
s << INDENT << "cppResult = " << CPP_SELF_VAR << " " << cppOpName << " cppArg;\n";
s << INDENT << "return ";
writeToPythonConversion(s, flagsType, nullptr, QLatin1String("cppResult"));
@@ -5984,7 +5960,6 @@ bool CppGenerator::finishGeneration()
s << Qt::endl;
}
- s << "#ifdef IS_PY3K\n";
s << "static struct PyModuleDef moduledef = {\n";
s << " /* m_base */ PyModuleDef_HEAD_INIT,\n";
s << " /* m_name */ \"" << moduleName() << "\",\n";
@@ -5996,14 +5971,14 @@ bool CppGenerator::finishGeneration()
s << " /* m_clear */ nullptr,\n";
s << " /* m_free */ nullptr\n";
s << "};\n\n";
- s << "#endif\n\n";
// PYSIDE-510: Create a signatures string for the introspection feature.
writeSignatureStrings(s, signatureStream, moduleName(), "global functions");
- s << "SBK_MODULE_INIT_FUNCTION_BEGIN(" << moduleName() << ")\n";
+ s << "extern \"C\" LIBSHIBOKEN_EXPORT PyObject *PyInit_"
+ << moduleName() << "()\n{\n";
- ErrorCode errorCode(QLatin1String("SBK_MODULE_INIT_ERROR"));
+ ErrorCode errorCode(QLatin1String("nullptr"));
// module inject-code target/beginning
if (!snips.isEmpty())
writeCodeSnips(s, snips, TypeSystem::CodeSnipPositionBeginning, TypeSystem::TargetLangCode);
@@ -6012,7 +5987,7 @@ bool CppGenerator::finishGeneration()
s << INDENT << "{\n" << indent(INDENT)
<< INDENT << "Shiboken::AutoDecRef requiredModule(Shiboken::Module::import(\"" << requiredModule << "\"));\n"
<< INDENT << "if (requiredModule.isNull())\n" << indent(INDENT)
- << INDENT << "return SBK_MODULE_INIT_ERROR;\n" << outdent(INDENT)
+ << INDENT << "return nullptr;\n" << outdent(INDENT)
<< INDENT << cppApiVariableName(requiredModule)
<< " = Shiboken::Module::getTypes(requiredModule);\n"
<< INDENT << convertersVariableName(requiredModule)
@@ -6031,12 +6006,7 @@ bool CppGenerator::finishGeneration()
s << INDENT << "static SbkConverter *sbkConverters[SBK_" << moduleName() << "_CONVERTERS_IDX_COUNT" << "];\n";
s << INDENT << convertersVariableName() << " = sbkConverters;\n\n";
- s << "#ifdef IS_PY3K\n";
- s << INDENT << "PyObject *module = Shiboken::Module::create(\"" << moduleName() << "\", &moduledef);\n";
- s << "#else\n";
- s << INDENT << "PyObject *module = Shiboken::Module::create(\"" << moduleName() << "\", ";
- s << moduleName() << "_methods);\n";
- s << "#endif\n\n";
+ s << INDENT << "PyObject *module = Shiboken::Module::create(\"" << moduleName() << "\", &moduledef);\n\n";
s << INDENT << "// Make module available from global scope\n";
s << INDENT << pythonModuleObjectName() << " = module;\n\n";
@@ -6120,10 +6090,8 @@ bool CppGenerator::finishGeneration()
// finish the rest of __signature__ initialization.
s << INDENT << "FinishSignatureInitialization(module, " << moduleName()
- << "_SignatureStrings);\n";
-
- s << Qt::endl;
- s << "SBK_MODULE_INIT_FUNCTION_END\n";
+ << "_SignatureStrings);\n"
+ << INDENT << "\nreturn module;\n}\n";
return file.done() != FileOut::Failure;
}