File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -178,11 +178,17 @@ class EnableDeferredRefcountingTest(unittest.TestCase):
178178 @support .requires_resource ("cpu" )
179179 def test_enable_deferred_refcount (self ):
180180 from threading import Thread
181+ import gc
181182
182183 self .assertEqual (_testcapi .pyobject_enable_deferred_refcount ("not tracked" ), 0 )
183184 foo = []
184185 self .assertEqual (_testcapi .pyobject_enable_deferred_refcount (foo ), int (support .Py_GIL_DISABLED ))
185186
187+ # The object must be tracked by the GC
188+ not_gc_tracked = tuple ([1 , 2 ])
189+ self .assertFalse (gc .is_tracked (not_gc_tracked ))
190+ self .assertEqual (_testcapi .pyobject_enable_deferred_refcount (not_gc_tracked ), 0 )
191+
186192 # Make sure reference counting works on foo now
187193 self .assertEqual (foo , [])
188194 if support .Py_GIL_DISABLED :
Original file line number Diff line number Diff line change 1+ :c:func: `PyUnstable_Object_EnableDeferredRefcount ` now returns ``0 `` if the
2+ object is not tracked by the garbage collector: if :func: `gc.is_tracked ` is
3+ false. Patch by Victor Stinner.
Original file line number Diff line number Diff line change @@ -2821,6 +2821,13 @@ PyUnstable_Object_EnableDeferredRefcount(PyObject *op)
28212821 return 0 ;
28222822 }
28232823
2824+ if (!PyObject_GC_IsTracked (op )) {
2825+ // When deferred refcount is enabled, the object will only be
2826+ // deallocated by the tracing garbage collector. So it must be tracked
2827+ // by the garbage collector.
2828+ return 0 ;
2829+ }
2830+
28242831 uint8_t bits = _Py_atomic_load_uint8 (& op -> ob_gc_bits );
28252832 if ((bits & _PyGC_BITS_DEFERRED ) != 0 )
28262833 {
You can’t perform that action at this time.
0 commit comments