首页 > 代码库 > python dict clear只能删除一层,不能够递归删除。
python dict clear只能删除一层,不能够递归删除。
1 void 2 PyDict_Clear(PyObject *op) 3 { 4 dictobject *mp; 5 dictentry *ep, *table; 6 int table_is_malloced; 7 Py_ssize_t fill; 8 dictentry small_copy[PyDict_MINSIZE]; 9 #ifdef Py_DEBUG 10 Py_ssize_t i, n; 11 #endif 12 13 if (!PyDict_Check(op)) 14 return; 15 mp = (dictobject *)op; 16 #ifdef Py_DEBUG 17 n = mp->ma_mask + 1; 18 i = 0; 19 #endif 20 21 table = mp->ma_table; 22 assert(table != NULL); 23 table_is_malloced = table != mp->ma_smalltable; 24 25 /* This is delicate. During the process of clearing the dict, 26 * decrefs can cause the dict to mutate. To avoid fatal confusion 27 * (voice of experience), we have to make the dict empty before 28 * clearing the slots, and never refer to anything via mp->xxx while 29 * clearing. 30 */ 31 fill = mp->ma_fill; 32 if (table_is_malloced) 33 EMPTY_TO_MINSIZE(mp); 34 35 else if (fill > 0) { 36 /* It‘s a small table with something that needs to be cleared. 37 * Afraid the only safe way is to copy the dict entries into 38 * another small table first. 39 */ 40 memcpy(small_copy, table, sizeof(small_copy)); 41 table = small_copy; 42 EMPTY_TO_MINSIZE(mp); 43 } 44 /* else it‘s a small table that‘s already empty */ 45 46 /* Now we can finally clear things. If C had refcounts, we could 47 * assert that the refcount on table is 1 now, i.e. that this function 48 * has unique access to it, so decref side-effects can‘t alter it. 49 */ 50 for (ep = table; fill > 0; ++ep) { 51 #ifdef Py_DEBUG 52 assert(i < n); 53 ++i; 54 #endif 55 if (ep->me_key) { 56 --fill; 57 Py_DECREF(ep->me_key); 58 Py_XDECREF(ep->me_value);//只有这里处理了,但是如果value是一个字典,并且有其他地方引用了,那么就不能删掉了,我觉得应该递归删除
/*i
f(PyDict_Check(op->me_value))
{
PyDict_Clear(op->me_value);
}
else{
Py_XDECREF(ep->me_value);
}
*/
59 } 60 #ifdef Py_DEBUG 61 else 62 assert(ep->me_value =http://www.mamicode.com/= NULL); 63 #endif 64 } 65 66 if (table_is_malloced) 67 PyMem_DEL(table); 68 }
python dict clear只能删除一层,不能够递归删除。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。