Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyArg_ParseTuple and a 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 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..)

like image 382
Paul Avatar asked Nov 15 '22 08:11

Paul


1 Answers

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.

like image 135
John Machin Avatar answered Nov 17 '22 05:11

John Machin