https://github.com/python/cpython/commit/090dd21ab9379d6a2a6923d6cbab697355fb7165
commit: 090dd21ab9379d6a2a6923d6cbab697355fb7165
branch: main
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-02-17T23:18:30+02:00
summary:

gh-115618: Remove improper Py_XDECREFs in property methods (GH-115619)

files:
A Misc/NEWS.d/next/Library/2024-02-17-18-47-12.gh-issue-115618.napiNp.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 8ace9fd17ab96e..ad5ab5a87b5a66 100644
--- a/Lib/test/test_property.py
+++ b/Lib/test/test_property.py
@@ -183,6 +183,24 @@ def test_refleaks_in___init__(self):
             fake_prop.__init__('fget', 'fset', 'fdel', 'doc')
         self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10)
 
+    @support.refcount_test
+    def test_gh_115618(self):
+        # Py_XDECREF() was improperly called for None argument
+        # in property methods.
+        gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount')
+        prop = property()
+        refs_before = gettotalrefcount()
+        for i in range(100):
+            prop = prop.getter(None)
+        self.assertIsNone(prop.fget)
+        for i in range(100):
+            prop = prop.setter(None)
+        self.assertIsNone(prop.fset)
+        for i in range(100):
+            prop = prop.deleter(None)
+        self.assertIsNone(prop.fdel)
+        self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10)
+
     def test_property_set_name_incorrect_args(self):
         p = property()
 
diff --git 
a/Misc/NEWS.d/next/Library/2024-02-17-18-47-12.gh-issue-115618.napiNp.rst 
b/Misc/NEWS.d/next/Library/2024-02-17-18-47-12.gh-issue-115618.napiNp.rst
new file mode 100644
index 00000000000000..cb4b147d5dc663
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-02-17-18-47-12.gh-issue-115618.napiNp.rst
@@ -0,0 +1,3 @@
+Fix improper decreasing the reference count for ``None`` argument in
+:class:`property` methods :meth:`~property.getter`, :meth:`~property.setter`
+and :meth:`~property.deleter`.
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index 805de2971ba475..c4cd51bdae45ab 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -1730,15 +1730,12 @@ property_copy(PyObject *old, PyObject *get, PyObject 
*set, PyObject *del)
         return NULL;
 
     if (get == NULL || get == Py_None) {
-        Py_XDECREF(get);
         get = pold->prop_get ? pold->prop_get : Py_None;
     }
     if (set == NULL || set == Py_None) {
-        Py_XDECREF(set);
         set = pold->prop_set ? pold->prop_set : Py_None;
     }
     if (del == NULL || del == Py_None) {
-        Py_XDECREF(del);
         del = pold->prop_del ? pold->prop_del : Py_None;
     }
     if (pold->getter_doc && get != Py_None) {

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to