diff --git a/Lib/test/test_property.py b/Lib/test/test_property.py index d07b8632aa8722..05faaba842e22e 100644 --- a/Lib/test/test_property.py +++ b/Lib/test/test_property.py @@ -403,6 +403,19 @@ def spam(self): return 2 self.assertEqual(Foo.spam.__doc__, "a new docstring") + def test_gh100942(self): + # See https://github.com/python/cpython/issues/100942 + class pro(property): + def __new__(typ, *args, **kwargs): + return "abcdef" + + p = property.__new__(pro) + + # These lines were causing crashes: + p.getter(lambda self: 1) + p.setter(lambda self, val: 1) + p.deleter(lambda self: 1) + class _PropertyUnreachableAttribute: msg_format = None diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-01-11-21-41-58.gh-issue-100942._jnwot.rst b/Misc/NEWS.d/next/Core and Builtins/2023-01-11-21-41-58.gh-issue-100942._jnwot.rst new file mode 100644 index 00000000000000..8fe47d0dd97bab --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-01-11-21-41-58.gh-issue-100942._jnwot.rst @@ -0,0 +1 @@ +Fix crash on ``property`` subclasses with weird ``__new__`` methods. diff --git a/Objects/descrobject.c b/Objects/descrobject.c index c545b90c6283e1..41c113ae130179 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -1711,8 +1711,11 @@ property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del) Py_DECREF(type); if (new == NULL) return NULL; + if (PyObject_TypeCheck(new, &PyProperty_Type)) { + Py_XSETREF(((propertyobject *) new)->prop_name, + Py_XNewRef(pold->prop_name)); + } - Py_XSETREF(((propertyobject *) new)->prop_name, Py_XNewRef(pold->prop_name)); return new; }