1 void2Pydict_clear (Pyobject *op)3 {4Dictobject *MP;5Dictentry *EP, *table;6 inttable_is_malloced;7 py_ssize_t fill;8 dictentry small_copy[pydict_minsize];9 #ifdef Py_debugTen py_ssize_t i, N; One #endif A - if(!Pydict_check (OP)) - return; theMP = (Dictobject *) op; - #ifdef Py_debug -n = mp->ma_mask +1; -i =0; + #endif - +Table = mp->ma_table; AASSERT (Table! =NULL); attable_is_malloced = table! = mp->ma_smalltable; - - /*This is delicate. During the process of clearing the dict, - * Decrefs can cause the dict to mutate. To avoid fatal confusion - * (voice of experience), we have the dict empty before - * Clearing the slots, and never refer to anything via mp->xxx while in * clearing. - */ toFill = mp->Ma_fill; + if(table_is_malloced) - Empty_to_minsize (MP); the * Else if(Fill >0) { $ /*It's a small table with something this needs to be cleared.Panax Notoginseng * Afraid the only safe-to-copy the dict entries into - * Another small table first. the */ +memcpy (small_copy, table,sizeof(small_copy)); ATable =small_copy; the Empty_to_minsize (MP); + } - /*else it ' s a small table that's already empty*/ $ $ /*Now we can finally clear things. If C had refcounts, we could - * assert that the refcount on table are 1 now, i.e. - * has a unique access to it, so decref side-effects can ' t alter it. the */ - for(ep = table; fill >0; ++EP) {Wuyi #ifdef Py_debug theASSERT (I <n); -++i; Wu #endif - if(ep->Me_key) { About--fill; $Py_decref (ep->me_key); -Py_xdecref (ep->me_value); //is only handled here, but if value is a dictionary and there are other references, then it cannot be deleted, and I think I should recursively delete
/*i
F (Pydict_check (Op->me_value )
{
Pydict_clear (op-& Gt;me_value);
}
else{
Py_xdecref ( ep->me_value) ;
}
*/
- } - #ifdef Py_debug A Else +ASSERT (Ep->me_value = =NULL); the #endif - } $ the if(table_is_malloced) the Pymem_del (table); the}
Python dict Clear can only delete one layer and cannot be deleted recursively.