Skip to content

Commit 5dac9c5

Browse files
[3.11] gh-115618: Remove improper Py_XDECREFs in property methods (GH-115619) (GH-115621)
(cherry picked from commit 090dd21)
1 parent cd0a071 commit 5dac9c5

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

Lib/test/test_property.py

+18
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,24 @@ def test_refleaks_in___init__(self):
183183
fake_prop.__init__('fget', 'fset', 'fdel', 'doc')
184184
self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10)
185185

186+
@support.refcount_test
187+
def test_gh_115618(self):
188+
# Py_XDECREF() was improperly called for None argument
189+
# in property methods.
190+
gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount')
191+
prop = property()
192+
refs_before = gettotalrefcount()
193+
for i in range(100):
194+
prop = prop.getter(None)
195+
self.assertIsNone(prop.fget)
196+
for i in range(100):
197+
prop = prop.setter(None)
198+
self.assertIsNone(prop.fset)
199+
for i in range(100):
200+
prop = prop.deleter(None)
201+
self.assertIsNone(prop.fdel)
202+
self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10)
203+
186204
@unittest.skipIf(sys.flags.optimize >= 2,
187205
"Docstrings are omitted with -O2 and above")
188206
def test_class_property(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix improper decreasing the reference count for ``None`` argument in
2+
:class:`property` methods :meth:`~property.getter`, :meth:`~property.setter`
3+
and :meth:`~property.deleter`.

Objects/descrobject.c

-3
Original file line numberDiff line numberDiff line change
@@ -1700,15 +1700,12 @@ property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
17001700
return NULL;
17011701

17021702
if (get == NULL || get == Py_None) {
1703-
Py_XDECREF(get);
17041703
get = pold->prop_get ? pold->prop_get : Py_None;
17051704
}
17061705
if (set == NULL || set == Py_None) {
1707-
Py_XDECREF(set);
17081706
set = pold->prop_set ? pold->prop_set : Py_None;
17091707
}
17101708
if (del == NULL || del == Py_None) {
1711-
Py_XDECREF(del);
17121709
del = pold->prop_del ? pold->prop_del : Py_None;
17131710
}
17141711
if (pold->getter_doc && get != Py_None) {

0 commit comments

Comments
 (0)