diff options
Diffstat (limited to 'sources/pyside6/PySide6/QtCore/glue/core_snippets.cpp')
-rw-r--r-- | sources/pyside6/PySide6/QtCore/glue/core_snippets.cpp | 149 |
1 files changed, 110 insertions, 39 deletions
diff --git a/sources/pyside6/PySide6/QtCore/glue/core_snippets.cpp b/sources/pyside6/PySide6/QtCore/glue/core_snippets.cpp index 5af0b804a..f6acf9d60 100644 --- a/sources/pyside6/PySide6/QtCore/glue/core_snippets.cpp +++ b/sources/pyside6/PySide6/QtCore/glue/core_snippets.cpp @@ -1,48 +1,17 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of Qt for Python. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "core_snippets_p.h" +#include "qtcorehelper.h" #include "pysideqobject.h" #include "shiboken.h" +#ifndef Py_LIMITED_API +# include <datetime.h> +#endif #include "basewrapper.h" #include "autodecref.h" +#include "pysideutils.h" #include <QtCore/QCoreApplication> #include <QtCore/QDebug> @@ -98,7 +67,8 @@ QVariant QVariant_convertToValueList(PyObject *list) Shiboken::AutoDecRef element(PySequence_GetItem(list, 0)); - QMetaType metaType = QVariant_resolveMetaType(element.cast<PyTypeObject *>()); + auto *type = reinterpret_cast<PyTypeObject *>(element.object()); + QMetaType metaType = QVariant_resolveMetaType(type); if (!metaType.isValid()) return {}; @@ -273,3 +243,104 @@ QString qObjectTr(PyTypeObject *type, const char *sourceText, const char *disamb } return result; } + +bool PyDate_ImportAndCheck(PyObject *pyIn) +{ + if (!PyDateTimeAPI) + PyDateTime_IMPORT; + return PyDate_Check(pyIn); +} + +bool PyDateTime_ImportAndCheck(PyObject *pyIn) +{ + if (!PyDateTimeAPI) + PyDateTime_IMPORT; + return PyDateTime_Check(pyIn); +} + +bool PyTime_ImportAndCheck(PyObject *pyIn) +{ + if (!PyDateTimeAPI) + PyDateTime_IMPORT; + return PyTime_Check(pyIn); +} + +PyObject *invokeMetaMethod(const InvokeMetaMethodFunc &f, + const QtCoreHelper::QGenericArgumentHolder &a0, + const QtCoreHelper::QGenericArgumentHolder &a1, + const QtCoreHelper::QGenericArgumentHolder &a2, + const QtCoreHelper::QGenericArgumentHolder &a3, + const QtCoreHelper::QGenericArgumentHolder &a4, + const QtCoreHelper::QGenericArgumentHolder &a5, + const QtCoreHelper::QGenericArgumentHolder &a6, + const QtCoreHelper::QGenericArgumentHolder &a7, + const QtCoreHelper::QGenericArgumentHolder &a8, + const QtCoreHelper::QGenericArgumentHolder &a9) +{ + PyThreadState *_save = PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS + const bool resultB = f(a0.toGenericArgument(), a1.toGenericArgument(), a2.toGenericArgument(), + a3.toGenericArgument(), a4.toGenericArgument(), a5.toGenericArgument(), + a6.toGenericArgument(), a7.toGenericArgument(), a8.toGenericArgument(), + a9.toGenericArgument()); + PyEval_RestoreThread(_save); // Py_END_ALLOW_THREADS + PyObject *result = resultB ? Py_True : Py_False; + Py_INCREF(result); + return result; +} + +// Convert a QGenericReturnArgument to Python for QMetaObject::invokeMethod +static PyObject *convertGenericReturnArgument(const void *retData, QMetaType metaType) +{ + PyObject *result = nullptr; + switch (metaType.id()) { + case QMetaType::Bool: + result = *reinterpret_cast<const bool *>(retData) ? Py_True : Py_False; + Py_INCREF(result); + break; + case QMetaType::Int: + result = PyLong_FromLong(*reinterpret_cast<const int *>(retData)); + break; + case QMetaType::Double: + result = PyFloat_FromDouble(*reinterpret_cast<const double *>(retData)); + break; + case QMetaType::QString: + result = PySide::qStringToPyUnicode(*reinterpret_cast<const QString *>(retData)); + break; + default: { + Shiboken::Conversions::SpecificConverter converter(metaType.name()); + const auto type = converter.conversionType(); + if (type == Shiboken::Conversions::SpecificConverter::InvalidConversion) { + PyErr_Format(PyExc_RuntimeError, "%s: Unable to find converter for \"%s\".", + __FUNCTION__, metaType.name()); + return nullptr; + } + result = converter.toPython(retData); + } + } + return result; +} + +PyObject *invokeMetaMethodWithReturn(const InvokeMetaMethodFuncWithReturn &f, + const QtCoreHelper::QGenericReturnArgumentHolder &r, + const QtCoreHelper::QGenericArgumentHolder &a0, + const QtCoreHelper::QGenericArgumentHolder &a1, + const QtCoreHelper::QGenericArgumentHolder &a2, + const QtCoreHelper::QGenericArgumentHolder &a3, + const QtCoreHelper::QGenericArgumentHolder &a4, + const QtCoreHelper::QGenericArgumentHolder &a5, + const QtCoreHelper::QGenericArgumentHolder &a6, + const QtCoreHelper::QGenericArgumentHolder &a7, + const QtCoreHelper::QGenericArgumentHolder &a8, + const QtCoreHelper::QGenericArgumentHolder &a9) +{ + PyThreadState *_save = PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS + const bool callResult = f(r.toGenericReturnArgument(), + a0.toGenericArgument(), a1.toGenericArgument(), a2.toGenericArgument(), + a3.toGenericArgument(), a4.toGenericArgument(), a5.toGenericArgument(), + a6.toGenericArgument(), a7.toGenericArgument(), a8.toGenericArgument(), + a9.toGenericArgument()); + PyEval_RestoreThread(_save); // Py_END_ALLOW_THREADS + if (!callResult) + return PyErr_Format(PyExc_RuntimeError, "QMetaMethod invocation failed."); + return convertGenericReturnArgument(r.data(), r.metaType()); +} |