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 == NULL); 63 #endif 64 } 65 66 if (table_is_malloced) 67 PyMem_DEL(table); 68 }

 

转载于:https://www.cnblogs.com/hackerl/p/6099500.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值