diff options
author | Georg Brandl <georg@python.org> | 2006-03-08 18:09:27 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2006-03-08 18:09:27 (GMT) |
commit | 533ff6fc0622bfae5d13d3a3838fbec3d9d21092 (patch) | |
tree | 9650a936ff5061230485f1a3f4e4a6d90470250a /Objects | |
parent | f3c4ad14104ad45bd53df7b7e63cfc889efc3c05 (diff) | |
download | cpython-533ff6fc0622bfae5d13d3a3838fbec3d9d21092.zip cpython-533ff6fc0622bfae5d13d3a3838fbec3d9d21092.tar.gz cpython-533ff6fc0622bfae5d13d3a3838fbec3d9d21092.tar.bz2 |
Patch #1434038: property() now uses the getter's docstring if there is
no "doc" argument given. This makes it possible to legitimately use
property() as a decorator to produce a read-only property.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/descrobject.c | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/Objects/descrobject.c b/Objects/descrobject.c index b96ad6e..9494062 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -1081,6 +1081,8 @@ PyWrapper_New(PyObject *d, PyObject *self) class property(object): def __init__(self, fget=None, fset=None, fdel=None, doc=None): + if doc is None and fget is not None and hasattr(fget, "__doc__"): + doc = fget.__doc__ self.__get = fget self.__set = fset self.__del = fdel @@ -1182,6 +1184,7 @@ static int property_init(PyObject *self, PyObject *args, PyObject *kwds) { PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL; + PyObject *get_doc = NULL; static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0}; propertyobject *gs = (propertyobject *)self; @@ -1196,6 +1199,15 @@ property_init(PyObject *self, PyObject *args, PyObject *kwds) if (del == Py_None) del = NULL; + /* if no docstring given and the getter has one, use that one */ + if ((doc == NULL || doc == Py_None) && get != NULL && + PyObject_HasAttrString(get, "__doc__")) { + if (!(get_doc = PyObject_GetAttrString(get, "__doc__"))) + return -1; + Py_DECREF(get_doc); /* it is INCREF'd again below */ + doc = get_doc; + } + Py_XINCREF(get); Py_XINCREF(set); Py_XINCREF(del); |