diff options
author | Christian Tismer <tismer@stackless.com> | 2019-08-01 15:24:00 +0200 |
---|---|---|
committer | Christian Tismer <tismer@stackless.com> | 2019-08-07 15:19:34 +0200 |
commit | 87986cf77194b995785ae06e9eff07524b711dba (patch) | |
tree | c2af02dd852c62b9464adb72ffea0bed0a6af57a /sources/shiboken2/ApiExtractor/abstractmetalang.cpp | |
parent | 21d948aa47dfe62b58286a31c729e76c9e3c13db (diff) |
Support Pointer Primitive Types by Arrays or Result Tuples
-- This change is part of the improved numpy support --
Most primitive types are handled in XML, but this was not reflected
by the signatures, error messages, doc strings and hinting stubs.
In order to enhance the information shown to be more correct,
the C++ parser part was rewritten for Python. It is written
closely to Python syntax, but keeps the existing information about
primitive types intact.
AbstractMetaType::NativePointerAsArrayPattern is now used to
mark a variable as an array. Heuristics are no longer used.
If a pointer variable is not marked as an array, the Python parser
generates a return value. If more than one value would be returned,
a result-tuple is generated.
Because we now have a deterministic categorization of types, the
"const" attribute is no more needed and the entries in mapping.py
are reduced.
A few missing <array/> markers were added.
The tool also now handles typing.List[] differently in arguments and
return types. While return types stay lists, they are for now changed
to typing.Sequence[] in argument lists.
A test was included.
These messages belong to the previous "deprecated functions" patch:
Further, QMatrixMxN.constData was removed from the typesystem
and replaced by a surrogate function that calls QMatrixMxN.data,
but also generates a warning.
The long forgotten generate_pyi.py was now published in the same
course.
Task-number: PYSIDE-795
Task-number: PYSIDE-951
Change-Id: Ia59fe4986919525a70ea7cc453c64cdf46e7fba0
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'sources/shiboken2/ApiExtractor/abstractmetalang.cpp')
-rw-r--r-- | sources/shiboken2/ApiExtractor/abstractmetalang.cpp | 63 |
1 files changed, 62 insertions, 1 deletions
diff --git a/sources/shiboken2/ApiExtractor/abstractmetalang.cpp b/sources/shiboken2/ApiExtractor/abstractmetalang.cpp index 455140e59..4ca448d50 100644 --- a/sources/shiboken2/ApiExtractor/abstractmetalang.cpp +++ b/sources/shiboken2/ApiExtractor/abstractmetalang.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2019 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of Qt for Python. @@ -268,6 +268,15 @@ QString AbstractMetaType::cppSignature() const return m_cachedCppSignature; } +QString AbstractMetaType::pythonSignature() const +{ + // PYSIDE-921: Handle container returntypes correctly. + // This is now a clean reimplementation. + if (m_cachedPythonSignature.isEmpty()) + m_cachedPythonSignature = formatPythonSignature(false); + return m_cachedPythonSignature; +} + AbstractMetaType::TypeUsagePattern AbstractMetaType::determineUsagePattern() const { if (m_typeEntry->isTemplateArgument() || m_referenceType == RValueReference) @@ -2555,6 +2564,58 @@ QString AbstractMetaType::formatSignature(bool minimal) const return result; } +QString AbstractMetaType::formatPythonSignature(bool minimal) const +{ + /* + * This is a version of the above, more suitable for Python. + * We avoid extra keywords that are not needed in Python. + * We prepend the package name, unless it is a primitive type. + * + * Primitive types like 'int', 'char' etc.: + * When we have a primitive with an indirection, we use that '*' + * character for later postprocessing, since those indirections + * need to be modified into a result tuple. + */ + QString result; + if (m_pattern == AbstractMetaType::NativePointerAsArrayPattern) + result += QLatin1String("array "); + // We no longer use the "const" qualifier for heuristics. Instead, + // NativePointerAsArrayPattern indicates when we have <array> in XML. + // if (m_typeEntry->isPrimitive() && isConstant()) + // result += QLatin1String("const "); + if (!m_typeEntry->isPrimitive() && !package().isEmpty()) + result += package() + QLatin1Char('.'); + if (isArray()) { + // Build nested array dimensions a[2][3] in correct order + result += m_arrayElementType->formatPythonSignature(true); + const int arrayPos = result.indexOf(QLatin1Char('[')); + if (arrayPos != -1) + result.insert(arrayPos, formatArraySize(m_arrayElementCount)); + else + result.append(formatArraySize(m_arrayElementCount)); + } else { + result += typeEntry()->qualifiedCppName(); + } + if (!m_instantiations.isEmpty()) { + result += QLatin1Char('['); + for (int i = 0, size = m_instantiations.size(); i < size; ++i) { + if (i > 0) + result += QLatin1String(", "); + result += m_instantiations.at(i)->formatPythonSignature(true); + } + result += QLatin1Char(']'); + } + if (m_typeEntry->isPrimitive()) + for (Indirection i : m_indirections) + result += TypeInfo::indirectionKeyword(i); + // If it is a flags type, we replace it with the full name: + // "PySide2.QtCore.Qt.ItemFlags" instead of "PySide2.QtCore.QFlags<Qt.ItemFlag>" + if (m_typeEntry->isFlags()) + result = fullName(); + result.replace(QLatin1String("::"), QLatin1String(".")); + return result; +} + bool AbstractMetaType::isCppPrimitive() const { return m_pattern == PrimitivePattern && m_typeEntry->isCppPrimitive(); |