diff options
author | Guido van Rossum <guido@python.org> | 2007-11-10 22:12:24 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-11-10 22:12:24 (GMT) |
commit | d1ef78942adedbb0d3483b43911038a44a77ad4d (patch) | |
tree | 87a53f3589ff9430c8c09b538ce70dd1745f3e85 /Objects/descrobject.c | |
parent | d3ffd341b8370f0bd134ecc6c1b634a12f35446d (diff) | |
download | cpython-d1ef78942adedbb0d3483b43911038a44a77ad4d.zip cpython-d1ef78942adedbb0d3483b43911038a44a77ad4d.tar.gz cpython-d1ef78942adedbb0d3483b43911038a44a77ad4d.tar.bz2 |
Issue 1416. Add getter, setter, deleter methods to properties that can be
used as decorators to create fully-populated properties.
Diffstat (limited to 'Objects/descrobject.c')
-rw-r--r-- | Objects/descrobject.c | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/Objects/descrobject.c b/Objects/descrobject.c index ec9faa5..535a49f 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -1108,6 +1108,60 @@ static PyMemberDef property_members[] = { {0} }; +PyDoc_STRVAR(getter_doc, + "Descriptor to change the getter on a property."); + +PyObject * +property_getter(PyObject *self, PyObject *getter) +{ + Py_XDECREF(((propertyobject *)self)->prop_get); + if (getter == Py_None) + getter = NULL; + Py_XINCREF(getter); + ((propertyobject *)self)->prop_get = getter; + Py_INCREF(self); + return self; +} + +PyDoc_STRVAR(setter_doc, + "Descriptor to change the setter on a property.\n"); + +PyObject * +property_setter(PyObject *self, PyObject *setter) +{ + Py_XDECREF(((propertyobject *)self)->prop_set); + if (setter == Py_None) + setter = NULL; + Py_XINCREF(setter); + ((propertyobject *)self)->prop_set = setter; + Py_INCREF(self); + return self; +} + +PyDoc_STRVAR(deleter_doc, + "Descriptor to change the deleter on a property."); + +PyObject * +property_deleter(PyObject *self, PyObject *deleter) +{ + Py_XDECREF(((propertyobject *)self)->prop_del); + if (deleter == Py_None) + deleter = NULL; + Py_XINCREF(deleter); + ((propertyobject *)self)->prop_del = deleter; + Py_INCREF(self); + return self; +} + + + +static PyMethodDef property_methods[] = { + {"getter", property_getter, METH_O, getter_doc}, + {"setter", property_setter, METH_O, setter_doc}, + {"deleter", property_deleter, METH_O, deleter_doc}, + {0} +}; + static void property_dealloc(PyObject *self) @@ -1260,7 +1314,7 @@ PyTypeObject PyProperty_Type = { 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - 0, /* tp_methods */ + property_methods, /* tp_methods */ property_members, /* tp_members */ 0, /* tp_getset */ 0, /* tp_base */ |