8000 gh-98963: Add a note to the error for property subclasses without __doc__ by encukou · Pull Request #99058 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-98963: Add a note to the error for property subclasses without __doc__ #99058

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

Closed
wants to merge 3 commits into from
Closed
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
gh-98963: Add a note to the error for property subclasses without __d…
…oc__

Subclasses created using the C API don't get __dict__, which means their
__doc__ can't be set for each property separately. The __doc__
descriptor from `property` itself is shadowed by the subclass's docstring.

This edge case isn't really worth mentioning in the docs. This adds a note
that should guide anyone encountering the issue to the right fix.
  • Loading branch information
encukou committed Nov 3, 2022
commit f530eaf58f34c03fc4a8dd79dd04e1257f99620e
11 changes: 11 additions & 0 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2359,6 +2359,17 @@ def test_testcapi_no_segfault(self):
class X(object):
p = property(_testcapi.test_with_docstring)

def test_subclass_with_nonwritable_doc(self):
# gh-98963: subclasses must have writable __doc__. A note is added to
# the exception in this surprising edge case.
class BadProperty(property):
__doc__ = property(lambda: None)
with self.assertRaises(AttributeError) as cm:
p = BadProperty(lambda: 123)
notes = cm.exception.__notes__
wanted = "subclasses of 'property' need to provide a writable __doc__"
self.assertTrue(any(note.startswith(wanted) for note in notes), notes)

def test_properties_plus(self):
class C(object):
foo = property(doc="hello")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add a note to the ``AttributeError`` raised when instantiating a subclass of
:py:type:`property` that does not have a writable ``__doc__`` attribute.
(Creating such a subclass is possible via the C API.)
22 changes: 21 additions & 1 deletion Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1826,8 +1826,28 @@ property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset,
int err = PyObject_SetAttr(
(PyObject *)self, &_Py_ID(__doc__), prop_doc);
Py_XDECREF(prop_doc);
if (err < 0)
if (err < 0) {
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
// gh-98963: the subclass doesn't have a __dict__
// (probably it was subclassed using C API).
// Add a note to prevent surprises.
PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
PyErr_NormalizeException(&type, &value, &traceback);
if (value) {
PyObject_CallMethod(
value, "add_note", "s",
"subclasses of 'property' need to provide a writable "
"__doc__ attribute (e.g. a __doc__ member or a "
"__dict__) to avoid per-property docstrings being "
"shadowed by the subclass docstring");
// ignore errors while setting the note
PyErr_Clear();
}
PyErr_Restore(type, value, traceback);
}
return -1;
}
}

return 0;
Expand Down
0