Re: Debugging Python C extensions with GDB
> On 14 Nov 2022, at 23:44, Jen Kris wrote: > > > Thanks for your reply. Victor's article didn't mention ctypes extensions, so > I wanted to post a question before I build from source. Gdb works on any program its not special to python. Victor is only talking about a specific use of gdb for python c extensions. Barry > > > Nov 14, 2022, 14:32 by ba...@barrys-emacs.org: > > On 14 Nov 2022, at 19:10, Jen Kris via Python-list > wrote: > > In September 2021, Victor Stinner wrote “Debugging Python C extensions with > GDB” > (https://developers.redhat.com/articles/2021/09/08/debugging-python-c-extensions-gdb#getting_started_with_python_3_9). > > > My question is: with Python 3.9+, can I debug into a C extension written in > pure C and called from ctypes -- that is not written using the C_API? > > Yes. > > Just put a breakpoint on the function in the c library that you want to debug. > You can set the breakpoint before a .so is loaded. > > Barry > > Thanks. > > Jen > > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Debugging Python C extensions with GDB
Thanks for your reply. Victor's article didn't mention ctypes extensions, so I wanted to post a question before I build from source. Nov 14, 2022, 14:32 by ba...@barrys-emacs.org: > > >> On 14 Nov 2022, at 19:10, Jen Kris via Python-list >> wrote: >> >> In September 2021, Victor Stinner wrote “Debugging Python C extensions with >> GDB” >> (https://developers.redhat.com/articles/2021/09/08/debugging-python-c-extensions-gdb#getting_started_with_python_3_9). >> >> >> My question is: with Python 3.9+, can I debug into a C extension written in >> pure C and called from ctypes -- that is not written using the C_API? >> > > Yes. > > Just put a breakpoint on the function in the c library that you want to debug. > You can set the breakpoint before a .so is loaded. > > Barry > >> >> Thanks. >> >> Jen >> >> >> >> -- >> https://mail.python.org/mailman/listinfo/python-list >> -- https://mail.python.org/mailman/listinfo/python-list
Re: Debugging Python C extensions with GDB
> On 14 Nov 2022, at 19:10, Jen Kris via Python-list > wrote: > > In September 2021, Victor Stinner wrote “Debugging Python C extensions with > GDB” > (https://developers.redhat.com/articles/2021/09/08/debugging-python-c-extensions-gdb#getting_started_with_python_3_9). > > > My question is: with Python 3.9+, can I debug into a C extension written in > pure C and called from ctypes -- that is not written using the C_API? Yes. Just put a breakpoint on the function in the c library that you want to debug. You can set the breakpoint before a .so is loaded. Barry > > Thanks. > > Jen > > > > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Debugging Python C extensions with GDB
In September 2021, Victor Stinner wrote “Debugging Python C extensions with GDB” (https://developers.redhat.com/articles/2021/09/08/debugging-python-c-extensions-gdb#getting_started_with_python_3_9). My question is: with Python 3.9+, can I debug into a C extension written in pure C and called from ctypes -- that is not written using the C_API? Thanks. Jen -- https://mail.python.org/mailman/listinfo/python-list
Python C Extensions
Hi, I have a memory leak problem with my C extension module. My C module returns large dictionaries to python, and the dictionaries never get deleted, so the memory for my program keeps growing. I do not know how to delete the dictionary object after it becomes irrelevant. I do not know if the version of python is relevant, I'm using the 2.5 ! Here is the C code: PyObject *GetDictionary(PyObject *self, PyObject *args) { PyObject *dict = PyDict_New(); PyObject *key; PyObject *value; char name[128]; for(int i = 0; i 60; i++) { sprintf(name,v%d,i); float number = 1.0 * 0.5*i; PyDict_SetItem(dict,Py_BuildValue(s,name),Py_BuildValue(f,number)); } return dict; } And here is the Code that I use in a loop, which causes the program memory to grow: import libpyTestModule as pyTEST bankTEST = {} for j in range(1,10): for k in range(1,10): bankTEST = pyTEST.GetDictionary() del bankTEST Any help will be appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python C Extensions
On 24/02/2011 16:01, aken8...@yahoo.com wrote: Hi, I have a memory leak problem with my C extension module. My C module returns large dictionaries to python, and the dictionaries never get deleted, so the memory for my program keeps growing. I do not know how to delete the dictionary object after it becomes irrelevant. I do not know if the version of python is relevant, I'm using the 2.5 ! Here is the C code: PyObject *GetDictionary(PyObject *self, PyObject *args) { PyObject *dict = PyDict_New(); PyObject *key; PyObject *value; char name[128]; for(int i = 0; i 60; i++) { sprintf(name,v%d,i); float number = 1.0 * 0.5*i; PyDict_SetItem(dict,Py_BuildValue(s,name),Py_BuildValue(f,number)); } return dict; } And here is the Code that I use in a loop, which causes the program memory to grow: import libpyTestModule as pyTEST bankTEST = {} for j in range(1,10): for k in range(1,10): bankTEST = pyTEST.GetDictionary() del bankTEST Any help will be appreciated. Py_BuildValue(...) returns an object with its refcount set to 1. PyDict_SetItem(...) increments the refcounts of the key and value objects when they are added to the dict, so their refcounts will then be 2. When the dict is garbage-collected the refcouts of the key and value objects will be decremented to 1, so they won't be collected, and as there aren't any other references to them, leading to a memory leak. You therefore need to decrement the refcounts of the key and value objects after adding them to the dict: PyObject *key = Py_BuildValue(s, name); PyObject *value = Py_BuildValue(f, number); PyDict_SetItem(dict, key, value); Py_DECREF(key); Py_DECREF(value); -- http://mail.python.org/mailman/listinfo/python-list
Re: Python C Extensions
Thank you very much, it worked. I thought the PyDict_SetItem should assume ownership of the passed object and decrease it's reference count (I do not know why). Does this also go for the Lists ? Should anything inserted into list also be DECRED-ed ? Thank you again for reply. On Feb 24, 11:33 am, MRAB pyt...@mrabarnett.plus.com wrote: On 24/02/2011 16:01, aken8...@yahoo.com wrote: Hi, I have a memory leak problem with my C extension module. My C module returns large dictionaries to python, and the dictionaries never get deleted, so the memory for my program keeps growing. I do not know how to delete the dictionary object after it becomes irrelevant. I do not know if the version of python is relevant, I'm using the 2.5 ! Here is the C code: PyObject *GetDictionary(PyObject *self, PyObject *args) { PyObject *dict = PyDict_New(); PyObject *key; PyObject *value; char name[128]; for(int i = 0; i 60; i++) { sprintf(name,v%d,i); float number = 1.0 * 0.5*i; PyDict_SetItem(dict,Py_BuildValue(s,name),Py_BuildValue(f,number)); } return dict; } And here is the Code that I use in a loop, which causes the program memory to grow: import libpyTestModule as pyTEST bankTEST = {} for j in range(1,10): for k in range(1,10): bankTEST = pyTEST.GetDictionary() del bankTEST Any help will be appreciated. Py_BuildValue(...) returns an object with its refcount set to 1. PyDict_SetItem(...) increments the refcounts of the key and value objects when they are added to the dict, so their refcounts will then be 2. When the dict is garbage-collected the refcouts of the key and value objects will be decremented to 1, so they won't be collected, and as there aren't any other references to them, leading to a memory leak. You therefore need to decrement the refcounts of the key and value objects after adding them to the dict: PyObject *key = Py_BuildValue(s, name); PyObject *value = Py_BuildValue(f, number); PyDict_SetItem(dict, key, value); Py_DECREF(key); Py_DECREF(value); -- http://mail.python.org/mailman/listinfo/python-list
Re: Python C Extensions
On Feb 24, 8:46 am, aken8...@yahoo.com aken8...@yahoo.com wrote: Thank you very much, it worked. I thought the PyDict_SetItem should assume ownership of the passed object and decrease it's reference count (I do not know why). Does this also go for the Lists ? Should anything inserted into list also be DECRED-ed ? The Python C API documentation has this information--if a function is documented as borrowing a reference, then it behaves as you were expecting (it doesn't increase the reference count). If it's documented as creating a new reference, it does increase the reference count. I don't know if there's a simple rule to know of a function borrows or creates a new reference; I've never noticed one. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Python C Extensions
On 24/02/2011 16:46, aken8...@yahoo.com wrote: Thank you very much, it worked. I thought the PyDict_SetItem should assume ownership of the passed object and decrease it's reference count (I do not know why). Does this also go for the Lists ? Should anything inserted into list also be DECRED-ed ? Thank you again for reply. [snip] The pattern is that calls which create an object will return that object with a refcount of 1, and calls which 'store' an object, for example, PyDict_SetItem(...) and PyList_Append(...) will increment the refcount of the stored object to ensure that it won't be garbage collected. When in doubt, try stepping through the code in a debugger. You'll see that storing an object will cause its refcount to be incremented. -- http://mail.python.org/mailman/listinfo/python-list
Re: How run valgrind on Python C extensions?
Chris I have Python C extensions that are giving me seg faults that I'd Chris like to run valgrind on. Chris Can I use valgrind on these through python?? HOW??? Just run Python under valgrind's control. It will automatically take care of it for you. Chris Is it easy or must I do some work like recompiling python source Chris with the -g extension? Should not be necessary. Skip -- http://mail.python.org/mailman/listinfo/python-list
How run valgrind on Python C extensions?
I have Python C extensions that are giving me seg faults that I'd like to run valgrind on. Can I use valgrind on these through python?? HOW??? Is it easy or must I do some work like recompiling python source with the -g extension? Thanks! Chris -- http://mail.python.org/mailman/listinfo/python-list