Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"Never make any mistaeks." (Anonymous, in a mail discussion about to a kernel bug report.)


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

SubjectAuthor
o Re: PyObject_CallFunctionObjArgs segfaultsJen Kris

1
Re: PyObject_CallFunctionObjArgs segfaults

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: jenkris@tutanota.com (Jen Kris)
Newsgroups: comp.lang.python
Subject: Re: PyObject_CallFunctionObjArgs segfaults
Date: Thu, 29 Sep 2022 22:47:14 +0200 (CEST)
Lines: 191
Message-ID: <mailman.500.1664484437.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>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de wHLRyhu+f3+XAmCknZDrkwzS1I+HOC4Ys1qW3BgWfvQg==
Return-Path: <jenkris@tutanota.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=tutanota.com header.i=@tutanota.com header.b=MSmwYsnv;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'comments': 0.03;
'argument': 0.04; '3.8': 0.05; 'library.': 0.05; 'random': 0.05;
'used.': 0.07; 'wrong.': 0.07; 'cc:addr:python-list': 0.09;
'didn\xe2\x80\x99t': 0.09; 'moved': 0.09; 'segmentation': 0.09;
'cc:no real name:2**0': 0.14; 'url:mailman': 0.15; 'problem.':
0.15; '2022,': 0.16; 'answer.': 0.16; 'arguments': 0.16;
'directory.': 0.16; 'email addr:python.org:': 0.16; 'incremented':
0.16; 'nltk': 0.16; 'pointers': 0.16; 'problem.\xc2\xa0': 0.16;
'program:': 0.16; 'pyobject*': 0.16; 'skip:. 20': 0.16; 'value?':
0.16; 'wrote:': 0.16; 'python': 0.16; 'api': 0.17; 'values': 0.17;
'instead': 0.17; 'solve': 0.19; 'cc:addr:python.org': 0.20;
'written': 0.22; 'problem,': 0.22; 'returns': 0.22; 'code': 0.23;
'cc:2**1': 0.23; 'skip:p 30': 0.23; 'received:de': 0.23; 'url-
ip:188.166.95.178/32': 0.25; 'url-ip:188.166.95/24': 0.25;
'url:listinfo': 0.25; 'url-ip:188.166/16': 0.25; 'seems': 0.26;
'tried': 0.26; 'library': 0.26; '>>>': 0.28; 'ideas': 0.28;
'recently': 0.29; 'program,': 0.31; 'url-ip:188/8': 0.31;
'program': 0.31; 'question': 0.32; '(as': 0.32; 'objects': 0.32;
'python-list': 0.32; 'sep': 0.32; 'signal': 0.32;
'received:192.168.1': 0.32; 'but': 0.32; 'same': 0.34; 'header:In-
Reply-To:1': 0.34; '8bit%:40': 0.35; 'using': 0.37; "it's": 0.37;
'this.': 0.37; 'received:192.168': 0.37; 'file': 0.38; 'way':
0.38; 'thanks': 0.38; 'list': 0.39; 'use': 0.39; 'still': 0.40;
'program.': 0.40; 'both': 0.40; 'reference': 0.60; 'best': 0.61;
'here': 0.62; 'clear': 0.64; 'verified': 0.64; 'your': 0.64;
'well': 0.65; 'now,': 0.67; 'right': 0.68; 'before,': 0.69;
'cast': 0.69; 'refers': 0.69; 'below': 0.69; '8bit%:38': 0.70;
'8bit%:6': 0.71; 'skip:\xe2 10': 0.71; 'documented': 0.76;
'correct.\xc2\xa0': 0.84; 'imports': 0.84; 'pointer': 0.84
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1664484434;
s=s1; d=tutanota.com;
h=From:From:To:To:Subject:Subject:Content-Description:Content-ID:Content-Type:Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:In-Reply-To:In-Reply-To:MIME-Version:MIME-Version:Message-ID:Message-ID:Reply-To:References:References:Sender;
bh=czwtYEhpEh3zoA3tQ6oGwCnIwb9+rTT0fcg9cte0jeY=;
b=MSmwYsnvv5G1GXIhOfYRib3YzC2yJrJP8ysKnkbgaRRGSV8D62pVrPvTK4jg2NLA
aC5pFkfRczdjExHaM3bjKtPHeIxZLRLDc6zFjjke3XGu89edU8IfZyUnzdEoyKrs2+j
4Wnqy9D8+l1TOJjq9OpZg47z4J1aAyfX2ufCDs2ySzNkHIy2uyRTAF84bGMbHEip7Kw
0hROF8fQxeFFmDn7gxK62Hyq2U9QpDcq+QBETsC+/c2Nz7PE50pdGs4m9jHN6f/xrid
lmWqJOotES9grkZX094vgWT6irVCMc5OyVXqSPMHoGi/1MgDNtiV8LqwT/DKeFZQ/xK
gSqQytxY2A==
In-Reply-To: <ND9prTQ--3-2@tutanota.com-ND9qgBV----2>
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
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: <ND9zJoA--7-2@tutanota.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>
 by: Jen Kris - Thu, 29 Sep 2022 20:47 UTC

To update my previous email, I found the problem, but I have a new problem. 

Previously I cast PyObject * value_ptr = (PyObject * )value_1 but that's not correct.  Instead I used PyObject * value_ptr = PyLong_FromLong(value_1) and that works.  HOWEVER, while PyObject_CallFunctionObjArgs does work now, it returns -1, which is not the right answer for random.seed..  I use "long return_val = PyLong_AsLong(p_seed_calc);" to convert it to a long. 

So my question is why do I get -1 as return value?  When I query p_seed calc : get:

(gdb) p p_seed_calc
$2 = (PyObject *) 0x7ffff69be120 <_Py_NoneStruct>

Thanks again.

Jen

Sep 29, 2022, 13:02 by python-list@python.org:

> Thanks very much to @MRAB for taking time to answer.  I changed my code to conform to your answer (as best I understand your comments on references), but I still get the same error.  My comments continue below the new code immediately below. 
>
> int64_t Get_LibModules(int64_t * return_array)
> {
> PyObject * pName_random = PyUnicode_FromString("random");
> PyObject * pMod_random = PyImport_Import(pName_random);
>
> Py_INCREF(pName_random);
> Py_INCREF(pMod_random);
>
> if (pMod_random == 0x0){
> PyErr_Print();
> return 1;}
>
> PyObject * pAttr_seed = PyObject_GetAttrString(pMod_random, "seed");
> PyObject * pAttr_randrange = PyObject_GetAttrString(pMod_random, "randrange");
>
> Py_INCREF(pAttr_seed);
> Py_INCREF(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 = (PyObject * )value_1;
> PyObject * p_seed_calc = PyObject_CallFunctionObjArgs(pAttr_seed, value_ptr, NULL);
>
> if (p_seed_calc == 0x0){
>     PyErr_Print();
>     return 1;}
>
> //Prepare return values
> long return_val = PyLong_AsLong(p_seed_calc);
>
> return return_val;
> }
>
> So I incremented the reference to all objects in Get_LibModules, but I still get the same segfault at PyObject_CallFunctionObjArgs.  Unfortunately, reference counting is not well documented so I’m not clear what’s wrong. 
>
>
>
>
> Sep 29, 2022, 10:06 by python@mrabarnett.plus.com:
>
>> On 2022-09-29 16:54, Jen Kris via Python-list wrote:
>>
>>> Recently I completed a project where I used PyObject_CallFunctionObjArgs extensively with the NLTK library from a program written in NASM, with no problems.  Now I am on a new project where I call the Python random library.  I use the same setup as before, but I am getting a segfault with random.seed.
>>>
>>> At the start of the NASM program I call a C API program that gets PyObject pointers to “seed” and “randrange” in the same way as I did before:
>>>
>>> int64_t Get_LibModules(int64_t * return_array)
>>> {
>>> PyObject * pName_random = PyUnicode_FromString("random");
>>> PyObject * pMod_random = PyImport_Import(pName_random);
>>>
>> Both PyUnicode_FromString and PyImport_Import return new references or null pointers.
>>
>>> if (pMod_random == 0x0){
>>> PyErr_Print();
>>>
>>
>> You're leaking a reference here (pName_random).
>>
>>> return 1;}
>>>
>>> PyObject * pAttr_seed = PyObject_GetAttrString(pMod_random, "seed");
>>> PyObject * pAttr_randrange = PyObject_GetAttrString(pMod_random, "randrange");
>>>
>>> return_array[0] = (int64_t)pAttr_seed;
>>> return_array[1] = (int64_t)pAttr_randrange;
>>>
>>
>> You're leaking 2 references here (pName_random and pMod_random).
>>
>>> return 0;
>>> }
>>>
>>> Later in the same program I call a C API program to call random.seed:
>>>
>>> int64_t C_API_2(PyObject * pAttr_seed, Py_ssize_t value_1)
>>> {
>>> PyObject * p_seed_calc = PyObject_CallFunctionObjArgs(pAttr_seed, value_1);
>>>
>>
>> It's expecting all of the arguments to be PyObject*, but value_1 is Py_ssize_t instead of PyObject* (a pointer to a _Python_ int).
>>
>> The argument list must end with a null pointer.
>>
>> It returns a new reference or a null pointer.
>>
>>>
>>> if (p_seed_calc == 0x0){
>>>     PyErr_Print();
>>>     return 1;}
>>>
>>> //Prepare return values
>>> long return_val = PyLong_AsLong(p_seed_calc);
>>>
>> You're leaking a reference here (p_seed_calc).
>>
>>> return return_val;
>>> }
>>>
>>> The first program correctly imports “random” and gets pointers to “seed” and “randrange.”  I verified that the same pointer is correctly passed into C_API_2, and the seed value (1234) is passed as  Py_ssize_t value_1.  But I get this segfault:
>>>
>>> Program received signal SIGSEGV, Segmentation fault.
>>> 0x00007ffff64858d5 in _Py_INCREF (op=0x4d2) at ../Include/object.h:459
>>> 459     ../Include/object.h: No such file or directory.
>>>
>>> So I tried Py_INCREF in the first program:
>>>
>>> Py_INCREF(pMod_random);
>>> Py_INCREF(pAttr_seed);
>>>
>>> Then I moved Py_INCREF(pAttr_seed) to the second program.  Same segfault.
>>>
>>> Finally, I initialized “random” and “seed” in the second program, where they are used.  Same segfault.
>>>
>>> The segfault refers to Py_INCREF, so this seems to do with reference counting, but Py_INCREF didn’t solve it.
>>>
>>> I’m using Python 3.8 on Ubuntu.
>>>
>>> Thanks for any ideas on how to solve this.
>>>
>>> Jen
>>>
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>


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

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor