views:

40

answers:

2

I have code like the following:

    PyObject *callback;
    PyObject *paths;

    // Process and convert arguments
    if (!PyArg_ParseTuple(args, "OO:schedule", &paths, &callback))
            return NULL;

What exactly happens inside PyArg_ParseTuple? My guess is that callback gets the function pointer I passed to args (also PyObject*). How does PyArg_ParseTuple convert the function pointer to PyObject*?

What I want to know is what happens if I pass in the same callback function pointer twice. I think callback gets allocated a new PyObject inside PyArg_ParseTuple, so it will get a different memory address each time, but will contain the same callback function pointer.

But if I PyObject_Hash callback, it will produce a different value each time, right? (since address is different each time..)

A: 

The point is that if you pass the same callback twice it will receive two objects but you will never be allowed to read only one which is the las wrote. You will have a kind of memory leak as one of the two pointers will not be referenced. Of course the garbage collector will eventually pass after you to clean all the mess. But anyhow...

I misread the PyObject_Hash should be called on callback and paths. It will be the same. but you probably want to compare callback and paths: if(callback==paths) {printf("it 's the same callabck");}

Xavier Combelle
What I want is to be able to know that it's the same callback function. How do I do that?
Paul
+1  A: 

PyArg_ParseTuple doesn't care about the type of an "O" arg. No conversion is done. No new object is created. The address of the object is dropped into the PyObject * C variable that you have specified. It does exactly the same to each of your two args.

I can't imagine what is the relevance of PyObject_Hash. If you want to compare two incarnations of your callback arg, just use == on the addresses.

John Machin