Python ctypes的数组输出?

时间:2015-12-10 15:00:45

标签: python c++ ctypes

我需要使用ctypes从Python调用C函数,并让该函数将一个或多个数组提供给Python。数组将始终是简单类型,如long,bool,double。

我非常希望数组是否可以动态调整大小。我会在每次通话之前知道所需的,但不同的通话应使用不同的尺寸。

我想我应该在Python中分配数组并让C代码覆盖内容,以便Python最终可以解除分配它所分配的内存。

我控制Python和C代码。

我现在有这个不起作用:

C:

2015-12-10 22:59:01.320 Storyboard[7647:5116727] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x12fd39a60 UIButton:0x12fd3c4f0'thing1'.leading == UIView:0x12fd3d590.leadingMargin>",
    "<NSIBPrototypingLayoutConstraint:0x12fe48040 'IB auto generated at build time for view with fixed frame' H:|-(20)-[UIButton:0x12fd3c4f0'thing1'](LTR)   (Names: '|':UIView:0x12fd3d590 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x12fd39a60 UIButton:0x12fd3c4f0'thing1'.leading == UIView:0x12fd3d590.leadingMargin>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-12-10 22:59:01.327 Storyboard[7647:5116727] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<_UILayoutSupportConstraint:0x12fea5ac0 V:[_UILayoutGuide:0x12fd3d960(0)]>",
    "<_UILayoutSupportConstraint:0x12fe9e960 V:|-(0)-[_UILayoutGuide:0x12fd3d960]   (Names: '|':UIView:0x12fd3d590 )>",
    "<NSLayoutConstraint:0x12fd3d800 V:[_UILayoutGuide:0x12fd3d960]-(0)-[UIButton:0x12fd3c4f0'thing1']>",
    "<NSIBPrototypingLayoutConstraint:0x12fea5e40 'IB auto generated at build time for view with fixed frame' V:|-(20)-[UIButton:0x12fd3c4f0'thing1']   (Names: '|':UIView:0x12fd3d590 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x12fd3d800 V:[_UILayoutGuide:0x12fd3d960]-(0)-[UIButton:0x12fd3c4f0'thing1']>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

的Python:

FOO_API long Foo(long* batch, long bufferSize)
{
    for (size_t i = 0; i < bufferSize; i++)
    {
        batch[i] = i;
    }
    return 0;
}

产地:

print "start test"
FooFunction = Bar.LoadedDll.Foo
longPtrType = ctypes.POINTER(ctypes.c_long)
FooFunction.argtypes = [longPtrType, ctypes.c_long]
FooFunction.restype = ctypes.c_long

arrayType = ctypes.c_long * 7
pyArray = [1] * 7
print pyArray
errorCode = FooFunction(arrayType(*pyArray), 7)
print pyArray
print "test finished"

应该生产:

start test
[1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1]
test finished

为什么这不起作用?或者我需要以不同的方式做到这一点吗?

2 个答案:

答案 0 :(得分:0)

使用python列表构建C数组;两者都是不同的对象。代码打印python列表,不受Foo调用的影响。

您需要构建C数组,传递它,然后在调用后使用它:

arrayType = ctypes.c_long * 7
array = arrayType(*[1] * 7)
print list(array)
errorCode = FooFunction(array, len(array))
print list(array)

答案 1 :(得分:0)

感谢falsetru的极快答案。我没有立刻注意到它,同时我到达了这个,这似乎也有效。我想知道一个人是否比另一个更好?

print "start test"
FooFunction = GpGlobals.LoadedDll.Foo
longArrayType = ctypes.c_long * (7)
FooFunction.argtypes = [longArrayType, ctypes.c_long]
FooFunction.restype = ctypes.c_long

pyArray = longArrayType()
for l in pyArray:
    print l        
errorCode = FooFunction(pyArray, 7)
for l in pyArray:
    print l
print "test finished"

我最初并不认为这适用于动态大小的数组,但我所要做的就是在每次调用之前重新定义argtypes。