Skip to content

Commit 4871a93

Browse files
rhettingersobolevn
authored andcommitted
[3.10] pythonGH-100942: Fix incorrect cast in property_copy(). (pythonGH-100965).
(cherry picked from commit 94fc770) Co-authored-by: Raymond Hettinger <[email protected]>
1 parent 87f9b1d commit 4871a93

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
@@ -1675,9 +1675,10 @@ property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
16751675
Py_DECREF(type);
16761676
if (new == NULL)
16771677
return NULL;
1678-
1679-
Py_XINCREF(pold->prop_name);
1680-
Py_XSETREF(((propertyobject *) new)->prop_name, pold->prop_name);
1678+
if (PyObject_TypeCheck((new), &PyProperty_Type)) {
1679+
Py_XINCREF(pold->prop_name);
1680+
Py_XSETREF(((propertyobject *) new)->prop_name, pold->prop_name);
1681+
}
16811682
return new;
16821683
}
16831684

0 commit comments

Comments
 (0)