Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Oscar Benjamin Newsgroups: comp.lang.python Subject: Re: What is the meaning of Py_INCREF a static PyTypeObject? Date: Fri, 13 Nov 2015 14:59:41 +0000 Lines: 27 Message-ID: References: <56444835.5020803@126.com> <5645542B.9070602@126.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de jzgefOa5fCzRMUXxf20e1wfuMMEbAuly5cKjuwlwOjcQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'static': 0.03; 'mentioned,': 0.07; 'cc:addr:python-list': 0.09; 'created,': 0.09; 'exception': 0.13; 'properly': 0.15; 'cc:name:python list': 0.16; 'deallocated': 0.16; 'heap': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'attribute': 0.18; "shouldn't": 0.18; '>>>': 0.20; '2015': 0.20; 'cc:addr:python.org': 0.20; 'prevent': 0.20; 'cc:2**1': 0.22; 'struct': 0.22; 'seems': 0.23; "python's": 0.23; 'import': 0.24; 'cc:addr:gmail.com': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'least': 0.27; 'message-id:@mail.gmail.com': 0.27; 'said,': 0.27; 'code': 0.30; 'guess': 0.31; 'holds': 0.32; 'possibly': 0.32; 'received:google.com': 0.35; 'fail': 0.35; 'received:209.85': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'received:209': 0.38; 'wrong': 0.38; 'skip:p 20': 0.38; 'someone': 0.38; 'subject:the': 0.39; 'called': 0.40; 'ever': 0.60; 'reach': 0.61; 'lives': 0.72; 'action.': 0.84; 'oscar': 0.84; 'using.': 0.84; 'zhang': 0.84; 'do:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=C5FoXSW0y856lHM6weC/vqTC8NS92EOuB9zdXaFooCs=; b=oQVrHB37G/dOlKsrBUuTWOc3hrRgoGKZ9655+an4Qgn/uhwjBEckljdT50fI/UoX8S v4t1lwlSdBqtpAOy7yMNHM9m8/VogeIOZ5wIleLMu3/k4BCWrTe23cJ0Y+8zVOs55NGn UvHIXZRaA2jg55tl5UWnw1n6dTXdCB1DQomyx9WGkECfUG5NPbBdN4Byl6LwSRMBg9oj 8bxTS3TFtPT1pRn3Vgv1u02RYgU/QcKRJwd9ZpFruc/Cph0XCGly4f0pkjGBEKIX5yGw siRysuvCOs95rCr7RhlES7rXixKlSHORTRxAAbs5LF/WsFLI1BICdmmo+TZ362GoeYQe klpg== X-Received: by 10.25.135.136 with SMTP id j130mr10345106lfd.95.1447426801298; Fri, 13 Nov 2015 07:00:01 -0800 (PST) In-Reply-To: <5645542B.9070602@126.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98744 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