]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-100942: Fix incorrect cast in property_copy(). (#100965)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Thu, 12 Jan 2023 22:13:56 +0000 (16:13 -0600)
committerGitHub <noreply@github.com>
Thu, 12 Jan 2023 22:13:56 +0000 (16:13 -0600)
Lib/test/test_property.py
Misc/NEWS.d/next/Core and Builtins/2023-01-11-22-52-19.gh-issue-100942.ontOy_.rst [new file with mode: 0644]
Objects/descrobject.c

index d07b8632aa8722ef4e8ed1e36d6cad3bb8c495a4..d4bdf50c0192ae658badc4b6c5f746f172ca4376 100644 (file)
@@ -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 (file)
index 0000000..daccea2
--- /dev/null
@@ -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.
index c545b90c6283e11dbdbbe92a1d3abdead29a4d89..334be75e8df9dfb3fde737dce51a888211486d65 100644 (file)
@@ -1712,7 +1712,9 @@ property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
     if (new == NULL)
         return NULL;
 
-    Py_XSETREF(((propertyobject *) new)->prop_name, Py_XNewRef(pold->prop_name));
+    if (PyObject_TypeCheck((new), &PyProperty_Type)) {
+        Py_XSETREF(((propertyobject *) new)->prop_name, Py_XNewRef(pold->prop_name));
+    }
     return new;
 }