[英]in method 'my_print_hexbytes', argument 1 of type 'uint32_t *'
我正在用SWIG写Python C扩展,但是在将二进制缓冲区传递给C api函数时感到沮丧。 这是我的示例:
在utils.c
#include "utils.h"
void my_print_hexbytes(uint32_t *bytes, uint32_t bytes_len)
{
uint32_t i;
for(i = 0; i < bytes_len; i++)
printf("%02x", bytes[i]);
printf("\n");
}
在utils.h
#include "commons.h"
#ifndef XXXX_UTILS_H
#define XXXX_UTILS_H
void my_print_hexbytes(uint32_t *bytes, uint32_t bytes_len);
#endif /* XXXX_UTILS_H */
commons.h
#ifndef XXXX_COMMONS_H
#define XXXX_COMMONS_H
....
....
#include <stdint.h>
....
....
#endif
在xxxx_for_py.i
%module xxxx_for_py
%{
#define SWIG_FILE_WITH_INIT
#include "commons.h"
#include "utils.h"
%}
%include "stdint.i"
void my_print_hexbytes(uint32_t *bytes, uint32_t bytes_len);
编译_xxxx_for_py.so
,我尝试在IPython中对其进行测试:
In [1]: import xxxx_for_py
In [2]: from ctypes import *
In [3]: a_t = c_uint * 2
In [4]: a = a_t(0x1, 0x2)
In [5]: xxxx_for_py.my_print_hexbytes(a, 2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-3c023fcf8b04> in <module>()
----> 1 xxxx_for_py.my_print_hexbytes(a, 2)
TypeError: in method 'my_print_hexbytes', argument 1 of type 'uint32_t *'
那个时候我不知道该如何处理。 我尝试更改a
as bytearray
但是没有用。
不知道是否有人可以在这个问题上帮助我。 谢谢!
感谢@Petesh评论。 我通过将a
为POINTER(c_uint)
了尝试,但仍然无法正常工作:(
In [5]: xxxx_for_py.my_print_hexbytes(cast(a, POINTER(c_uint)), 2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-2abb9dae484d> in <module>()
----> 1 xxxx_for_py.my_print_hexbytes(cast(a, POINTER(c_uint)), 2)
TypeError: in method 'my_print_hexbytes', argument 1 of type 'uint32_t *'
在SWIG文档中的“ 无 carray.i
数组”一节中引用了我将carray.i
添加到xxxx_for_py.i
在xxxx_for_py.i
....
....stdint.i"
%include "carrays.i"
%array_class(uint32_t, uint32Array)
....
重新编译并加载.so
,仍然出现相同的错误
In [1]: import xxxx_for_py
In [2]: a = xxxx_for_py.uint32Array(2)
In [3]: a[0] = 0x0
In [4]: a[1] = 0x1
In [5]: xxxx_for_py.my_print_hexbytes(a, 2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-3c023fcf8b04> in <module>()
----> 1 xxxx_for_py.my_print_hexbytes(a, 2)
TypeError: in method 'my_print_hexbytes', argument 1 of type 'uint32_t *'
经过简短的测试,我自己找到了解决方案。 有关详细信息,请参见本文http://au9ustine.github.io/wiki/crypto/cuda/swig.html#typeerror-in-method-xxxx-argument-y-of-type-zzzz ( 2017年1月13日更新 :此页面已损坏,我将在此处移动该页面的内容)
继续尝试通过将二进制缓冲区传递给普通的C模块(即,由gcc编译的库而没有额外的依赖关系)来测试Python代码。 内部可能看起来像:
integer list -> array/struct -> raw string -> C function parameter
从create_string_buffer的一些描述中。 通过create_string_buffer
,Python可以提供这样的功能,即动态分配内存供当前变量使用。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
size_t
print_int_buf(void *src, size_t len)
{
size_t i = 0;
uint32_t *int_ptr = (uint32_t *)src;
for (i = 0; i < len; i++) {
printf("%p: %x\n", int_ptr + i, int_ptr[i]);
}
return i;
}
如果编译为名为my_ext_lib.so
的共享库,则可以在Python中加载代码
import ctypes
from ctypes import *
import array
my_ext = ctypes.CDLL('./my_ext_lib.so')
buf = create_string_buffer(array.array('I', range(0x10)).tostring())
my_ext_lib.print_int_buf(buf, 0x10)
它确实起作用。
尽管ctype和SWIG是用于在Python和C之间进行互操作集成的完全不同的方法,但是我在这里使用ctype代码来详细说明将参数传递给已编译模块的基本机制及其工作方式(适用于Python模块和琐碎的C共享库模块)。
由于我认为最大的区别是函数接口(或方法签名),为什么不将其更改为void *
而不是uint32_t *
? (尽管这不是最好的方法,但它仍然可能是临时的遏制解决方案)
我将my_print_hexbytes
方法的uint32_t *
替换为void *
,并附加了依赖项cdata.i
(我不确定它是否有效,但出于安全考虑已添加了它)。 因此,更改在下面列出,最终显示了预期的结果。
utils.c
#include "utils.h"
...
void
my_print_hexbytes(void *bytes, size_t bytes_len)
{
size_t i = 0;
uint32_t *int_buf_ptr = (uint32_t *)bytes;
for(i = 0; i < bytes_len; i++)
printf("%02x", int_buf_ptr[i]);
printf("\n");
}
头文件utils.h
#include "commons.h"
#ifndef XXXX_UTILS_H
#define XXXX_UTILS_H
...
void my_print_hexbytes(void *bytes, size_t bytes_len);
#endif /* XXXX_UTILS_H */
xxxx_for_py.i
%module xxxx_for_py
%{
#define SWIG_FILE_WITH_INIT
#include "commons.h"
#include "utils.h"
%}
%include "stdint.i"
%include "carrays.i"
%include "cdata.i"
%array_class(uint32_t, uint32Array)
...
void my_print_hexbytes(void *bytes, size_t bytes_len);
在Python中调用方法,就像:
In [1]: import xxxx_for_py
In [2]: a = xxxx_for_py.uint32Array(0x10)
In [3]: for i in range(0x10):
...: a[i] = i
...:
In [4]: xxxx_for_py.my_print_hexbytes(a, 0x10)
000102030405060708090a0b0c0d0e0f
发现您的问题,因为同样的问题。
不幸的是,SWIG似乎不支持高级类型:在http://www.swig.org/Doc3.0/Library.html#Library_carrays中 ,在对array_class宏进行描述之后,其写道:“使用此宏时,键入只能使用简单的类型名称,例如int或float。”
我也尝试使用“ unsigned int”失败。
最后,我的解决方法是使用C ++向量而不是uint32_t指针,这是swig文件的相关部分:
%include "stdint.i"
%include "std_vector.i"
namespace std {
%template(vectoruint32) vector<uint32_t>;
};
h文件的一部分:
#include <vector>
static uint32_t foo(std::vector<uint32_t> v) {return v[1] - v[0];}
蟒蛇:
import example
l = [1, 2, 3, 4]
example.foo(l)
希望这可以帮助
我有一个非常简单的案例,但仍然出现此错误。 这个问题的解决方法有效: Swig python-C ++如何使用int8_t类型
我在.i文件中的模块def之后直接添加了%include "stdint.i"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.