From 56c24b5d1aa507c62bdf3af917519e6c7cf7718c Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 17 Sep 2021 11:36:38 +0200 Subject: shiboken6: Add built-in CPython types Add the CPython types along with their check functions. Introduce a new Python type entry for this. [ChangeLog][shiboken6] CPython types like PyObject, PySequence are now built into shiboken6 and no longer need to be specified in the typesystem files. Task-number: PYSIDE-1660 Change-Id: Ia2a7e5445c11b99cae069818aa5b0e1aa169533c Reviewed-by: Christian Tismer Reviewed-by: Cristian Maureira-Fredes --- .../PySide6/QtCore/typesystem_core_common.xml | 9 ---- .../PySide6/QtGui/typesystem_gui_common.xml | 2 - sources/shiboken6/ApiExtractor/typedatabase.cpp | 43 ++++++++++++++++ sources/shiboken6/ApiExtractor/typesystem.cpp | 58 +++++++++++++++++++++- sources/shiboken6/ApiExtractor/typesystem.h | 24 +++++++++ sources/shiboken6/ApiExtractor/typesystem_enums.h | 9 ++++ .../shibokenmodule/typesystem_shiboken.xml | 4 +- .../tests/samplebinding/typesystem_sample.xml | 6 --- 8 files changed, 134 insertions(+), 21 deletions(-) diff --git a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml index 30d5f3d7b..8e5b3811b 100644 --- a/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml +++ b/sources/pyside6/PySide6/QtCore/typesystem_core_common.xml @@ -42,15 +42,6 @@ - - - - - - - - - diff --git a/sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml b/sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml index 96fc37c88..275359ff8 100644 --- a/sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml +++ b/sources/pyside6/PySide6/QtGui/typesystem_gui_common.xml @@ -45,8 +45,6 @@ - - diff --git a/sources/shiboken6/ApiExtractor/typedatabase.cpp b/sources/shiboken6/ApiExtractor/typedatabase.cpp index 6b63d2052..81784c098 100644 --- a/sources/shiboken6/ApiExtractor/typedatabase.cpp +++ b/sources/shiboken6/ApiExtractor/typedatabase.cpp @@ -57,10 +57,53 @@ using ApiVersions = QList; Q_GLOBAL_STATIC(ApiVersions, apiVersions) +struct PythonType +{ + QString name; + QString checkFunction; + TypeSystem::CPythonType type; +}; + +using PythonTypes = QList; + +static const PythonTypes &builtinPythonTypes() +{ + static const PythonTypes result{ + // "Traditional" custom types + // numpy + {u"PyArrayObject"_qs, u"PyArray_Check"_qs, TypeSystem::CPythonType::Other}, + {u"PyBuffer"_qs, u"Shiboken::Buffer::checkType"_qs, TypeSystem::CPythonType::Other}, + {u"PyByteArray"_qs, u"PyByteArray_Check"_qs, TypeSystem::CPythonType::Other}, + {u"PyBytes"_qs, u"PyBytes_Check"_qs, TypeSystem::CPythonType::Other}, + {u"PyCallable"_qs, u"PyCallable_Check"_qs, TypeSystem::CPythonType::Other}, + {u"PyDate"_qs, u"PyDate_Check"_qs, TypeSystem::CPythonType::Other}, + {u"PyDateTime"_qs, u"PyDateTime_Check_Check"_qs, TypeSystem::CPythonType::Other}, + {u"PyDict"_qs, u"PyDict_Check"_qs, TypeSystem::CPythonType::Other}, + // Convenience macro in sbkconverter.h + {u"PyObject"_qs, u"PyObject_Check"_qs, TypeSystem::CPythonType::Other}, + // shiboken-specific + {u"PyPathLike"_qs, u"Shiboken::String::checkPath"_qs, TypeSystem::CPythonType::Other}, + {u"PySequence"_qs, u"Shiboken::String::checkIterable"_qs, TypeSystem::CPythonType::Other}, + {u"PyUnicode"_qs, u"PyUnicode_Check"_qs, TypeSystem::CPythonType::String}, + {u"PyTypeObject"_qs, u"PyType_Check"_qs, TypeSystem::CPythonType::Other}, + {u"str"_qs, u"Shiboken::String::check"_qs, TypeSystem::CPythonType::String}, + // Types used as target lang API types for primitive types + {u"PyBool"_qs, u"PyBool_Check"_qs, TypeSystem::CPythonType::Bool}, + {u"PyComplex"_qs, u"PyComplex_Check"_qs, TypeSystem::CPythonType::Other}, + {u"PyLong"_qs, u"PyLong_Check"_qs, TypeSystem::CPythonType::Integer}, + {u"PyFloat"_qs, u"PyFloat_Check"_qs, TypeSystem::CPythonType::Float}, + // Single character strings to match C++ char types + {u"SbkChar"_qs, u"SbkChar_Check"_qs, TypeSystem::CPythonType::String} + }; + return result; +} + TypeDatabase::TypeDatabase() { addBuiltInType(new VoidTypeEntry()); addBuiltInType(new VarargsTypeEntry()); + for (const auto &pt : builtinPythonTypes()) + addBuiltInType(new PythonTypeEntry(pt.name, pt.checkFunction, pt.type)); } TypeDatabase::~TypeDatabase() = default; diff --git a/sources/shiboken6/ApiExtractor/typesystem.cpp b/sources/shiboken6/ApiExtractor/typesystem.cpp index b01281da6..298baa7d7 100644 --- a/sources/shiboken6/ApiExtractor/typesystem.cpp +++ b/sources/shiboken6/ApiExtractor/typesystem.cpp @@ -318,7 +318,7 @@ bool TypeEntry::isVarargs() const bool TypeEntry::isCustom() const { - return m_d->m_type == CustomType; + return m_d->m_type == CustomType || m_d->m_type == PythonType; } bool TypeEntry::isTypeSystem() const @@ -693,6 +693,47 @@ void CustomTypeEntry::setCheckFunction(const QString &f) d->m_checkFunction = f; } +// ----------------- PythonTypeEntry +class PythonTypeEntryPrivate : public CustomTypeEntryPrivate +{ +public: + using CustomTypeEntryPrivate::CustomTypeEntryPrivate; + explicit PythonTypeEntryPrivate(const QString &entryName, + const QString &checkFunction, + TypeSystem::CPythonType type) : + CustomTypeEntryPrivate(entryName, TypeEntry::PythonType, {}, {}), + m_cPythonType(type) + { + m_checkFunction = checkFunction; + } + + TypeSystem::CPythonType m_cPythonType; +}; + +PythonTypeEntry::PythonTypeEntry(const QString &entryName, + const QString &checkFunction, + TypeSystem::CPythonType type) : + CustomTypeEntry(new PythonTypeEntryPrivate(entryName, checkFunction, type)) +{ +} + +TypeEntry *PythonTypeEntry::clone() const +{ + S_D(const PythonTypeEntry); + return new PythonTypeEntry(new PythonTypeEntryPrivate(*d)); +} + +TypeSystem::CPythonType PythonTypeEntry::cPythonType() const +{ + S_D(const PythonTypeEntry); + return d->m_cPythonType; +} + +PythonTypeEntry::PythonTypeEntry(TypeEntryPrivate *d) : + CustomTypeEntry(d) +{ +} + // ----------------- TypeSystemTypeEntry class TypeSystemTypeEntryPrivate : public TypeEntryPrivate { @@ -2212,6 +2253,21 @@ void ComplexTypeEntry::formatDebug(QDebug &debug) const FORMAT_LIST_SIZE("fieldMods", d->m_fieldMods) } +void CustomTypeEntry::formatDebug(QDebug &debug) const +{ + S_D(const CustomTypeEntry); + TypeEntry::formatDebug(debug); + debug << ", checkFunction=" << d->m_checkFunction; +} + +void PythonTypeEntry::formatDebug(QDebug &debug) const +{ + S_D(const PythonTypeEntry); + + CustomTypeEntry::formatDebug(debug); + debug << ", type=" << int(d->m_cPythonType); +} + void FunctionTypeEntry::formatDebug(QDebug &debug) const { S_D(const FunctionTypeEntry); diff --git a/sources/shiboken6/ApiExtractor/typesystem.h b/sources/shiboken6/ApiExtractor/typesystem.h index 1ab27b4f4..fcd0abe99 100644 --- a/sources/shiboken6/ApiExtractor/typesystem.h +++ b/sources/shiboken6/ApiExtractor/typesystem.h @@ -102,6 +102,7 @@ public: ArrayType, TypeSystemType, CustomType, + PythonType, FunctionType, SmartPointerType, TypedefType @@ -297,10 +298,33 @@ public: QString checkFunction() const; void setCheckFunction(const QString &f); +#ifndef QT_NO_DEBUG_STREAM + void formatDebug(QDebug &d) const override; +#endif + protected: explicit CustomTypeEntry(TypeEntryPrivate *d); }; +class PythonTypeEntry : public CustomTypeEntry +{ +public: + explicit PythonTypeEntry(const QString &entryName, + const QString &checkFunction, + TypeSystem::CPythonType type); + + TypeEntry *clone() const override; + + TypeSystem::CPythonType cPythonType() const; + +#ifndef QT_NO_DEBUG_STREAM + void formatDebug(QDebug &d) const override; +#endif + +protected: + explicit PythonTypeEntry(TypeEntryPrivate *d); +}; + class TypeSystemTypeEntry : public TypeEntry { public: diff --git a/sources/shiboken6/ApiExtractor/typesystem_enums.h b/sources/shiboken6/ApiExtractor/typesystem_enums.h index 1a972ec1f..d2b719e85 100644 --- a/sources/shiboken6/ApiExtractor/typesystem_enums.h +++ b/sources/shiboken6/ApiExtractor/typesystem_enums.h @@ -98,6 +98,15 @@ enum class BoolCast { // Generate nb_bool (overriding command line) Enabled }; +enum class CPythonType +{ + Bool, + Float, + Integer, + String, + Other +}; + enum : int { OverloadNumberUnset = -1, OverloadNumberDefault = 99999 }; } // namespace TypeSystem diff --git a/sources/shiboken6/shibokenmodule/typesystem_shiboken.xml b/sources/shiboken6/shibokenmodule/typesystem_shiboken.xml index 74f9d0856..1bee3f543 100644 --- a/sources/shiboken6/shibokenmodule/typesystem_shiboken.xml +++ b/sources/shiboken6/shibokenmodule/typesystem_shiboken.xml @@ -1,7 +1,5 @@ - - @@ -18,7 +16,7 @@ - + auto *pyType = reinterpret_cast<PyTypeObject *>(%2); if (Shiboken::ObjectType::checkType(pyType)) { diff --git a/sources/shiboken6/tests/samplebinding/typesystem_sample.xml b/sources/shiboken6/tests/samplebinding/typesystem_sample.xml index 95c51938e..1c1b87914 100644 --- a/sources/shiboken6/tests/samplebinding/typesystem_sample.xml +++ b/sources/shiboken6/tests/samplebinding/typesystem_sample.xml @@ -3,12 +3,6 @@ - - - - - - -- cgit v1.2.3