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

Re: C-API PyObject_Call

2010-03-20 Thread Stefan Behnel
moerchendiser2k3, 20.03.2010 03:01: Yes, the user is able to set a file which contains a function that does what the user wants. > But in a case, I expect a special return value of this function. Ah, ok, that was the important piece of information that you omitted from your previous posts. So

Re: C-API PyObject_Call

2010-03-19 Thread moerchendiser2k3
Yes, the user is able to set a file which contains a function that does what the user wants. But in a case, I expect a special return value of this function. So, I need to tell him, in which file/line the error occured, otherwise he dont know where to look. Imagine he set 20 files, and all of the

Re: C-API PyObject_Call

2010-03-19 Thread Stefan Behnel
moerchendiser2k3, 19.03.2010 14:12: In my case I call a funcion and I would like to get the line where the function returned. for instance: def my_function(): return 3 So I would like to get line 2(in C) somehow. Ok, so you're looking for the C-level trace function in CPython, I guess.

Re: C-API PyObject_Call

2010-03-19 Thread moerchendiser2k3
In my case I call a funcion and I would like to get the line where the function returned. for instance: def my_function(): return 3 So I would like to get line 2(in C) somehow. -- http://mail.python.org/mailman/listinfo/python-list

Re: C-API PyObject_Call

2010-03-18 Thread Stefan Behnel
moerchendiser2k3, 18.03.2010 14:58: Funny that you (being the OP) ask *me* what kind of line information *you* want. Stefan Well, I need the line/file information of Python : Then please explain what kind of Python line number you expect to find in C code. Hint: providing details often re

Re: C-API PyObject_Call

2010-03-18 Thread moerchendiser2k3
> Funny that you (being the OP) ask *me* what kind of line information *you* > want. > > Stefan Well, I need the line/file information of Python : -- http://mail.python.org/mailman/listinfo/python-list

Re: C-API PyObject_Call

2010-03-18 Thread Stefan Behnel
moerchendiser2k3, 17.03.2010 23:35: 1) put the line number information into the message string when you raise the exception you mean the line and file information of the C code, right? Funny that you (being the OP) ask *me* what kind of line information *you* want. Stefan -- http://mail.p

Re: C-API PyObject_Call

2010-03-17 Thread moerchendiser2k3
> 1) put the line number information into the message string when you raise > the exception you mean the line and file information of the C code, right? -- http://mail.python.org/mailman/listinfo/python-list

Re: C-API PyObject_Call

2010-03-16 Thread Stefan Behnel
moerchendiser2k3, 16.03.2010 19:25: Hi, currently I am not at home, I will post some stuff when I am back. Just the note: I throw an exception with the C API. Looks like that PyObject *result = PyObject_Call(my_isntance, "", NULL); if(result==NULL) { PyErr_Print(); //when this happens, th

Re: C-API PyObject_Call

2010-03-16 Thread moerchendiser2k3
At first thanks for your answers!!! On 16 Mrz., 21:16, Carl Banks wrote: > Here you raise an exception with a C statement, > and catch and print it in the very next line.  The exception doesn't > exit from Python code so there are no lines to print. Exactly, I dont expect any line/file informati

Re: C-API PyObject_Call

2010-03-16 Thread Carl Banks
On Mar 16, 11:25 am, moerchendiser2k3 wrote: > Hi, currently I am not at home, I will post some stuff when I am back. > Just the note: I throw an exception with the C API. > > Looks like that > > PyObject *result = PyObject_Call(my_isntance, "", NULL); > if(result==NULL) > { >     PyErr_Print(); /

Re: C-API PyObject_Call

2010-03-16 Thread moerchendiser2k3
Hi, currently I am not at home, I will post some stuff when I am back. Just the note: I throw an exception with the C API. Looks like that PyObject *result = PyObject_Call(my_isntance, "", NULL); if(result==NULL) { PyErr_Print(); //when this happens, the traceback is correct with information

Re: C-API PyObject_Call

2010-03-16 Thread Steve Holden
moerchendiser2k3 wrote: > In one case I have to check the return value of PyObject_Call, and if > its not of the correct return value, > I throw an exception, but I just get a simple output: > > TypeError: Expected an instance of XYZ, no int. > > instead of > > Traceback (most called...) > TypeE

Re: C-API PyObject_Call

2010-03-16 Thread moerchendiser2k3
In one case I have to check the return value of PyObject_Call, and if its not of the correct return value, I throw an exception, but I just get a simple output: TypeError: Expected an instance of XYZ, no int. instead of Traceback (most called...) TypeError: in line 3, file test.py: expected an i

Re: C-API PyObject_Call

2010-03-16 Thread Stefan Behnel
moerchendiser2k3, 16.03.2010 12:52: i have a serious problem and I am looking for a solution. I pass an instance of a class from a file to PyObject_Call. When something fails, I get a full traceback. If it succeeds, I get the return value. Is it possible to get information from which line/file t

Re: C-API PyObject_Call

2010-03-16 Thread Stefan Behnel
moerchendiser2k3, 16.03.2010 17:08: But the stack is empty after PyObject_Call, isnt it? I think Steve was expecting that you wanted to debug into your program, step into the call, and find the line yourself. Stefan -- http://mail.python.org/mailman/listinfo/python-list

Re: C-API PyObject_Call

2010-03-16 Thread moerchendiser2k3
But the stack is empty after PyObject_Call, isnt it? -- http://mail.python.org/mailman/listinfo/python-list

Re: C-API PyObject_Call

2010-03-16 Thread Steve Holden
moerchendiser2k3 wrote: > Hi, > > i have a serious problem and I am looking for a solution. I pass an > instance of a class from a file to PyObject_Call. When something > fails, I get a full traceback. If it succeeds, I get the return value. > > Is it possible to get information from which line/f

C-API PyObject_Call

2010-03-16 Thread moerchendiser2k3
Hi, i have a serious problem and I am looking for a solution. I pass an instance of a class from a file to PyObject_Call. When something fails, I get a full traceback. If it succeeds, I get the return value. Is it possible to get information from which line/file the return value of PyObject_Call