LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Extending Python with C (reference counting questions) (https://www.linuxquestions.org/questions/programming-9/extending-python-with-c-reference-counting-questions-656834/)

spacepigeon 07-19-2008 09:27 AM

Extending Python with C (reference counting questions)
 
Hi there,

I have been using Python for a while and recently have started to extend with C modules. I am a little confused about reference counting though. Suppose we have the following:
Code:

static PyObject *SomeFunction(PyObject *self, PyObject *args){
         
          PyObject *storage_for_python_list;

        if(!PyArg_ParseTuple(args, "O", &storage_for_python_list))
            return NULL;

        ...do something with list
       
        Py_INCREF(Py_None);
          return Py_None;

do we need to Py_DECREF(storage_for_python_list)??

Second, is doing the following a bad idea?
Code:

Py_Object *list;
list= PyList_New(n);
PyList_SetItem(list, index, PyFloat_FromDouble(some_double_value));

I'm not sure what happens to the reference created from PyFloat_FromDouble in this case.

Thanks in advance for any suggestions/feedback.


All times are GMT -5. The time now is 11:41 AM.