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


Groups > comp.lang.python > #98710

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 Jason Swails <jason.swails@gmail.com>
Newsgroups comp.lang.python
Subject Re: What is the meaning of Py_INCREF a static PyTypeObject?
Date Thu, 12 Nov 2015 13:52:10 -0500
Lines 47
Message-ID <mailman.278.1447354333.16136.python-list@python.org> (permalink)
References <56444835.5020803@126.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de dgEuUhqhVjVPwBucm1ry9wUaofFVTsuUVteo3OPJUXug==
Return-Path <jason.swails@gmail.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; 'cc:addr:python-list': 0.09; 'destroyed.': 0.09; 'garbage': 0.09; 'imported': 0.09; 'macros': 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; 'definition)': 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; '(the': 0.22; 'struct': 0.22; 'trying': 0.22; 'am,': 0.23; 'seems': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'message- id:@mail.gmail.com': 0.27; 'defining': 0.27; 'function': 0.28; 'objects': 0.29; 'tutorial': 0.29; 'normally': 0.30; 'point': 0.33; 'url:python': 0.33; 'received:google.com': 0.35; '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; 'received:209.85': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'really': 0.37; '12,': 0.37; 'doing': 0.38; 'received:209': 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; '&amp;': 0.61; 'skip:n 10': 0.62; 'you.': 0.64; 'zhang': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=u/9OSh1lJfPtCXvuDrKx10VW91w6xVWrKzVBQyF6PWM=; b=ZgtJPIYVPTcGTU/ZH9oiu4SmIOOjB5BSRAAlImX/6tQMtoyOFX/S8HdxncHg6vbE61 PptALuBLme7MZEbStAyPlfAeqRQG2pJod3PCi+f8lOb7SY6/7F3mBeju9JR2IpNeblMe 6O0DxZ0LlyuG5nZFWqQ7C2TvBj8n3rHXI5lzE6YaQqPI48Wa5KNiCZxLgPchW//qRV72 n+n4m4l3JTiOAu58zyH13fQctXMrbrizLh8naFEO8O6dNeXKGQmZOOfddPImFf1xp13V MsxzBlvpHcNZLaS4JqZfl2TwmESrrFEmwKGoBpBwnGGHdy1Ky1aQ+Z248iou32xs/7vT +ZUg==
X-Received by 10.13.229.3 with SMTP id o3mr16200632ywe.341.1447354330545; Thu, 12 Nov 2015 10:52:10 -0800 (PST)
In-Reply-To <56444835.5020803@126.com>
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:98710

Show key headers only | View raw


On Thu, Nov 12, 2015 at 3:05 AM, Xiang Zhang <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? Jason Swails <jason.swails@gmail.com> - 2015-11-12 13:52 -0500

csiph-web