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*?
PyArg_ParseTuple中到底发生了什么?我的猜测是回调函数得到了传递给args的函数指针(同样是PyObject*)。PyArg_ParseTuple如何将函数指针转换为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.
我想知道的是如果我传入相同的回调函数指针两次会发生什么。我认为回调会在PyArg_ParseTuple中分配一个新的PyObject,因此每次都会得到不同的内存地址,但是会包含相同的回调函数指针。
But if I PyObject_Hash callback, it will produce a different value each time, right? (since address is different each time..)
但是如果我使用PyObject_Hash回调,每次都会产生不同的值,对吧?(因为地址每次都不一样。)
2 个解决方案
#1
1
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.
PyArg_ParseTuple不关心“O”arg的类型。不做转换。没有创建新的对象。对象的地址被放入您指定的PyObject * C变量中。它对你的两个arg都是一样的。
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.
我无法想象PyObject_Hash的相关性是什么。如果您想比较回调arg的两种形式,只需在地址上使用== =。
#2
0
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...
关键是,如果您传递相同的回调两次,它将接收两个对象,但永远不允许您只读取一个对象,这是las所写的。您将有一种内存泄漏,因为这两个指针中的一个将不会被引用。当然垃圾回收器最终会通过你来清理所有的垃圾。但无论如何…
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");}
我误读了PyObject_Hash,应该调用回调和路径。还是一样的。但是,您可能想要比较回调和路径:if(callback==path) {printf(“它是同一个callabck”);}
#1
1
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.
PyArg_ParseTuple不关心“O”arg的类型。不做转换。没有创建新的对象。对象的地址被放入您指定的PyObject * C变量中。它对你的两个arg都是一样的。
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.
我无法想象PyObject_Hash的相关性是什么。如果您想比较回调arg的两种形式,只需在地址上使用== =。
#2
0
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...
关键是,如果您传递相同的回调两次,它将接收两个对象,但永远不允许您只读取一个对象,这是las所写的。您将有一种内存泄漏,因为这两个指针中的一个将不会被引用。当然垃圾回收器最终会通过你来清理所有的垃圾。但无论如何…
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");}
我误读了PyObject_Hash,应该调用回调和路径。还是一样的。但是,您可能想要比较回调和路径:if(callback==path) {printf(“它是同一个callabck”);}