Skip to content

Commit 855b1a9

Browse files
sobolevnrhettinger
andauthored
[3.11] GH-100942: Fix incorrect cast in property_copy(). (GH-100965). (#101008)
(cherry picked from commit 94fc770) Co-authored-by: Raymond Hettinger <[email protected]>
1 parent 6d98282 commit 855b1a9

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,9 +1723,10 @@ property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
17231723
Py_DECREF(type);
17241724
if (new == NULL)
17251725
return NULL;
1726-
1727-
Py_XINCREF(pold->prop_name);
1728-
Py_XSETREF(((propertyobject *) new)->prop_name, pold->prop_name);
1726+
if (PyObject_TypeCheck((new), &PyProperty_Type)) {
1727+
Py_XINCREF(pold->prop_name);
1728+
Py_XSETREF(((propertyobject *) new)->prop_name, pold->prop_name);
1729+
}
17291730
return new;
17301731
}
17311732

0 commit comments

Comments
 (0)