Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #98744
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: What is the meaning of Py_INCREF a static PyTypeObject? |
| Date | 2015-11-13 14:59 +0000 |
| Message-ID | <mailman.290.1447426808.16136.python-list@python.org> (permalink) |
| References | <56444835.5020803@126.com> <CAEk9e3pP6H=6P68p6r0iNiPYh0HF9kGemTFTnPbyLf2Acx1wgg@mail.gmail.com> <5645542B.9070602@126.com> |
On 13 November 2015 at 03:08, Xiang Zhang <18518281186@126.com> wrote:
> I think the meaning of Py_INCREF a static type object is to prevent it from
> being deallocated when it is Py_DECREFed somehow later. Just as you said, it
> may be somehow deallocated when using.
>
> NoddyType is a static struct so I don't think it lives on Python's heap and
> deallocating it is a wrong action. Just as I mentioned, type_dealloc seems
> to only deallocated HEAPTYPE. And by the way, when NoddyType is created, it
> has a reference count 1 with PyVarObject_HEAD_INIT. So if I don't Py_INCREF
> it, when it is somehow Py_DECREDed later, it will reach reference count 0
> and fail the assert in type_dealloc. If it is Py_INCREFed, just like the
> module holds a reference to it, it will at least have a reference count 1
> and never reach 0.
Other code shouldn't Py_DECREF it unless it owns a reference meaning
that it has first called Py_INCREF. The only exception I can think is
possibly that someone would do:
>>> import noddy
>>> del noddy.Noddy
For that to work properly I guess that Noddy needs a reference count
of 2: one for being a module attribute and an extra one to prevent it
from ever being deallocated.
--
Oscar
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: What is the meaning of Py_INCREF a static PyTypeObject? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-11-13 14:59 +0000
csiph-web