@@ -354,6 +354,83 @@ def test_pyobject_dump(self):
354354 output = self .pyobject_dump (NULL )
355355 self .assertRegex (output , r'<object at .* is freed>' )
356356
357+ def test_object_clear_managed_dict (self ):
358+ # PyObject_ClearManagedDict
359+ clear_managed_dict = _testcapi .pyobject_clear_managed_dict
360+ ManagedDictType = _testcapi .ManagedDictType
361+
362+ obj = ManagedDictType ()
363+ obj .value = 'first'
364+ # asserting `__dict__` is a must to materialize the value before clean:
365+ self .assertEqual (obj .__dict__ , {'value' : 'first' })
366+ self .assertIs (type (obj .__dict__ ), dict )
367+ clear_managed_dict (obj )
368+ self .assertEqual (obj .__dict__ , {})
369+ self .assertIs (type (obj .__dict__ ), dict )
370+
371+ # big object:
372+ obj = ManagedDictType ()
373+ existing_dict = {f'attr_{ x } ' : x for x in range (100 )}
374+ obj .__dict__ = existing_dict
375+ self .assertIs (obj .__dict__ , existing_dict )
376+ clear_managed_dict (obj )
377+ self .assertEqual (obj .__dict__ , {})
378+ self .assertIs (type (obj .__dict__ ), dict )
379+
380+ # empty dict:
381+ obj = ManagedDictType ()
382+ self .assertEqual (obj .__dict__ , {})
383+ self .assertIs (type (obj .__dict__ ), dict )
384+ clear_managed_dict (obj )
385+ self .assertEqual (obj .__dict__ , {})
386+ self .assertIs (type (obj .__dict__ ), dict )
387+
388+ # double clear:
389+ obj = ManagedDictType ()
390+ obj .value = 'first'
391+ self .assertEqual (obj .__dict__ , {'value' : 'first' })
392+ clear_managed_dict (obj )
393+ clear_managed_dict (obj )
394+ self .assertEqual (obj .__dict__ , {})
395+ self .assertIs (type (obj .__dict__ ), dict )
396+
397+ # unmaterialized:
398+ obj = ManagedDictType ()
399+ obj .value = 'first'
400+ clear_managed_dict (obj )
401+ self .assertEqual (obj .__dict__ , {})
402+ self .assertIs (type (obj .__dict__ ), dict )
403+
404+ # CRASHES: clear_managed_dict(NULL)
405+
406+ @threading_helper .requires_working_threading ()
407+ @support .nomemtest
408+ def test_object_clear_managed_dict_ft_memory_error (self ):
409+ # gh-152254: Test PyObject_ClearManagedDict() error.
410+ # The setup must contain: object owned by another thread
411+ # and the attempt to clear the managed dict with no memory left.
412+ clear_managed_dict = _testcapi .pyobject_clear_managed_dict
413+ ManagedDictType = _testcapi .ManagedDictType
414+
415+ obj = ManagedDictType ()
416+ obj .value = 'first'
417+ self .assertEqual (len (obj .__dict__ ), 1 )
418+
419+ def worker ():
420+ # This needs to be in a separate thread for the crash to happen:
421+ with support .catch_unraisable_exception () as ex :
422+ # `PyObject_ClearManagedDict()` used to crash:
423+ _testcapi .call_with_nomemory (0 , 1 , clear_managed_dict , obj )
424+ self .assertIsNotNone (ex .unraisable )
425+ self .assertIs (ex .unraisable .exc_type , MemoryError )
426+ self .assertIn ('managed dict' , ex .unraisable .err_msg )
427+ self .assertEqual (obj .__dict__ , {})
428+ self .assertIs (type (obj .__dict__ ), dict )
429+
430+ threading_helper .run_concurrently ([worker ])
431+ self .assertEqual (obj .__dict__ , {})
432+ self .assertIs (type (obj .__dict__ ), dict )
433+
357434
358435class RefTracerTest (unittest .TestCase ):
359436 @support .requires_resource ('cpu' )
0 commit comments