Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

UNIX enhancements aren't.


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

SubjectAuthor
o Re: C API PyObject_Call segfaults with stringInada Naoki

1
Re: C API PyObject_Call segfaults with string

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: songofac...@gmail.com (Inada Naoki)
Newsgroups: comp.lang.python
Subject: Re: C API PyObject_Call segfaults with string
Date: Thu, 10 Feb 2022 10:23:47 +0900
Lines: 116
Message-ID: <mailman.78.1644456255.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>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de 6F4syXfzfzJjyJVf8z4w7Qu3tr7AEP1Q015l8h7FQUBQ==
Return-Path: <songofacandy@gmail.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=gmail.com header.i=@gmail.com header.b=C+6eVMe5;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '2022': 0.05; 'string':
0.07; 'subject:API': 0.07; 'cc:addr:python-list': 0.09;
'segmentation': 0.09; 'skip:_ 20': 0.09; 'subject:skip:P 10':
0.09; 'url:mailman': 0.15; '(because': 0.16; '2022,': 0.16; '3.9':
0.16; 'api.': 0.16; 'are.': 0.16; 'cc:name:python list': 0.16;
'constructed': 0.16; 'doc': 0.16; 'examples.': 0.16; 'inada':
0.16; 'naoki': 0.16; 'received:209.85.221.51': 0.16; 'received
:mail-wr1-f51.google.com': 0.16; 'str.': 0.16; 'subject:string':
0.16; 'tuple': 0.16; 'unicode': 0.16; 'wrote:': 0.16; 'problem':
0.16; 'python': 0.16; 'feb': 0.17; 'thu,': 0.19;
'cc:addr:python.org': 0.20; 'returns': 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; 'tried': 0.26; 'object': 0.26; 'function':
0.27; 'url-ip:188/8': 0.31; 'program': 0.31; "doesn't": 0.32;
'guess': 0.32; 'python-list': 0.32; 'signal': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; "i'm": 0.33; '0);': 0.33;
'same': 0.34; 'header:In-Reply-To:1': 0.34; 'received:google.com':
0.34; 'yes,': 0.35; 'final': 0.35; 'from:addr:gmail.com': 0.35;
'url:edu': 0.36; 'using': 0.37; 'received:209.85': 0.37; 'read':
0.38; 'thanks': 0.38; 'received:209': 0.39; 'two': 0.39; 'use':
0.39; 'should': 0.40; '10,': 0.61; 'here.': 0.61; 'format': 0.62;
'everything': 0.63; 'down': 0.64; 'finished': 0.64; 'your': 0.64;
'accept': 0.67; 'right': 0.68; 'eliminated': 0.84; 'pointer':
0.84; 'sentences': 0.84; 'url:people': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to
:cc:content-transfer-encoding;
bh=Eu08ZIkcsq6s+dKcTcIBZrA0HpKi2vFK1+5EGXRRfzk=;
b=C+6eVMe56/NsxMZU7JhzDyMI9BvSbGZsLScQqyi0zfG9PynLke0yFJAD8FZIe2rn7b
LLzqk7JyZCjoMnPqjS2mSxCXxIC39Y5ar1JzEVxtlIdWHXXN1uY/zxQBo0cAbqMhy9aj
7kql4t57kzrQsoimcJoHg6orCjTlrZO+8VhSKNyYEcyp3HKPz8mc46ULp5lZgsu99dmK
2THfYTr1nNB897ZA3msVFkSGzxDW+mEV47HIEHYqjTPDNVg4A3TxfQe5GZQDmG+8e3Lf
POfDVGL+MOCUshsrHHTL2wRx29rzTKQiQhnvcq1i6h/9+n2/MROF0v1ETnWHRlUjpOKi
ERSA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to:cc:content-transfer-encoding;
bh=Eu08ZIkcsq6s+dKcTcIBZrA0HpKi2vFK1+5EGXRRfzk=;
b=IocwlM8szV43tNBDmdz4ydE74RLD86yJX9VCOFPxICUiFO2ncsFB5v984+hVgsWtdP
12+0ewiR+ATUV+5XpjSO4q4k7mDxafKAwwlRDeJg5o2GsQtn9fZKD9LFDGPRH+20zoZw
VhSduaVqBiwN7YnO2DKRU91MM9P5jA+XnbJctD3Jzo7mS7jLa5MlKjIFEpJoFf5CppOx
AwlKH3VCB7FKcYCU9IJD9uLOtriLvTPg6Asd6XM0VWZ4jgbjbSDMz3yb9XoMHnb+/K7i
7OAV5MB9+kX+1wBFX247FMR5e1s8V1rSlViLGXLpRbDT0MlJG3MA3CJJgSGTqe88VY6/
L2yA==
X-Gm-Message-State: AOAM530CVnaQKT7vWcYsPHP6iCkxq8/pir7hh/gurfVyzjD0q1GarLRX
0VDMiuYqmkSOkTdYaVx6lx4PCZDMkVPetk5RxDpxoO+tt5Q=
X-Google-Smtp-Source: ABdhPJznj/2cFDR32iCrJD5pc55UabQObaUsAPQsu9R29yEzVYnQmaovaBnLguBvvwBjR2GAb1MfmaMuhVliE+vvLJs=
X-Received: by 2002:a5d:6850:: with SMTP id o16mr4244492wrw.344.1644456254044;
Wed, 09 Feb 2022 17:24:14 -0800 (PST)
In-Reply-To: <MvWBjNR--3-2@tutanota.com>
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: <CAEfz+Ty3HGXymXt2VMAeVXzR7rw14HPu6rBrrARLV1UGXQEE=w@mail.gmail.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>
 by: Inada Naoki - Thu, 10 Feb 2022 01:23 UTC

// https://docs.python.org/3/c-api/call.html#c.PyObject_CallNoArgs
// This function is only for one arg. Python >= 3.9 is required.
pSents = PyObject_CallOneArg(pSentMod, pListItem);

Or

// https://docs.python.org/3/c-api/call.html#c.PyObject_CallFunctionObjArgs
// This function can call function with multiple arguments. Can be
used with Python <3.9 too.
pSents = PyObject_CallFunctionObjArgs(pSentMod, pListItem);

On Thu, Feb 10, 2022 at 10:15 AM Jen Kris <jenkris@tutanota.com> wrote:
>
> Right you are. In that case should I use Py_BuildValue and convert to tuple (because it won't return a tuple for a one-arg), or should I just convert pListStr to tuple? Thanks for your help.
>
>
> Feb 9, 2022, 17:08 by songofacandy@gmail.com:
>
> On Thu, Feb 10, 2022 at 10:05 AM Jen Kris <jenkris@tutanota.com> wrote:
>
>
> Thanks for your reply.
>
> I eliminated the DECREF and now it doesn't segfault but it returns 0x0. Same when I substitute pListStrE for pListStr. pListStr contains the string representation of the fileid, so it seemed like the one to use. According to http://web.mit.edu/people/amliu/vrut/python/ext/buildValue.html, PyBuildValue "builds a tuple only if its format string contains two or more format units" and that doc contains examples.
>
>
> Yes, and PyObject_Call accept tuple, not str.
>
>
> https://docs.python.org/3/c-api/call.html#c.PyObject_Call
>
>
> Feb 9, 2022, 16:52 by songofacandy@gmail.com:
>
> On Thu, Feb 10, 2022 at 9:42 AM Jen Kris via Python-list
> <python-list@python.org> wrote:
>
>
> I have everything finished down to the last line (sentences = gutenberg..sents(fileid)) where I use PyObject_Call to call gutenberg.sents, but it segfaults. The fileid is a string -- the first fileid in this corpus is "austen-emma.txt."
>
> pName = PyUnicode_FromString("nltk.corpus");
> pModule = PyImport_Import(pName);
>
> pSubMod = PyObject_GetAttrString(pModule, "gutenberg");
> pFidMod = PyObject_GetAttrString(pSubMod, "fileids");
> pSentMod = PyObject_GetAttrString(pSubMod, "sents");
>
> pFileIds = PyObject_CallObject(pFidMod, 0);
> pListItem = PyList_GetItem(pFileIds, listIndex);
> pListStrE = PyUnicode_AsEncodedString(pListItem, "UTF-8", "strict");
> pListStr = PyBytes_AS_STRING(pListStrE);
> Py_DECREF(pListStrE);
>
>
> HERE.
> PyBytes_AS_STRING() returns pointer in the pListStrE Object.
> So Py_DECREF(pListStrE) makes pListStr a dangling pointer.
>
>
> // sentences = gutenberg.sents(fileid)
> PyObject *c_args = Py_BuildValue("s", pListStr);
>
>
> Why do you encode&decode pListStrE?
> Why don't you use just pListStrE?
>
> PyObject *NullPtr = 0;
> pSents = PyObject_Call(pSentMod, c_args, NullPtr);
>
>
> c_args must tuple, but you passed a unicode object here.
> Read https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue
>
> The final line segfaults:
> Program received signal SIGSEGV, Segmentation fault.
> 0x00007ffff6e4e8d5 in _PyEval_EvalCodeWithName ()
> from /usr/lib/x86_64-linux-gnu/libpython3.8.so.1.0
>
> My guess is the problem is in Py_BuildValue, which returns a pointer but it may not be constructed correctly. I also tried it with "O" and it doesn't segfault but it returns 0x0.
>
> I'm new to using the C API. Thanks for any help.
>
> Jen
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
>
> Bests,
>
> --
> Inada Naoki <songofacandy@gmail.com>
>
>
>
> --
> Inada Naoki <songofacandy@gmail.com>
>
>

--
Inada Naoki <songofacandy@gmail.com>

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor