From b87951b8882d05817b6c62b7721911ecb122577c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristi=C3=A1n=20Maureira-Fredes?= Date: Fri, 6 Mar 2020 17:03:03 +0100 Subject: Add doc getter for Property This allows to access the doc attribute from Properties when set: >>> p = Property(int, doc="some doc") >>> p.__doc__ some doc >>> p = Property(int) >>> p.__doc__ None Fixes: PYSIDE-135 Change-Id: Idf3e6c6632c775a50cfc8ecf03de3d2dc485f9f4 Reviewed-by: Friedemann Kleint Reviewed-by: Christian Tismer --- sources/pyside2/libpyside/pysideproperty.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/sources/pyside2/libpyside/pysideproperty.cpp b/sources/pyside2/libpyside/pysideproperty.cpp index 74a77e6c3..bdabf1202 100644 --- a/sources/pyside2/libpyside/pysideproperty.cpp +++ b/sources/pyside2/libpyside/pysideproperty.cpp @@ -61,6 +61,9 @@ static PyObject *qPropertyGetter(PyObject *, PyObject *); static int qpropertyTraverse(PyObject *self, visitproc visit, void *arg); static int qpropertyClear(PyObject *self); +// Attributes +static PyObject *qPropertyDocGet(PyObject *, void *); + static PyMethodDef PySidePropertyMethods[] = { {"setter", (PyCFunction)qPropertySetter, METH_O, 0}, {"write", (PyCFunction)qPropertySetter, METH_O, 0}, @@ -69,6 +72,11 @@ static PyMethodDef PySidePropertyMethods[] = { {0, 0, 0, 0} }; +static PyGetSetDef PySidePropertyType_getset[] = { + {"__doc__", qPropertyDocGet, nullptr, nullptr, nullptr}, + {nullptr, nullptr, nullptr, nullptr, nullptr} +}; + static PyType_Slot PySidePropertyType_slots[] = { {Py_tp_dealloc, (void *)qpropertyDeAlloc}, {Py_tp_call, (void *)qPropertyCall}, @@ -77,6 +85,7 @@ static PyType_Slot PySidePropertyType_slots[] = { {Py_tp_methods, (void *)PySidePropertyMethods}, {Py_tp_init, (void *)qpropertyTpInit}, {Py_tp_new, (void *)qpropertyTpNew}, + {Py_tp_getset, PySidePropertyType_getset}, {0, 0} }; // Dotted modulename is crucial for PyType_FromSpec to work. Is this name right? @@ -265,6 +274,24 @@ PyObject *qPropertyGetter(PyObject *self, PyObject *callback) return nullptr; } +static PyObject *qPropertyDocGet(PyObject *self, void *) +{ + auto data = reinterpret_cast(self); + PySidePropertyPrivate *pData = data->d; + + QByteArray doc(pData->doc); + if (!doc.isEmpty()) { +#if PY_MAJOR_VERSION >= 3 + return PyUnicode_FromString(doc); +#else + return PyString_FromString(doc); +#endif + } + Py_INCREF(Py_None); + return Py_None; +} + + static int qpropertyTraverse(PyObject *self, visitproc visit, void *arg) { PySidePropertyPrivate *data = reinterpret_cast(self)->d; -- cgit v1.2.3