我使用pythoncapi来填充包含PyObject*
元素的列表,并将其传输到Python中的函数。一切都很顺利,但是有一个问题-Python中的transferredlist包含奇数<NULL>
个条目。这个列表也包含了“想要的”对象,所以看起来它几乎可以工作了。在
以下是列表预览:
[<NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <NULL>, <dbg.TBack object at 0x19675870>, <interface.Grid object at 0x196758B0>, <interface.Grid object at 0x196758F0>, <interface.slp object at 0x196757D0>]
我在pythoncapi中用于填充列表的代码。在
^{pr2}$我尝试过添加if(!handle)
检查,但在实践中似乎没有任何效果。在
<NULL>
项?
尝试this solution
或者
否则,您可以检查
^{pr2}$handle
的字符串表示形式。根据the documentation,您可以使用我在想
^{3}$问题在于如何分配列表。从the documentation到{}:
也就是说,它生成一个“
len
”空指针的列表,而不是生成一个带有预先分配空间的空列表。在解决方案是:
len
0),并保持其余代码不变(使用PyList_Append
),或者PyList_SetItem
而不是append。如果您使用这种方法,请注意PyList_setItem
窃取了一个引用(因此您必须incref
handle
),并且您想要的列表长度只包括那些带有getHandle()!=NULL
的对象。在相关问题 更多 >
编程相关推荐