Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

To err is human -- to blame it on a computer is even more so.


devel / comp.lang.python / Re: PyObject_CallFunctionObjArgs segfaults

SubjectAuthor
* Re: PyObject_CallFunctionObjArgs segfaultsMRAB
`- Re: PyObject_CallFunctionObjArgs segfaultsGreg Ewing

1
Re: PyObject_CallFunctionObjArgs segfaults

<mailman.509.1664565725.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19619&group=comp.lang.python#19619

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: pyt...@mrabarnett.plus.com (MRAB)
Newsgroups: comp.lang.python
Subject: Re: PyObject_CallFunctionObjArgs segfaults
Date: Fri, 30 Sep 2022 20:18:55 +0100
Lines: 203
Message-ID: <mailman.509.1664565725.20444.python-list@python.org>
References: <ND8xHA---3-2@tutanota.com>
<fbd7fcc1-6095-e381-4721-bc5c09975202@mrabarnett.plus.com>
<ND9prTQ--3-2@tutanota.com-ND9qgBV----2> <ND9zJoA--7-2@tutanota.com>
<b60d1d3b-1381-8646-fd43-15496bf993dd@mrabarnett.plus.com>
<NDAPOvd--3-2@tutanota.com>
<aadf0f47-468b-158e-a0fc-82bb72fa2b98@mrabarnett.plus.com>
<4e8fa90c-6f59-1999-93bc-2a31ba14c12e@mrabarnett.plus.com>
<NDE7c5l--3-2@tutanota.com>
<65c6d3ef-c426-d486-565b-549442598eda@mrabarnett.plus.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de BuEny0BOZVyGWSJ/kqHY9wR84P5O7vToiWclHJti2WcQ==
Return-Path: <python@mrabarnett.plus.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=plus.com header.i=@plus.com header.b=DLcuI47s;
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'comments': 0.03; 'string':
0.07; '"new': 0.09; 'cc:addr:python-list': 0.09;
'from:addr:python': 0.09; "hasn't": 0.09; 'lookup': 0.09;
'received:192.168.1.64': 0.09; 'something,': 0.09; 'cc:no real
name:2**0': 0.14; 'questions.': 0.14; 'url:mailman': 0.15;
'(because': 0.16; '2022,': 0.16; '[snip]': 0.16; 'assuming': 0.16;
'comment.': 0.16; 'confused': 0.16; 'followup': 0.16;
'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16;
'message-id:@mrabarnett.plus.com': 0.16; 'not.\xc2\xa0': 0.16;
'object,': 0.16; 'operations,': 0.16; 'received:84.93': 0.16;
'received:84.93.230': 0.16; 'received:plus.net': 0.16;
'reference"': 0.16; 'reference,': 0.16; 'releases': 0.16;
'solved': 0.16; 'temporarily,': 0.16; 'wrote:': 0.16; 'api': 0.17;
'says': 0.17; 'values': 0.17; 'probably': 0.17;
'cc:addr:python.org': 0.20; 'input': 0.21; "i've": 0.22;
'basically': 0.22; 'creates': 0.22; 'problem,': 0.22; 'skip:p 30':
0.23; 'url-ip:188.166.95.178/32': 0.25; 'url-ip:188.166.95/24':
0.25; 'help.': 0.25; 'url:listinfo': 0.25; 'cc:2**0': 0.25; 'url-
ip:188.166/16': 0.25; 'anyone': 0.25; 'object': 0.26; 'leave':
0.27; 'else': 0.27; 'function': 0.27; 'example,': 0.28; 'it,':
0.29; 'header:User-Agent:1': 0.30; 'effect': 0.31; 'url-ip:188/8':
0.31; 'think': 0.32; 'official': 0.32; 'guess': 0.32; 'needed,':
0.32; 'objects': 0.32; 'returning': 0.32; 'said,': 0.32; 'sep':
0.32; 'received:192.168.1': 0.32; 'but': 0.32; 'there': 0.33;
'hold': 0.33; 'url:blog': 0.33; 'mean': 0.34; 'header:In-Reply-
To:1': 0.34; 'question.': 0.35; 'yes,': 0.35; 'count': 0.36;
'functions': 0.36; 'member': 0.37; 'using': 0.37; "it's": 0.37;
'others': 0.37; 'received:192.168': 0.37; 'way': 0.38; 'could':
0.38; 'means': 0.38; 'thanks': 0.38; 'list': 0.39; 'use': 0.39;
'shared': 0.39; 'still': 0.40; 'should': 0.40; 'helps': 0.60;
'reference': 0.60; 'remember': 0.61; "there's": 0.61; 'here':
0.62; 'once': 0.63; "you'd": 0.64; 'finished': 0.64; 'your': 0.64;
'also:': 0.69; 'them,': 0.70; 'performance': 0.71; 'longer': 0.71;
'free': 0.72; 'reference.': 0.81; 'returned': 0.81; 'garbage':
0.84; 'increments': 0.84; 'ownership.': 0.84; 'pointer': 0.84;
'reduces': 0.84; 'required)': 0.84; 'said:': 0.84; 'say,': 0.84;
'steal': 0.84; 'when,': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=plus.com; s=042019;
t=1664565537; bh=ra9J7luu8noFpbEDH1jwGNc38qKf4PQYHFG1k+Trmn0=;
h=Date:Subject:To:Cc:References:From:In-Reply-To;
b=DLcuI47sfR+9lBsfOHv+gng1hk4JNle08GFeAKWCmWcpXyKep+5sJZ8rGeCvXVqJu
WLGGMBFIeDlz+SU3yUZ5Jhfqi1k0A7TDYCxbcvIoPk38kfsZbk702CV//uuR/MrH9j
PLOs1VbkE4fgAhrTjJcecDdj7tKWyZL0cNiQpCe6Bxhe3de4wEGgQVIlsD+LP6C3qR
bf1+Ss0Vgh6PQIEruGxgaZuZVf5hnDfnsZxASct4hq4U/cB4bFZIAUNKdo/cq7mWd9
Etl7X5Rg08hVehnzYd3pBO3UQHtesNAmEqmYWtirbIFnwlL1K921Sgdqf++tYFeDEe
LrB+BhQMqFThg==
X-Clacks-Overhead: "GNU Terry Pratchett"
X-CM-Score: 0.00
X-CNFS-Analysis: v=2.4 cv=M/5elg8s c=1 sm=1 tr=0 ts=63374121
a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17
a=IkcTkHD0fZMA:10 a=uPZiAMpXAAAA:8 a=Ax6nnwu1AAAA:8 a=EBOSESyhAAAA:8
a=8AHkEIZyAAAA:8 a=VocDfwHW0Pp8fM6N7CwA:9 a=QEXdDO2ut3YA:10
a=o1J51AizQ82FDRWIhIQo:22 a=yJM6EZoI5SlJf8ks9Ge_:22
X-AUTH: mrabarnett@:2500
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.3.0
Content-Language: en-GB
In-Reply-To: <NDE7c5l--3-2@tutanota.com>
X-CMAE-Envelope: MS4xfD9BBaUf5TftwxkKL0umB57Hbw4qRtOez9yfgNyc+w07CV4mmL7suOJZ5w/uuIhIXtx4f2ScP1spJf91v++yKyA/owvXk60bYUQPHXxzPjUitKtbeXWG
K9NLyHq2JPC24twCQqaSmMQ18A9Kr2I+uh0hIEvztR+ekKZ0recDqmMmqOm9+acBNBgz/F/pEg2CzvqpBLr40hmGpGRolBNANLk=
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <https://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>
X-Mailman-Original-Message-ID: <65c6d3ef-c426-d486-565b-549442598eda@mrabarnett.plus.com>
X-Mailman-Original-References: <ND8xHA---3-2@tutanota.com>
<fbd7fcc1-6095-e381-4721-bc5c09975202@mrabarnett.plus.com>
<ND9prTQ--3-2@tutanota.com-ND9qgBV----2> <ND9zJoA--7-2@tutanota.com>
<b60d1d3b-1381-8646-fd43-15496bf993dd@mrabarnett.plus.com>
<NDAPOvd--3-2@tutanota.com>
<aadf0f47-468b-158e-a0fc-82bb72fa2b98@mrabarnett.plus.com>
<4e8fa90c-6f59-1999-93bc-2a31ba14c12e@mrabarnett.plus.com>
<NDE7c5l--3-2@tutanota.com>
 by: MRAB - Fri, 30 Sep 2022 19:18 UTC

On 2022-09-30 17:02, Jen Kris wrote:
>
> Thanks very much for your detailed reply.  I have a few followup
> questions.
>
> You said, “Some functions return an object that has already been
> incref'ed ("new reference"). This occurs when it has either created a
> new object (the refcount will be 1) or has returned a pointer to an
> existing object (the refcount will be > 1 because it has been
> incref'ed).  Other functions return an object that hasn't been
> incref'ed. This occurs when you're looking up something, for example,
> looking at a member of a list or the value of an attribute.”
>
> In the official docs some functions show “Return value: New reference”
> and others do not.  Is there any reason why I should not just INCREF
> on every new object, regardless of whether it’s a new reference or
> not, and DECREF when I am finished with it?  The answer at
> https://stackoverflow.com/questions/59870703/python-c-extension-need-to-py-incref-a-borrowed-reference-if-not-returning-it-to
> says “With out-of-order execution, the INCREF/DECREF are basically
> free operations, so performance is no reason to leave them out.” 
> Doing so means I don’t have to check each object to see if it needs to
> be INCREF’d or not, and that is a big help.
>
It's OK to INCREF them, provided that you DECREF them when you no longer
need them, and remember that if it's a "new reference" you'd need to
DECREF it twice.
> Also:
>
> What is a borrowed reference, and how does it effect reference
> counting?  According to
> https://jayrambhia.com/blog/pythonc-api-reference-counting, “Use
> Py_INCREF on a borrowed PyObject pointer you already have. This
> increments the reference count on the object, and obligates you to
> dispose of it properly.”  So I guess it’s yes, but I’m confused by
> “pointer you already have.”

A borrowed reference is when it hasn't been INCREFed.

You can think of INCREFing as a way of indicating ownership, which is
often shared ownership (refcount > 1). When you're borrowing a
reference, you're using it temporarily, but not claiming ownership. When
the last owner releases its ownership (DECREF reduces the refcount to
0), the object can be garbage collected.

When, say, you lookup an attribute, or get an object from a list with
PyList_GetItem, it won't have been INCREFed. You're using it
temporarily, just borrowing a reference.
>
> What does it mean to steal a reference?  If a function steals a
> reference does it have to decref it without incref (because it’s stolen)?
When function steals a reference, it's claiming ownership but not
INCREFing it.
>
> Finally, you said:
>
> if (pMod_random == 0x0){
>     PyErr_Print();
> Leaks here because of the refcount
>
> Assuming pMod_random is not null, why would this leak?
>
It's pName_random that's the leak.

PyUnicode_FromString("random") will either create and return a new
object for the string "random" (refcount == 1) or return a reference to
an existing object (refcount > 1). You need to DECREF it before
returning from the function.

Suppose it created a new object. You call the function, it creates an
object, you use it, then return from the function. The object still
exists, but there's no reference to it. Now call the function again. It
creates another object, you use it, then return from the function. You
now have 2 objects with no reference to them.

> Thanks again for your input on this question.
>
> Jen
>
>
>
> Sep 29, 2022, 17:33 by python@mrabarnett.plus.com:
>
> On 2022-09-30 01:02, MRAB wrote:
>
> On 2022-09-29 23:41, Jen Kris wrote:
>
>
> I just solved this C API problem, and I’m posting the
> answer to help anyone else who might need it.
>
> [snip]
>
> What I like to do is write comments that state which variables
> hold a reference, followed by '+' if it's a new reference
> (incref'ed) and '?' if it could be null. '+?' means that it's
> probably a new reference but could be null. Once I know that it's
> not null, I can remove the '?', and once I've decref'ed it (if
> required) and no longer need it, I remobe it from the comment.
>
> Clearing up references, as soon as they're not needed, helps to
> keep the number of current references more manageable.
>
>
> int64_t Get_LibModules(int64_t * return_array) {
> PyObject * pName_random = PyUnicode_FromString("random");
> //> pName_random+?
> if (!pName_random) {
> PyErr_Print();
> return 1;
> }
>
> //> pName_random+
> PyObject * pMod_random = PyImport_Import(pName_random);
> //> pName_random+ pMod_random+?
> Py_DECREF(pName_random);
> //> pMod_random+?
> if (!pMod_random) {
> PyErr_Print();
> return 1;
> }
>
> //> pMod_random+
> PyObject * pAttr_seed = PyObject_GetAttrString(pMod_random, "seed");
> //> pMod_random+ pAttr_seed?
> if (!pAttr_seed) {
> Py_DECREF(pMod_random);
> PyErr_Print();
> return 1;
> }
>
> //> pMod_random+ pAttr_seed
> PyObject * pAttr_randrange = PyObject_GetAttrString(pMod_random,
> "randrange");
> //> pMod_random+ pAttr_seed pAttr_randrange?
> Py_DECREF(pMod_random);
> //> pAttr_seed pAttr_randrange?
> if (!pAttr_randrange) {
> PyErr_Print();
> return 1;
> }
>
> //> pAttr_seed pAttr_randrange
> return_array[0] = (int64_t)pAttr_seed;
> return_array[1] = (int64_t)pAttr_randrange;
>
> return 0;
> }
>
> int64_t C_API_2(PyObject * pAttr_seed, Py_ssize_t value_1) {
> PyObject * value_ptr = PyLong_FromLong(value_1);
> //> value_ptr+?
> if (!!value_ptr) {
> PyErr_Print();
> return 1;
> }
>
> //> value_ptr+
> PyObject * p_seed_calc = PyObject_CallFunctionObjArgs(pAttr_seed,
> value_ptr, NULL);
> //> value_ptr+ p_seed_calc+?
> Py_DECREF(value_ptr);
> //> p_seed_calc+?
> if (!p_seed_calc) {
> PyErr_Print();
> return 1;
> }
>
> //> p_seed_calc+
> Py_DECREF(p_seed_calc);
> return 0;
> }
>
> int64_t C_API_12(PyObject * pAttr_randrange, Py_ssize_t value_1) {
> PyObject * value_ptr = PyLong_FromLong(value_1);
> //> value_ptr+?
> if (!value_ptr) {
> PyErr_Print();
> return 1;
> }
>
> //> value_ptr+
> PyObject * p_randrange_calc =
> PyObject_CallFunctionObjArgs(pAttr_randrange, value_ptr, NULL);
> //> value_ptr+ p_randrange_calc+?
> Py_DECREF(value_ptr);
> //> p_randrange_calc+?
> if (!p_randrange_calc) {
> PyErr_Print();
> return 1;
> }
>
> //Prepare return values
> //> p_randrange_calc+
> return_val = PyLong_AsLong(p_randrange_calc);
> Py_DECREF(p_randrange_calc);
>
> return return_val;
> }
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
>

Re: PyObject_CallFunctionObjArgs segfaults

<jpp3knFdrouU1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19621&group=comp.lang.python#19621

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: greg.ew...@canterbury.ac.nz (Greg Ewing)
Newsgroups: comp.lang.python
Subject: Re: PyObject_CallFunctionObjArgs segfaults
Date: Sat, 1 Oct 2022 09:59:00 +1300
Lines: 12
Message-ID: <jpp3knFdrouU1@mid.individual.net>
References: <ND8xHA---3-2@tutanota.com>
<fbd7fcc1-6095-e381-4721-bc5c09975202@mrabarnett.plus.com>
<ND9prTQ--3-2@tutanota.com-ND9qgBV----2> <ND9zJoA--7-2@tutanota.com>
<b60d1d3b-1381-8646-fd43-15496bf993dd@mrabarnett.plus.com>
<NDAPOvd--3-2@tutanota.com>
<aadf0f47-468b-158e-a0fc-82bb72fa2b98@mrabarnett.plus.com>
<4e8fa90c-6f59-1999-93bc-2a31ba14c12e@mrabarnett.plus.com>
<NDE7c5l--3-2@tutanota.com>
<65c6d3ef-c426-d486-565b-549442598eda@mrabarnett.plus.com>
<mailman.509.1664565725.20444.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net Vy0zkw4RdPsHtjUGjB06jw5eyPmfDY81JDsTygHlDviLGBkcGj
Cancel-Lock: sha1:71qzI+9qnZJRN8L30zJGyl3rmro=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:91.0)
Gecko/20100101 Thunderbird/91.3.2
Content-Language: en-US
In-Reply-To: <mailman.509.1664565725.20444.python-list@python.org>
 by: Greg Ewing - Fri, 30 Sep 2022 20:59 UTC

On 1/10/22 8:18 am, MRAB wrote:
> It's OK to INCREF them, provided that you DECREF them when you no longer
> need them, and remember that if it's a "new reference" you'd need to
> DECREF it twice.

Which means there would usually be no point in doing the extra
INCREF/DECREF. You still need to know whether it's a new reference
or not and treat it accordingly.

--
Greg

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor