diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-09-17 11:36:38 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-09-24 13:52:35 +0200 |
commit | 56c24b5d1aa507c62bdf3af917519e6c7cf7718c (patch) | |
tree | a94522e345d17e5492bd49a82a4089038fb7ebaa | |
parent | bce1bfb3af99aeb24259df34d662e8fcf072d3fd (diff) |
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 <tismer@stackless.com>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
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 @@ <typesystem package="PySide6.QtCore"> <load-typesystem name="templates/core_common.xml" generate="no"/> - <custom-type name="str"/> - <custom-type name="PyBytes"/> - <custom-type name="PyByteArray"/> - <custom-type name="PyCallable"/> - <custom-type name="PyObject"/> - <custom-type name="PyPathLike"/> - <custom-type name="PySequence"/> - <custom-type name="PyTypeObject"/> - <custom-type name="PyUnicode"/> <custom-type name="list of QAbstractAnimation"/> <custom-type name="PySideSignalInstance" check-function="PySide::Signal::checkInstanceType"/> 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 @@ <load-typesystem name="templates/gui_common.xml" generate="no"/> <load-typesystem name="templates/opengl_common.xml" generate="no"/> - <custom-type name="PyArrayObject"/> - <?if !darwin?> <?entity GLint int?> <?entity GLuint unsigned int?> 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<ApiVersion>; Q_GLOBAL_STATIC(ApiVersions, apiVersions) +struct PythonType +{ + QString name; + QString checkFunction; + TypeSystem::CPythonType type; +}; + +using PythonTypes = QList<PythonType>; + +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 @@ <?xml version="1.0" ?> <typesystem package="Shiboken"> - <custom-type name="PyObject" /> - <custom-type name="PyType" /> <primitive-type name="bool" /> <primitive-type name="unsigned long" /> <primitive-type name="size_t" /> @@ -18,7 +16,7 @@ </inject-code> </add-function> - <add-function signature="wrapInstance(size_t, PyType)" return-type="PyObject*"> + <add-function signature="wrapInstance(size_t, PyTypeObject)" return-type="PyObject*"> <inject-code> 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 @@ <suppress-warning text="Duplicate type entry: 'sample'" /> <suppress-warning text="Duplicate type entry: 'SampleNamespace'" /> - <custom-type name="str"/> - <custom-type name="PyBytes"/> - <custom-type name="PyDate"/> - <custom-type name="PyObject"/> - <custom-type name="PyUnicode"/> - <primitive-type name="bool"/> <primitive-type name="double"/> <primitive-type name="real"/> |