[Python-checkins] [3.11] GH-100942: Fix incorrect cast in property_copy(). (GH-100965). (#101008)
kumaraditya303
webhook-mailer at python.org
Sun Jan 15 02:08:31 EST 2023
https://github.com/python/cpython/commit/855b1a935eebc134a4ccf5f2c9d8f7dda21deb70
commit: 855b1a935eebc134a4ccf5f2c9d8f7dda21deb70
branch: 3.11
author: Nikita Sobolev <mail at sobolevn.me>
committer: kumaraditya303 <59607654+kumaraditya303 at users.noreply.github.com>
date: 2023-01-15T12:38:25+05:30
summary:
[3.11] GH-100942: Fix incorrect cast in property_copy(). (GH-100965). (#101008)
(cherry picked from commit 94fc7706b7bc3d57cdd6d15bf8e8c4499ae53a69)
Co-authored-by: Raymond Hettinger <rhettinger at users.noreply.github.com>
files:
A Misc/NEWS.d/next/Core and Builtins/2023-01-11-22-52-19.gh-issue-100942.ontOy_.rst
M Lib/test/test_property.py
M Objects/descrobject.c
diff --git a/Lib/test/test_property.py b/Lib/test/test_property.py
index d91ad1c19127..953431504a72 100644
--- a/Lib/test/test_property.py
+++ b/Lib/test/test_property.py
@@ -214,6 +214,23 @@ def test_property_set_name_incorrect_args(self):
):
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 000000000000..daccea255b16
--- /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 6a5c2a4cf999..4d8b83758b52 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -1723,9 +1723,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;
}
More information about the Python-checkins
mailing list