Re: C API PyObject_Call segfaults with string

2022-02-10 Thread Jen Kris via Python-list
Thank you for that suggestion.  It allowed me to replace six lines of code with one.  :) Feb 10, 2022, 12:43 by pyt...@mrabarnett.plus.com: > 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

Re: C API PyObject_Call segfaults with string

2022-02-10 Thread MRAB
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, "UT

Re: C API PyObject_Call segfaults with string

2022-02-10 Thread Jen Kris via Python-list
Hi and thanks very much for your comments on reference counting.  Since I'm new to the C_API that will help a lot.  I know that reference counting is one of the difficult issues with the C API.  I just posted a reply to Inada Naoki showing how I solved the problem I posted yesterday.  Thanks

Re: C API PyObject_Call segfaults with string

2022-02-10 Thread Jen Kris via Python-list
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

Re: C API PyObject_Call segfaults with string

2022-02-09 Thread MRAB
On 2022-02-10 01:37, Jen Kris via Python-list 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. 'PyObject_CallFunction' looks like a good one to use: """PyObjec

Re: C API PyObject_Call segfaults with string

2022-02-09 Thread Jen Kris via Python-list
I'll do that and post back tomorrow.  The office is closing and I have to leave now (I'm in Seattle).  Thanks again for your help.  Feb 9, 2022, 17:40 by songofaca...@gmail.com: > On Thu, Feb 10, 2022 at 10:37 AM Jen Kris wrote: > >> >> I'm using Python 3.8 so I tried your second choice: >> >

Re: C API PyObject_Call segfaults with string

2022-02-09 Thread Inada Naoki
On Thu, Feb 10, 2022 at 10:37 AM Jen Kris 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 fu

Re: C API PyObject_Call segfaults with string

2022-02-09 Thread Jen Kris via Python-list
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.  Feb 9, 2022, 17:23 by songofaca...@gmail.com: > // https://docs.python.org/3/c-api/call.html#c.PyObject_CallNoArgs >

Re: C API PyObject_Call segfaults with string

2022-02-09 Thread Inada Naoki
// 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

Re: C API PyObject_Call segfaults with string

2022-02-09 Thread Jen Kris via Python-list
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 songofaca...@gmail.com: > On Thu, Feb 10, 2022 at 10:05 AM Jen Kris wrote: >

Re: C API PyObject_Call segfaults with string

2022-02-09 Thread Inada Naoki
On Thu, Feb 10, 2022 at 10:05 AM Jen Kris 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

Re: C API PyObject_Call segfaults with string

2022-02-09 Thread Jen Kris via Python-list
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/pyth

Re: C API PyObject_Call segfaults with string

2022-02-09 Thread Inada Naoki
On Thu, Feb 10, 2022 at 9:42 AM Jen Kris via Python-list 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 "a

C API PyObject_Call segfaults with string

2022-02-09 Thread Jen Kris via Python-list
This is a follow-on to a question I asked yesterday, which was answered by MRAB.   I'm using the Python C API to load the Gutenberg corpus from the nltk library and iterate through the sentences.  The Python code I am trying to replicate is: from nltk.corpus import gutenberg for i, fileid in en