为什么不立即释放匿名(C)Python对象? [英] Why aren't anonymous (C)Python objects deallocated immediately?

查看:279
本文介绍了为什么不立即释放匿名(C)Python对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到了一些关于CPython的对象释放的事情,这激起了我的好奇心。比方说,我定义了一个打印从 tp_dealloc 函数中的消息的类型:

  static void pyfoo_Bar_dealloc(pyfoo_Bar * self)
{
PySys_WriteStdout(Bar freed \\\
);
self> ob_type-> tp_free((PyObject *)self);
}

我也用分配器做了正确的事情:

  PyMODINIT_FUNC initpyfoo(void)
{
PyObject * m;

pyfoo_BarType.tp_new = PyType_GenericNew;
/ * ... * /
}

我编译并运行一个Python 2.6解释器在 pyfoo.so 目录中:

 > ;>> import pyfoo 
>>> a = pyfoo.Bar()
>>> a = None
已释放的栏位
>>> quit()

这就是我所期望的......参考计数降为零,收集 Bar 对象。但现在如果我这样做:

 >>> import pyfoo 
>>> pyfoo.Bar()
< pyfoo.Bar对象在0x7f7c6d8f2080>
>>> quit()
已释放

... Bar 对象在解释器退出前不会收集。但确实,匿名创建的 Bar 对象的引用计数为零,就像明确指定的引用计数一样。那为什么不在创建之后立即释放?



(我在Debian Squeeze上用Python 2.6.6编译了GCC 4.4.5,我知道它不是一个bug,我知道python-the-language并没有对Python解释器做任何特殊的约束......我只是想知道发生了什么,使得它忽略了这样的匿名对象。) pyfoo.Bar()之后,该对象仍然可以访问使用特殊对象 _



顺便说一下,这可以在纯Python中使用:

  class X:
def __del __(self):
print'deleted'

后来:

 >>> a = X()
>>> a = None
deleted
>>> X()
< __ main __。0x7f391bb066c8的X实例>
>>> _
< __ main __。0x7f391bb066c8的X实例>
>>> 3#原因_将被重新分配
删除
3

注意重新分配 _ 隐式删除 X 对象?


I noticed something about CPython's object deallocation which piqued my curiosity. Let's say I define a type that prints a message from its tp_dealloc function:

static void pyfoo_Bar_dealloc(pyfoo_Bar* self)
{    
    PySys_WriteStdout("Bar freed\n");   
    self->ob_type->tp_free((PyObject*)self);
}

I've also done the right thing with the allocator:

PyMODINIT_FUNC initpyfoo(void) 
{
    PyObject* m;

    pyfoo_BarType.tp_new = PyType_GenericNew;
    /* ... */
}

I compile it and run a Python 2.6 interpreter in the directory with pyfoo.so:

>>> import pyfoo
>>> a = pyfoo.Bar()
>>> a = None
Bar freed
>>> quit()

This is what I'd expect... the reference count drops to zero and the Bar object is collected. But now if I do:

>>> import pyfoo
>>> pyfoo.Bar()
<pyfoo.Bar object at 0x7f7c6d8f2080>
>>> quit()
Bar freed

... the Bar object is not collected until the interpreter exits. But surely the reference count on the Bar object created anonymously is zero, just like the one explicitly assigned. So why is it not deallocated immediately after creation?

(I'm doing this on Debian Squeeze with Python 2.6.6 compiled with GCC 4.4.5. I know it's not a "bug", I know Python-the-language doesn't place any particular constraints on Python interpreters this... I just want to know what is going on under the hood that makes it ignore anonymous objects like this.)

解决方案

Because after you called pyfoo.Bar() the object is still accessible using the special object _

This works with pure Python, by the way:

class X:
    def __del__(self):
        print 'deleted'

And later:

 >>>a = X()
 >>>a = None
 deleted
 >>>X()
 <__main__.X instance at 0x7f391bb066c8> 
 >>> _
 <__main__.X instance at 0x7f391bb066c8>
 >>> 3 # causes _ to be reassigned
 deleted
 3

Notice how reassigning _ implicitly deleted the X object?

这篇关于为什么不立即释放匿名(C)Python对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆