]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] GH-100942: Fix incorrect cast in property_copy(). (GH-100965). (#101008)
authorNikita Sobolev <mail@sobolevn.me>
Sun, 15 Jan 2023 07:08:25 +0000 (10:08 +0300)
committerGitHub <noreply@github.com>
Sun, 15 Jan 2023 07:08:25 +0000 (12:38 +0530)
(cherry picked from commit 94fc7706b7bc3d57cdd6d15bf8e8c4499ae53a69)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
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 d91ad1c191275e1ee3d0ad38839345b63727e02b..953431504a72479f62d3d332ba5bac6cf58350d8 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 6a5c2a4cf9998132aadfc2129c3fef8a23887d59..4d8b83758b52d3cc5ab5137e32556ff219890d33 100644 (file)
@@ -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;
 }