8000 GH-100942: Fix incorrect cast in property_copy(). (#100965) · python/cpython@94fc770 · GitHub
[go: up one dir, main page]

Skip to content

Commit 94fc770

Browse files
authored
GH-100942: Fix incorrect cast in property_copy(). (#100965)
1 parent b511d35 commit 94fc770

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/test/test_property.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,23 @@ def test_property_set_name_incorrect_args(self):
214214
):
215215
p.__set_name__(*([0] * i))
216216

217+
def test_property_setname_on_property_subclass(self):
218+
# https://github.com/python/cpython/issues/100942
219+
# Copy was setting the name field without first
220+
# verifying that the copy was an actual property
221+
# instance. As a result, the code below was
222+
# causing a segfault.
223+
224+
class pro(property):
225+
def __new__(typ, *args, **kwargs):
226+
return "abcdef"
227+
228+
class A:
229+
pass
230+
231+
p = property.__new__(pro)
232+
p.__set_name__(A, 1)
233+
np = p.getter(lambda self: 1)
217234

218235
# Issue 5890: subclasses of property do not preserve method __doc__ strings
219236
class PropertySub(property):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed segfault in property.getter/setter/deleter that occurred when a property
2+
subclass overrode the ``__new__`` method to return a non-property instance.

Objects/descrobject.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,9 @@ property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
17121712
if (new == NULL)
17131713
return NULL;
17141714

1715-
Py_XSETREF(((propertyobject *) new)->prop_name, Py_XNewRef(pold->prop_name));
1715+
if (PyObject_TypeCheck((new), &PyProperty_Type)) {
1716+
Py_XSETREF(((propertyobject *) new)->prop_name, Py_XNewRef(pold->prop_name));
1717+
}
17161718
return new;
17171719
}
17181720

0 commit comments

Comments
 (0)
0