diff options
| -rw-r--r-- | Lib/test/test_property.py | 17 | ||||
| -rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2023-01-11-22-52-19.gh-issue-100942.ontOy_.rst | 2 | ||||
| -rw-r--r-- | Objects/descrobject.c | 7 |
3 files changed, 23 insertions, 3 deletions
diff --git a/Lib/test/test_property.py b/Lib/test/test_property.py index 7f3813fc8c..4cd4283d84 100644 --- a/Lib/test/test_property.py +++ b/Lib/test/test_property.py @@ -214,6 +214,23 @@ class PropertyTests(unittest.TestCase): ): 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): diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-01-11-22-52-19.gh-issue-100942.ontOy_.rst b/Misc/NEWS.d/next/Core and Builtins/2023-01-11-22-52-19.gh-issue-100942.ontOy_.rst new file mode 100644 index 0000000000..daccea255b --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-01-11-22-52-19.gh-issue-100942.ontOy_.rst @@ -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. diff --git a/Objects/descrobject.c b/Objects/descrobject.c index ee3ad1b7a3..1b1487a025 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -1675,9 +1675,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; } |
