diff options
Diffstat (limited to 'sources/shiboken6/libshiboken/voidptr.cpp')
-rw-r--r-- | sources/shiboken6/libshiboken/voidptr.cpp | 182 |
1 files changed, 91 insertions, 91 deletions
diff --git a/sources/shiboken6/libshiboken/voidptr.cpp b/sources/shiboken6/libshiboken/voidptr.cpp index 9eb0c01eb..8bb3f6ac8 100644 --- a/sources/shiboken6/libshiboken/voidptr.cpp +++ b/sources/shiboken6/libshiboken/voidptr.cpp @@ -1,43 +1,8 @@ -/**************************************************************************** -** -** Copyright (C) 2017 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) 2017 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 "voidptr.h" +#include "pep384ext.h" #include "sbkconverter.h" #include "basewrapper.h" #include "basewrapper_p.h" @@ -46,22 +11,21 @@ extern "C" { // Void pointer object definition. -typedef struct { +struct SbkVoidPtrObject { PyObject_HEAD void *cptr; Py_ssize_t size; bool isWritable; -} SbkVoidPtrObject; +}; -PyObject *SbkVoidPtrObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +PyObject *SbkVoidPtrObject_new(PyTypeObject *type, PyObject * /* args */, PyObject * /* kwds */) { // PYSIDE-560: It is much safer to first call a function and then do a // type cast than to do everything in one line. The bad construct looked // like this, actual call forgotten: // SbkVoidPtrObject *self = // reinterpret_cast<SbkVoidPtrObject *>(type->tp_alloc); - PyObject *ob = type->tp_alloc(type, 0); - auto *self = reinterpret_cast<SbkVoidPtrObject *>(ob); + auto *self = PepExt_TypeCallAlloc<SbkVoidPtrObject>(type, 0); if (self != nullptr) { self->cptr = nullptr; @@ -72,7 +36,7 @@ PyObject *SbkVoidPtrObject_new(PyTypeObject *type, PyObject *args, PyObject *kwd return reinterpret_cast<PyObject *>(self); } -#define SbkVoidPtr_Check(op) (Py_TYPE(op) == SbkVoidPtrTypeF()) +#define SbkVoidPtr_Check(op) (Py_TYPE(op) == SbkVoidPtr_TypeF()) int SbkVoidPtrObject_init(PyObject *self, PyObject *args, PyObject *kwds) @@ -163,12 +127,19 @@ PyObject *SbkVoidPtrObject_richcmp(PyObject *obj1, PyObject *obj2, int op) if (validObjects) { switch (op) { - case Py_EQ: if (cptr1 == cptr2) result = Py_True; break; - case Py_NE: if (cptr1 != cptr2) result = Py_True; break; - case Py_LT: break; - case Py_LE: break; - case Py_GT: break; - case Py_GE: break; + case Py_EQ: + if (cptr1 == cptr2) + result = Py_True; + break; + case Py_NE: + if (cptr1 != cptr2) + result = Py_True; + break; + case Py_LT: + case Py_LE: + case Py_GT: + case Py_GE: + break; } } @@ -182,13 +153,12 @@ PyObject *SbkVoidPtrObject_int(PyObject *v) return PyLong_FromVoidPtr(sbkObject->cptr); } -PyObject *toBytes(PyObject *self, PyObject *args) +PyObject *toBytes(PyObject *self, PyObject * /* args */) { auto *sbkObject = reinterpret_cast<SbkVoidPtrObject *>(self); - if (sbkObject->size < 0) { - PyErr_SetString(PyExc_IndexError, "VoidPtr does not have a size set."); - return nullptr; - } + if (sbkObject->size < 0) + return PyErr_Format(PyExc_IndexError, "VoidPtr does not have a size set."); + PyObject *bytes = PyBytes_FromStringAndSize(reinterpret_cast<const char *>(sbkObject->cptr), sbkObject->size); Py_XINCREF(bytes); @@ -196,8 +166,8 @@ PyObject *toBytes(PyObject *self, PyObject *args) } static struct PyMethodDef SbkVoidPtrObject_methods[] = { - {"toBytes", toBytes, METH_NOARGS}, - {nullptr} + {"toBytes", toBytes, METH_NOARGS, nullptr}, + {nullptr, nullptr, 0, nullptr} }; static Py_ssize_t SbkVoidPtrObject_length(PyObject *v) @@ -285,47 +255,49 @@ static PyBufferProcs SbkVoidPtrObjectBufferProc = { (releasebufferproc)nullptr // bf_releasebuffer }; -// Void pointer type definition. -static PyType_Slot SbkVoidPtrType_slots[] = { - {Py_tp_repr, (void *)SbkVoidPtrObject_repr}, - {Py_nb_int, (void *)SbkVoidPtrObject_int}, - {Py_sq_length, (void *)SbkVoidPtrObject_length}, - {Py_tp_str, (void *)SbkVoidPtrObject_str}, - {Py_tp_richcompare, (void *)SbkVoidPtrObject_richcmp}, - {Py_tp_init, (void *)SbkVoidPtrObject_init}, - {Py_tp_new, (void *)SbkVoidPtrObject_new}, - {Py_tp_dealloc, (void *)Sbk_object_dealloc}, - {Py_tp_methods, (void *)SbkVoidPtrObject_methods}, - {0, nullptr} -}; -static PyType_Spec SbkVoidPtrType_spec = { - "2:shiboken6.shiboken6.VoidPtr", - sizeof(SbkVoidPtrObject), - 0, - Py_TPFLAGS_DEFAULT, - SbkVoidPtrType_slots, -}; - - +static PyTypeObject *createVoidPtrType() +{ + PyType_Slot SbkVoidPtrType_slots[] = { + {Py_tp_repr, reinterpret_cast<void *>(SbkVoidPtrObject_repr)}, + {Py_nb_int, reinterpret_cast<void *>(SbkVoidPtrObject_int)}, + {Py_sq_length, reinterpret_cast<void *>(SbkVoidPtrObject_length)}, + {Py_tp_str, reinterpret_cast<void *>(SbkVoidPtrObject_str)}, + {Py_tp_richcompare, reinterpret_cast<void *>(SbkVoidPtrObject_richcmp)}, + {Py_tp_init, reinterpret_cast<void *>(SbkVoidPtrObject_init)}, + {Py_tp_new, reinterpret_cast<void *>(SbkVoidPtrObject_new)}, + {Py_tp_dealloc, reinterpret_cast<void *>(Sbk_object_dealloc)}, + {Py_tp_methods, reinterpret_cast<void *>(SbkVoidPtrObject_methods)}, + {0, nullptr} + }; + + PyType_Spec SbkVoidPtrType_spec = { + "2:shiboken6.Shiboken.VoidPtr", + sizeof(SbkVoidPtrObject), + 0, + Py_TPFLAGS_DEFAULT, + SbkVoidPtrType_slots, + }; + + return SbkType_FromSpec_BMDWB(&SbkVoidPtrType_spec, + nullptr, nullptr, 0, 0, + &SbkVoidPtrObjectBufferProc); } -PyTypeObject *SbkVoidPtrTypeF(void) +PyTypeObject *SbkVoidPtr_TypeF(void) { - static PyTypeObject *type = nullptr; - if (!type) { - type = reinterpret_cast<PyTypeObject *>(SbkType_FromSpec(&SbkVoidPtrType_spec)); - PepType_AS_BUFFER(type) = &SbkVoidPtrObjectBufferProc; - } + static auto *type = createVoidPtrType(); return type; } +} // extern "C" + namespace VoidPtr { static int voidPointerInitialized = false; void init() { - if (PyType_Ready(reinterpret_cast<PyTypeObject *>(SbkVoidPtrTypeF())) < 0) + if (PyType_Ready(SbkVoidPtr_TypeF()) < 0) Py_FatalError("[libshiboken] Failed to initialize Shiboken.VoidPtr type."); else voidPointerInitialized = true; @@ -334,9 +306,9 @@ void init() void addVoidPtrToModule(PyObject *module) { if (voidPointerInitialized) { - Py_INCREF(SbkVoidPtrTypeF()); - PyModule_AddObject(module, PepType_GetNameStr(SbkVoidPtrTypeF()), - reinterpret_cast<PyObject *>(SbkVoidPtrTypeF())); + Py_INCREF(SbkVoidPtr_TypeF()); + PyModule_AddObject(module, PepType_GetNameStr(SbkVoidPtr_TypeF()), + reinterpret_cast<PyObject *>(SbkVoidPtr_TypeF())); } } @@ -345,7 +317,7 @@ static PyObject *createVoidPtr(void *cppIn, Py_ssize_t size = 0, bool isWritable if (!cppIn) Py_RETURN_NONE; - SbkVoidPtrObject *result = PyObject_New(SbkVoidPtrObject, SbkVoidPtrTypeF()); + SbkVoidPtrObject *result = PyObject_New(SbkVoidPtrObject, SbkVoidPtr_TypeF()); if (!result) Py_RETURN_NONE; @@ -418,7 +390,7 @@ static PythonToCppFunc PythonBufferToCppIsConvertible(PyObject *pyIn) SbkConverter *createConverter() { - SbkConverter *converter = Shiboken::Conversions::createConverter(SbkVoidPtrTypeF(), toPython); + SbkConverter *converter = Shiboken::Conversions::createConverter(SbkVoidPtr_TypeF(), toPython); Shiboken::Conversions::addPythonToCppValueConversion(converter, VoidPtrToCpp, VoidPtrToCppIsConvertible); @@ -431,4 +403,32 @@ SbkConverter *createConverter() return converter; } +void setSize(PyObject *voidPtr, Py_ssize_t size) +{ + assert(voidPtr->ob_type == SbkVoidPtr_TypeF()); + auto *voidPtrObj = reinterpret_cast<SbkVoidPtrObject *>(voidPtr); + voidPtrObj->size = size; +} + +Py_ssize_t getSize(PyObject *voidPtr) +{ + assert(voidPtr->ob_type == SbkVoidPtr_TypeF()); + auto *voidPtrObj = reinterpret_cast<SbkVoidPtrObject *>(voidPtr); + return voidPtrObj->size; +} + +bool isWritable(PyObject *voidPtr) +{ + assert(voidPtr->ob_type == SbkVoidPtr_TypeF()); + auto *voidPtrObj = reinterpret_cast<SbkVoidPtrObject *>(voidPtr); + return voidPtrObj->isWritable; +} + +void setWritable(PyObject *voidPtr, bool isWritable) +{ + assert(voidPtr->ob_type == SbkVoidPtr_TypeF()); + auto *voidPtrObj = reinterpret_cast<SbkVoidPtrObject *>(voidPtr); + voidPtrObj->isWritable = isWritable; +} + } // namespace VoidPtr |