Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
[3.11] GH-100942: Fix incorrect cast in property_copy(). (GH-100965).
(cherry picked from commit 94fc770)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
  • Loading branch information
rhettinger authored and sobolevn committed Jan 13, 2023
commit e42fbf0320e5440c32762785ca442f94e98a964f
17 changes: 17 additions & 0 deletions Lib/test/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,23 @@ def test_property_set_name_incorrect_args(self):
):
p.__set_name__(*([0] * i))

def test_property_setname_on_property_subclass(self):
# https://github.com/python/cpython/issues/100942
# Copy was setting the name field without first
# verifying that the copy was an actual property
# instance. As a result, the code below was
# causing a segfault.

class pro(property):
def __new__(typ, *args, **kwargs):
return "abcdef"

class A:
pass

p = property.__new__(pro)
p.__set_name__(A, 1)
np = p.getter(lambda self: 1)

# Issue 5890: subclasses of property do not preserve method __doc__ strings
class PropertySub(property):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed segfault in property.getter/setter/deleter that occurred when a property
subclass overrode the ``__new__`` method to return a non-property instance.
7 changes: 4 additions & 3 deletions Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1723,9 +1723,10 @@ property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
Py_DECREF(type);
if (new == NULL)
return NULL;

Py_XINCREF(pold->prop_name);
Py_XSETREF(((propertyobject *) new)->prop_name, pold->prop_name);
if (PyObject_TypeCheck((new), &PyProperty_Type)) {
Py_XINCREF(pold->prop_name);
Py_XSETREF(((propertyobject *) new)->prop_name, pold->prop_name);
}
return new;
}

Expand Down