PyArg_ParseTuple 和回调函数指针
我有如下代码:
PyObject *callback;
PyObject *paths;
// Process and convert arguments
if (!PyArg_ParseTuple(args, "OO:schedule", &paths, &callback))
return NULL;
PyArg_ParseTuple 内部到底发生了什么?我的猜测是回调获取我传递给 args 的函数指针(也是 PyObject*)。 PyArg_ParseTuple 如何将函数指针转换为 PyObject*?
我想知道的是,如果我两次传入同一个回调函数指针会发生什么。我认为回调在 PyArg_ParseTuple 中分配了一个新的 PyObject,因此每次都会获得不同的内存地址,但将包含相同的回调函数指针。
但是如果我 PyObject_Hash 回调,它每次都会产生不同的值,对吗? (因为每次地址都不同..)
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..)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PyArg_ParseTuple
不关心“O”参数的类型。不进行任何转换。没有创建新对象。对象的地址被放入您指定的PyObject *
C 变量中。它对两个参数的作用完全相同。我无法想象
PyObject_Hash
的相关性是什么。如果您想比较回调参数的两个形式,只需在地址上使用==
即可。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 thePyObject *
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.重点是,如果您两次传递相同的回调,它将收到两个对象,但您永远不会被允许只读取最后一个对象。您将遇到一种内存泄漏,因为两个指针之一将不会被引用。当然,垃圾收集器最终会在你后面经过来清理所有的混乱。但无论如何...我误读了 PyObject_Hash 应该在回调和路径上调用。会是一样的。但您可能想比较回调和路径: if(callback==paths) {printf("it's the same callabck");}
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");}