diff options
author | Cristián Maureira-Fredes <cristian.maureira-fredes@qt.io> | 2020-03-06 17:03:03 +0100 |
---|---|---|
committer | Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io> | 2020-03-25 14:39:22 +0100 |
commit | b87951b8882d05817b6c62b7721911ecb122577c (patch) | |
tree | a7997ca2736a7bf04fa84b8ff7554f1fda29704d | |
parent | 502dfcf352b0094e2b1e8b495af1cdeb3e7874ed (diff) |
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 <Friedemann.Kleint@qt.io>
Reviewed-by: Christian Tismer <tismer@stackless.com>
-rw-r--r-- | sources/pyside2/libpyside/pysideproperty.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
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<PySideProperty *>(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<PySideProperty *>(self)->d; |