PyArg_ParseTuple and callback function pointer

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 is going on inside PyArg_ParseTuple? I assume that the callback gets the function pointer that I passed to args (also PyObject *). How does PyArg_ParseTuple convert a function pointer to PyObject *?

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

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

+4
source share
2 answers

PyArg_ParseTuple does not care about the type of the argument "O". Conversion is not performed. A new object is not created. The address of the object is dropped into the PyObject * C variable you specified. He does the same for each of your two arguments.

I cannot imagine what PyObject_Hash is relevant PyObject_Hash . If you want to compare the two incarnations of your callback argument, just use == at the addresses.

+1
source

The fact is that if you pass the same callback twice, it will receive two objects, but you will never be allowed to read only one that is written in las. You will have some kind of memory leak, as one of the two pointers will not be referenced. Of course, the garbage collector will eventually come after you to clean up the whole mess. But anyway ...

I misunderstood that PyObject_Hash should be called for callback and paths. It will be one and the same. but you probably want to compare the callback and the paths: if (callback == paths) {printf ("this is the same callabck");}

0
source

All Articles