Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"Imitation is the sincerest form of television." -- The New Mighty Mouse


devel / comp.lang.python / Re: C API PyObject_Call segfaults with string

SubjectAuthor
o Re: C API PyObject_Call segfaults with stringMRAB

1
Re: C API PyObject_Call segfaults with string

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

  copy mid

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

  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: C API PyObject_Call segfaults with string
Date: Thu, 10 Feb 2022 20:43:10 +0000
Lines: 72
Message-ID: <mailman.98.1644525978.7010.python-list@python.org>
References: <MvW3s4j--3-2@tutanota.com>
<CAEfz+TxXDn975BWxhG87O2GfVNpZj6tJ4vA-=ub44CcwHeY=hA@mail.gmail.com>
<MvW9RVE--3-2@tutanota.com>
<CAEfz+Ty7MhhLZugDgnEjrQX_UmN7sLUifbBPz3gtB73QFe5TWQ@mail.gmail.com>
<MvWBjNR--3-2@tutanota.com>
<CAEfz+Ty3HGXymXt2VMAeVXzR7rw14HPu6rBrrARLV1UGXQEE=w@mail.gmail.com>
<MvWGgaJ--B-2@tutanota.com>
<CAEfz+Tx4rpN6+fqrPMd-oeuNxitGPbGf5NYCqZCdAJa6htW1gQ@mail.gmail.com>
<Mv_DLCJ--3-2@tutanota.com>
<bf9c3200-f5b0-6ed5-5229-d4095021f6b4@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 Eg7jveDYIaSbvS6ZCYK41QTZmUBUEWfNOaLX31LQWtCw==
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=IQ3lqCnj;
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'traceback': 0.04; '(e.g.':
0.05; '2022': 0.05; '3.8': 0.05; 'string': 0.07; 'subject:API':
0.07; 'utf-8': 0.07; 'from:addr:python': 0.09;
'received:192.168.1.64': 0.09; 'subject:skip:P 10': 0.09; '2022,':
0.16; 'encoding': 0.16; 'from:addr:mrabarnett.plus.com': 0.16;
'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16;
'problem.\xc2\xa0': 0.16; 'received:84.93': 0.16;
'received:84.93.230': 0.16; 'received:84.93.230.227': 0.16;
'received:plus.net': 0.16; 'subject:string': 0.16; 'unicode':
0.16; 'yesterday,': 0.16; 'wrote:': 0.16; 'python': 0.16; 'feb':
0.17; 'thu,': 0.19; 'to:addr:python-list': 0.20; 'exception':
0.22; 'code': 0.23; 'skip:p 30': 0.23; 'tried': 0.26; '>>>': 0.28;
'asked': 0.29; 'error': 0.29; 'header:User-Agent:1': 0.30;
"doesn't": 0.32; 'python-list': 0.32; 'received:192.168.1': 0.32;
'but': 0.32; "i'm": 0.33; '0);': 0.33; 'work.': 0.34; 'header:In-
Reply-To:1': 0.34; 'item': 0.35; 'final': 0.35; 'those': 0.36;
'using': 0.37; 'this.': 0.37; 'received:192.168': 0.37; 'means':
0.38; '8bit%:14': 0.38; 'thanks': 0.38; 'enough': 0.39;
'mentioned': 0.39; 'valid': 0.39; 'use': 0.39; 'try': 0.40; '10,':
0.61; 'here': 0.62; 'your': 0.64; 'well': 0.65; 'sequence': 0.69;
'you.': 0.71; 'relevant': 0.73; 'happened.': 0.84; 'null)': 0.84;
'pointer': 0.84; 'pointing': 0.84; 'sentences': 0.84;
'\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=plus.com; s=042019;
t=1644525791; bh=g42jGNGj10J4y3QblVk5B6aWS3OoQmnv002q2aXhLcI=;
h=Date:Subject:To:References:From:In-Reply-To;
b=IQ3lqCnjfm9YGSV5JJJTSuKzosoISn/Iu5N+6FVuQJpOrWrywX9uuZqMqqHjH0xMk
ba/0J3dpDeeAYVviAFTpm+ABSa+GZeXA8O5sXmRnKHgIpoUf+ekbtdlpfHrJACmKZW
/26CvyJLBUIdXAjyoK8ZiBr2F1rESQZyg57eB5x2aQ/MWH5Y0LNCVZUmYIjagqHb4c
xREzTZWiEXP9cvx3qR5yKwSnzYoWcF/z/7r+OE/DOdrz08OqMnkrXZK5MTfb3nrryv
d3HZUaDbIsmXFZxq7uykY8iKSQztb1HlA6e+SLutzJv4jb+wlh1KswVNlW5d62nR3b
KHcHfJoDNpSMg==
X-Clacks-Overhead: "GNU Terry Pratchett"
X-CM-Score: 0.00
X-CNFS-Analysis: v=2.4 cv=MesPB7zf c=1 sm=1 tr=0 ts=620578df
a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17
a=IkcTkHD0fZMA:10 a=pGLkceISAAAA:8 a=mlNn8-T5AAAA:8 a=ZoM5UalTjkvtaWg_tE0A:9
a=QEXdDO2ut3YA:10 a=_4ER54FGBOWlvg8qisjz:22
X-AUTH: mrabarnett@:2500
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.5.1
Content-Language: en-GB
In-Reply-To: <Mv_DLCJ--3-2@tutanota.com>
X-CMAE-Envelope: MS4xfCCqAuK8jBRdcOll2odMy7ReTmLpBBudnSGimxG08xdD+apTrtShWgVzC9AVp1o2RTIa5yaMQvQWylLx6AgAt2r1Lmf9u38wYFLgRNnAa7Qpj1ovZAkc
aBDZLucnPt2r2XL83BgELO2MytGV1gPDEDP4azn05huEjzAyT7GXX+oUja3S5MqMb8dhIOJIdu1ehbVW7m/fFG0r9wd8vUmYIKQ=
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: <bf9c3200-f5b0-6ed5-5229-d4095021f6b4@mrabarnett.plus.com>
X-Mailman-Original-References: <MvW3s4j--3-2@tutanota.com>
<CAEfz+TxXDn975BWxhG87O2GfVNpZj6tJ4vA-=ub44CcwHeY=hA@mail.gmail.com>
<MvW9RVE--3-2@tutanota.com>
<CAEfz+Ty7MhhLZugDgnEjrQX_UmN7sLUifbBPz3gtB73QFe5TWQ@mail.gmail.com>
<MvWBjNR--3-2@tutanota.com>
<CAEfz+Ty3HGXymXt2VMAeVXzR7rw14HPu6rBrrARLV1UGXQEE=w@mail.gmail.com>
<MvWGgaJ--B-2@tutanota.com>
<CAEfz+Tx4rpN6+fqrPMd-oeuNxitGPbGf5NYCqZCdAJa6htW1gQ@mail.gmail.com>
<Mv_DLCJ--3-2@tutanota.com>
 by: MRAB - Thu, 10 Feb 2022 20:43 UTC

On 2022-02-10 20:00, Jen Kris via Python-list wrote:
> With the help of PyErr_Print() I have it solved.  Here is the final code (the part relevant to sents):
>
>    Py_ssize_t listIndex = 0;
>    pListItem = PyList_GetItem(pFileIds, listIndex);
>    pListStrE = PyUnicode_AsEncodedString(pListItem, "UTF-8", "strict");
>    pListStr = PyBytes_AS_STRING(pListStrE); // Borrowed pointer
>
>    // Then:  sentences = gutenberg.sents(fileid) - this is a sequence item
>    PyObject *c_args = Py_BuildValue("s", pListStr);
>    PyObject *args_tuple = PyTuple_New(1);
>    PyTuple_SetItem(args_tuple, 0, c_args);
>
>    pSents = PyObject_CallObject(pSentMod, args_tuple);
>
>    if ( pSents == 0x0){
>        PyErr_Print();
>        return return_value; }
>
> As you mentioned yesterday, CallObject needs a tuple, so that was the problem.  Now it works.
>
> You also asked why I don't just use pListStrE.  I tried that and got a long error message from PyErr_Print.  I'm not far enough along in my C_API work to understand why, but it doesn't work.
>
> Thanks very much for your help on this.
>
You're encoding a Unicode string to a UTF-8 bytestring:

pListStrE = PyUnicode_AsEncodedString(pListItem, "UTF-8", "strict");

then pointing to the bytes of that UTF-8 bytestring:

pListStr = PyBytes_AS_STRING(pListStrE); // Borrowed pointer

then making a Unicode string from those UTF-8 bytes:

PyObject *c_args = Py_BuildValue("s", pListStr);

You might was well just use the original Unicode string!

Try this instead:

Py_ssize_t listIndex = 0;
pListItem = PyList_GetItem(pFileIds, listIndex);
//> pListItem?

pSents = PyObject_CallFunctionObjArgs(pSentMod, pListItem, 0);
//> pSents+?

if (pSents == 0x0){
PyErr_Print();
return return_value;
}

>
>
> Feb 9, 2022, 17:40 by songofacandy@gmail.com:
>
>> On Thu, Feb 10, 2022 at 10:37 AM Jen Kris <jenkris@tutanota.com> wrote:
>>
>>>
>>> I'm using Python 3.8 so I tried your second choice:
>>>
>>> pSents = PyObject_CallFunctionObjArgs(pSentMod, pListItem);
>>>
>>> but pSents is 0x0. pSentMod and pListItem are valid pointers.
>>>
>>
>> It means exception happened.
>> If you are writing Python/C function, return NULL (e.g. `if (pSents ==
>> NULL) return NULL`)
>> Then Python show the exception and traceback for you.
>>

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor