10000 bpo-24766: doc= argument to subclasses of property not handled correctly by embray · Pull Request #2487 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-24766: doc= argument to subclasses of property not handled correctly #2487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-24766: doc= argument to subclasses of property not handled correc…
…tly (GH-2487)
  • Loading branch information
embray committed Aug 4, 2020
commit 349f3099e0208a3693b926ba254ba0275b9cd0fb
22 changes: 22 additions & 0 deletions Lib/test/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,28 @@ def spam(self):
Foo.spam.__doc__,
"spam wrapped in property subclass")

@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -O2 and above")
def test_prefer_explicit_doc(self):
# Issue 25757: subclasses of property lose docstring
self.assertEqual(property(doc="explicit doc").__doc__, "explicit doc")
self.assertEqual(PropertySub(doc="explicit doc").__doc__, "explicit doc")

class Foo:
spam = PropertySub(doc="spam explicit doc")

@spam.getter
def spam(self):
"""ignored as doc already set"""
return 1

def _stuff_getter(self):
"""ignored as doc set directly"""
stuff = PropertySub(doc="stuff doc argument", fget=_stuff_getter)

self.assertEqual(Foo.spam.__doc__, "spam explicit doc")
self.assertEqual(Foo.stuff.__doc__, "stuff doc argument")

@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -O2 and above")
def test_property_setter_copies_getter_docstring(self):
Expand Down
28 changes: 14 additions & 14 deletions Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1679,6 +1679,8 @@ property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset,
PyObject *fdel, PyObject *doc)
/*[clinic end generated code: output=01a960742b692b57 input=dfb5dbbffc6932d5]*/
{
_Py_IDENTIFIER(__doc__);

if (fget == Py_None)
fget = NULL;
if (fset == Py_None)
Expand All @@ -1699,28 +1701,26 @@ property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset,

/* if no docstring given and the getter has one, use that one */
if ((doc == NULL || doc == Py_None) && fget != NULL) {
_Py_IDENTIFIER(__doc__);
PyObject *get_doc;
int rc = _PyObject_LookupAttrId(fget, &PyId___doc__, &get_doc);
if (rc <= 0) {
return rc;
}
if (Py_IS_TYPE(self, &PyProperty_Type)) {
Py_XSETREF(self->prop_doc, get_doc);
}
else {
/* If this is a property subclass, put __doc__
in dict of the subclass instance instead,
otherwise it gets shadowed by __doc__ in the
class's dict. */
int err = _PyObject_SetAttrId((PyObject *)self, &PyId___doc__, get_doc);
Py_DECREF(get_doc);
if (err < 0)
return -1;
}
Py_XSETREF(self->prop_doc, get_doc);
self->getter_doc = 1;
}

/* If this is a property subclass, put __doc__ in dict of the subclass
instance as well, otherwise it gets shadowed by __doc__ in the
class's dict. */
if (Py_IS_TYPE(self, &PyProperty_Type)) {
int err = _PyObject_SetAttrId((PyObject *)self, &PyId___doc__,
self->prop_doc);
if (err < 0) {
return -1;
}
}

return 0;
}

Expand Down
0