Skip to content

Commit 9bc9512

Browse files
committed
gh-100942: Fix crash on property subtypes with weird __new__
1 parent 762745a commit 9bc9512

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

Lib/test/test_property.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,16 @@ def spam(self):
403403
return 2
404404
self.assertEqual(Foo.spam.__doc__, "a new docstring")
405405

406+
def test_gh100942(self):
407+
# See https://github.com/python/cpython/issues/100942
408+
class pro(property):
409+
def __new__(typ, *args, **kwargs):
410+
return "abcdef"
411+
412+
p = property.__new__(pro)
413+
with self.assertRaises(TypeError):
414+
p.getter(lambda self: 1) # this line was causing a crash
415+
406416

407417
class _PropertyUnreachableAttribute:
408418
msg_format = None
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix crash on ``property`` subclasses with weird ``__new__`` methods.

Objects/descrobject.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,11 @@ property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
17111711
Py_DECREF(type);
17121712
if (new == NULL)
17131713
return NULL;
1714+
if (!PyObject_TypeCheck(new, &PyProperty_Type)) {
1715+
PyErr_Format(PyExc_TypeError, "property instance expected, got %.50s",
1716+
Py_TYPE(new)->tp_name);
1717+
return NULL;
1718+
}
17141719

17151720
Py_XSETREF(((propertyobject *) new)->prop_name, Py_XNewRef(pold->prop_name));
17161721
return new;

0 commit comments

Comments
 (0)