Re: PyObject_CallFunctionObjArgs segfaults

2022-09-30 Thread Greg Ewing
On 1/10/22 8:18 am, MRAB wrote: It's OK to INCREF them, provided that you DECREF them when you no longer need them, and remember that if it's a "new reference" you'd need to DECREF it twice. Which means there would usually be no point in doing the extra INCREF/DECREF. You still need to know wh

Re: PyObject_CallFunctionObjArgs segfaults

2022-09-30 Thread Jen Kris via Python-list
That's great.  It clarifies things a lot for me, particularly re ref count for new references.  I would have had trouble if I didn't decref it twice.  Thanks very much once again.  Sep 30, 2022, 12:18 by pyt...@mrabarnett.plus.com: > On 2022-09-30 17:02, Jen Kris wrote: > >> >> Thanks very

Re: PyObject_CallFunctionObjArgs segfaults

2022-09-30 Thread MRAB
On 2022-09-30 17:02, Jen Kris wrote: Thanks very much for your detailed reply.  I have a few followup questions. You said, “Some functions return an object that has already been incref'ed ("new reference"). This occurs when it has either created a new object (the refcount will be 1) or has

Re: PyObject_CallFunctionObjArgs segfaults

2022-09-30 Thread Jen Kris via Python-list
Thanks very much for your detailed reply.  I have a few followup questions.  You said, “Some functions return an object that has already been incref'ed ("new reference"). This occurs when it has either created a new object (the refcount will be 1) or has returned a pointer to an existing objec

Re: PyObject_CallFunctionObjArgs segfaults

2022-09-29 Thread MRAB
On 2022-09-30 01:02, MRAB wrote: On 2022-09-29 23:41, Jen Kris wrote: I just solved this C API problem, and I’m posting the answer to help anyone else who might need it. [snip] What I like to do is write comments that state which variables hold a reference, followed by '+' if it's a new r

Re: PyObject_CallFunctionObjArgs segfaults

2022-09-29 Thread MRAB
On 2022-09-29 23:41, Jen Kris wrote: I just solved this C API problem, and I’m posting the answer to help anyone else who might need it. The errors were: (1) we must call Py_INCREF on each object when it’s created. Some functions return an object that has already been incref'ed ("new refer

Re: PyObject_CallFunctionObjArgs segfaults

2022-09-29 Thread Jen Kris via Python-list
I just solved this C API problem, and I’m posting the answer to help anyone else who might need it.  The errors were: (1) we must call Py_INCREF on each object when it’s created. (2) in C_API_2 (see below) we don’t cast value_1 as I did before with PyObject * value_ptr = (PyObject * )value_1

Re: PyObject_CallFunctionObjArgs segfaults

2022-09-29 Thread MRAB
On 2022-09-29 21:47, Jen Kris wrote: 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, whil

Re: PyObject_CallFunctionObjArgs segfaults

2022-09-29 Thread Jen Kris via Python-list
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 wo

Re: PyObject_CallFunctionObjArgs segfaults

2022-09-29 Thread Jen Kris via Python-list
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) { Py

Re: PyObject_CallFunctionObjArgs segfaults

2022-09-29 Thread MRAB
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 s

PyObject_CallFunctionObjArgs segfaults

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