@@ -273,84 +273,6 @@ def func(x):
273273 for i in [0 , 1 , 2 ]:
274274 self .assertFalse (_testcapi .pyobject_is_unique_temporary_new_object ())
275275
276- def test_object_clear_managed_dict (self ):
277- # PyObject_ClearManagedDict
278- clear_managed_dict = _testcapi .pyobject_clear_managed_dict
279- ManagedDictType = _testcapi .ManagedDictType
280-
281- obj = ManagedDictType ()
282- obj .value = 'first'
283- # asserting `__dict__` is a must to materialize the value before clean:
284- self .assertEqual (obj .__dict__ , {'value' : 'first' })
285- self .assertIs (type (obj .__dict__ ), dict )
286- clear_managed_dict (obj )
287- self .assertEqual (obj .__dict__ , {})
288- self .assertIs (type (obj .__dict__ ), dict )
289-
290- # big object:
291- obj = ManagedDictType ()
292- existing_dict = {f'attr_{ x } ' : x for x in range (100 )}
293- obj .__dict__ = existing_dict
294- self .assertIs (obj .__dict__ , existing_dict )
295- clear_managed_dict (obj )
296- self .assertEqual (obj .__dict__ , {})
297- self .assertIs (type (obj .__dict__ ), dict )
298-
299- # empty dict:
300- obj = ManagedDictType ()
301- self .assertEqual (obj .__dict__ , {})
302- self .assertIs (type (obj .__dict__ ), dict )
303- clear_managed_dict (obj )
304- self .assertEqual (obj .__dict__ , {})
305- self .assertIs (type (obj .__dict__ ), dict )
306-
307- # double clear:
308- obj = ManagedDictType ()
309- obj .value = 'first'
310- self .assertEqual (obj .__dict__ , {'value' : 'first' })
311- clear_managed_dict (obj )
312- clear_managed_dict (obj )
313- self .assertEqual (obj .__dict__ , {})
314- self .assertIs (type (obj .__dict__ ), dict )
315-
316- # unmaterialized:
317- obj = ManagedDictType ()
318- obj .value = 'first'
319- clear_managed_dict (obj )
320- self .assertEqual (obj .__dict__ , {})
321- self .assertIs (type (obj .__dict__ ), dict )
322-
323- # CRASHES: clear_managed_dict(NULL)
324-
325- @threading_helper .requires_working_threading ()
326- @support .nomemtest
327- def test_object_clear_managed_dict_ft_memory_error (self ):
328- # gh-152254: Test PyObject_ClearManagedDict() error.
329- # The setup must contain: object owned by another thread
330- # and the attempt to clear the managed dict with no memory left.
331- clear_managed_dict = _testcapi .pyobject_clear_managed_dict
332- ManagedDictType = _testcapi .ManagedDictType
333-
334- obj = ManagedDictType ()
335- obj .value = 'first'
336- self .assertEqual (len (obj .__dict__ ), 1 )
337-
338- def worker ():
339- # This needs to be in a separate thread for the crash to happen:
340- with support .catch_unraisable_exception () as ex :
341- # `PyObject_ClearManagedDict()` used to crash:
342- _testcapi .call_with_nomemory (0 , 1 , clear_managed_dict , obj )
343- self .assertIsNotNone (ex .unraisable )
344- self .assertIs (ex .unraisable .exc_type , MemoryError )
345- self .assertIn ('managed dict' , ex .unraisable .err_msg )
346- self .assertEqual (obj .__dict__ , {})
347- self .assertIs (type (obj .__dict__ ), dict )
348-
349- threading_helper .run_concurrently ([worker ])
350- self .assertEqual (obj .__dict__ , {})
351- self .assertIs (type (obj .__dict__ ), dict )
352-
353-
354276class RefTracerTest (unittest .TestCase ):
355277 @support .requires_resource ('cpu' )
356278 @support .skip_emscripten_stack_overflow ()
0 commit comments