Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98738

Re: What is the meaning of Py_INCREF a static PyTypeObject?

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Xiang Zhang <18518281186@126.com>
Newsgroups comp.lang.python
Subject Re: What is the meaning of Py_INCREF a static PyTypeObject?
Date Fri, 13 Nov 2015 11:08:27 +0800
Lines 65
Message-ID <mailman.285.1447419646.16136.python-list@python.org> (permalink)
References <56444835.5020803@126.com> <CAEk9e3pP6H=6P68p6r0iNiPYh0HF9kGemTFTnPbyLf2Acx1wgg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de 4bOM0Mqt2SSsn498zigAwgJ0RQNfI2NESC7lWtSu2V9w==
Return-Path <18518281186@126.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'static': 0.03; 'api.': 0.04; '<>,': 0.07; 'mentioned,': 0.07; 'cc:addr:python-list': 0.09; 'created,': 0.09; 'destroyed.': 0.09; 'garbage': 0.09; 'imported': 0.09; 'macros': 0.09; 'message-id:@126.com': 0.09; 'namespace': 0.09; 'python': 0.10; 'python.': 0.11; 'thu,': 0.15; '*always*': 0.16; 'attribute,': 0.16; 'cc:name:python list': 0.16; 'deallocated': 0.16; 'definition)': 0.16; 'heap': 0.16; 'module).': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'segfault': 0.16; 'unnecessary.': 0.16; 'wrote:': 0.16; 'module,': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'prevent': 0.20; '(the': 0.22; 'struct': 0.22; 'trying': 0.22; 'am,': 0.23; 'seems': 0.23; "python's": 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'header:User- Agent:1': 0.26; 'least': 0.27; 'defining': 0.27; 'said,': 0.27; 'function': 0.28; 'objects': 0.29; 'tutorial': 0.29; 'code': 0.30; 'normally': 0.30; 'holds': 0.32; 'maybe': 0.33; 'point': 0.33; 'url:python': 0.33; 'could': 0.35; 'done': 0.35; 'fail': 0.35; 'jason': 0.35; 'nov': 0.35; 'something': 0.35; 'skip:p 30': 0.35; 'sometimes': 0.35; 'but': 0.36; 'url:org': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'really': 0.37; 'being': 0.37; 'thanks': 0.37; '12,': 0.37; 'doing': 0.38; 'wrong': 0.38; 'skip:p 20': 0.38; 'mean': 0.38; 'why': 0.39; 'does': 0.39; 'subject:the': 0.39; 'some': 0.40; 'url:3': 0.60; 'your': 0.60; '&amp;': 0.61; 'reach': 0.61; 'skip:n 10': 0.62; 'more': 0.63; 'you.': 0.64; 'hints': 0.66; 'reply': 0.68; 'lives': 0.72; 'action.': 0.84; 'using.': 0.84; 'zhang': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=Subject:From:Message-ID:Date:MIME-Version; bh=C9ZeO HOyEJ+QFZDHRz7CWqWwYN5kDpuOP8ynEnwZ3oM=; b=Wi9ojMaai4dtqP7/p8S4a nmkTtU2P+jzOlulyIc0lnSutjMfbKpwWN7s70+kJH+SU9nfv3xzz3QxvPutOxHSN ijn8uWfNx7tdnD8YTdUrWHYGAn25puROkQP82DDq8AKNntMxZ0xKjCTMQuotNYl2 P2iVSU7vudFVi+42T41GEU=
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0
In-Reply-To <CAEk9e3pP6H=6P68p6r0iNiPYh0HF9kGemTFTnPbyLf2Acx1wgg@mail.gmail.com>
X-CM-TRANSID j9KowABndTkrVEVWUgmECQ--.2758S2
X-Coremail-Antispam 1Uf129KBjvJXoWxXFWDJrW7Kw43tFWxZF4xZwb_yoW5Gr4xpF 4S9w4UGrWkXr4xtFWkAw4UurWIkrZrCFW3JayYqry2vw45Xa4fKw4UK390qa43Crs7GFyY qw4Uur4UGay3Za7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jiID7UUUUU=
X-Originating-IP [61.135.152.207]
X-CM-SenderInfo 5dqj4vxu20qiyswou0bp/1tbiHg6x-lPguxeYvQAAsl
X-Mailman-Approved-At Fri, 13 Nov 2015 08:00:45 -0500
X-Content-Filtered-By Mailman/MimeDel 2.1.20+
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:98738

Show key headers only | View raw


Thanks for your reply jason. Your reply does give me hints and then I 
read more code and find maybe you are wrong in some points.

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.

On 11/13/2015 02:52 AM, Jason Swails wrote:
>
>
> On Thu, Nov 12, 2015 at 3:05 AM, Xiang Zhang <18518281186@126.com 
> <mailto:18518281186@126.com>> wrote:
>
>     Recently I am learning Python C API.
>
>     When I read the tutorial
>     <https://docs.python.org/3/extending/newtypes.html#the-basics>,
>     defining new types, I feel confused. After
>     PyType_Ready(&noddy_NoddyType) comes Py_INCREF(&noddy_NoddyType).
>     Actually noddy_NoddyType is a static struct so I don't understand
>     why I need to Py_INCREF it. Since it's Py_INCREFed, does it mean
>     sometimes we also need to Py_DECREF it? But then it seems that
>     type_dealloc will be invoked and it will fail
>     assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
>
>
> ​It is a module attribute, so when the module is imported it has to 
> have a single reference (the reference *in* the module).  If you don't 
> INCREF it, then it will have a refcount of 0, and immediately be ready 
> for garbage collection.  So if you try to use the type from the 
> module, you could get a segfault because it's trying to use an object 
> (type definition) that was already destroyed.
>
> Note that you don't *always* have to INCREF objects after you create 
> them in C.  Some macros and function do that for you. And in some 
> cases, all you want or need is a borrowed reference.  In those cases, 
> Py_INCREF is unnecessary.
>
> The DECREF will be done when it's normally done in Python.  If you do 
> something like
>
> import noddy
> del noddy.NoddyType
>
> ​All that's really doing is removing NoddyType from the noddy 
> namespace and Py_DECREFing it.  Alternatively, doing
>
> import noddy
> noddy.NoddyType = 10 # rebind the name
>
> Then the original object NoddyType was pointing to will be DECREFed 
> and NoddyType will point to an object taking the value of 10.
>
> HTH,
> Jason

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: What is the meaning of Py_INCREF a static PyTypeObject? Xiang Zhang <18518281186@126.com> - 2015-11-13 11:08 +0800

csiph-web