Python dict Clear can only delete one layer and cannot be deleted recursively.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.