当我尝试将ctypes数组用作numpy数组时,我收到以下警告消息:
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes, numpy
>>> TenByteBuffer = ctypes.c_ubyte * 10
>>> a = TenByteBuffer()
>>> b = numpy.ctypeslib.as_array(a)
C:\Python27\lib\site-packages\numpy\ctypeslib.py:402: RuntimeWarning: Item size
computed from the PEP 3118 buffer format string does not match the actual item s
ize.
return array(obj, copy=False)
>>> b
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=uint8)
Run Code Online (Sandbox Code Playgroud)
但是代码似乎正在起作用.忽略这个警告是不是一个坏主意?
背景:我正在调用一个实时生成数据的C …
我有一个Rust函数返回一个array我想要使用这个数组Python,它可能是一个list或numpy.array它并不重要.
我的Rust功能如下所示:
#[no_mangle]
pub extern fn make_array() -> [i32; 4] {
let my_array: [i32; 4] = [1,2,3,4];
return my_array;
}
Run Code Online (Sandbox Code Playgroud)
我试图用Python调用它:
In [20]: import ctypes
In [21]: from ctypes import cdll
In [22]: lib = cdll.LoadLibrary("/home/user/RustStuff/embed/target/release/libembed.so")
In [23]: lib.make_array.restype = ctypes.ARRAY(ctypes.c_int32, 4)
In [24]: temp = lib.make_array()
In [25]: [i for i in temp]
Out[25]: [1, 2, -760202930, 32611]
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?为什么我的输出不是[1,2,3,4]?为什么我的前两个元素是正确的,另外两个元素是垃圾?
我无法找到任何好的文档ctypes.ARRAY,所以我只是选择了正确的,所以这可能是问题所在.
我正在尝试使用 ctypes 包装一个 C 函数,它返回一个未知大小的字符数组。该函数来自 gdal c api,但我的问题并非特定于该函数。
我想知道是否有一种通用方法可以解构返回未知大小的 char** 数组对象的函数的输出。在 ctypes 中,这将是POINTER(c_char_p * X)X 未知的地方。
# Define the function wrapper.
f = ctypes.CDLL('libgdal.so.20').GDALGetMetadata
MAX_OUTPUT_LENGTH = 10
f.restype = ctypes.POINTER(ctypes.c_char_p * MAX_OUTPUT_LENGTH)
f.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
# Example call (the second argument can be null).
result = []
counter = 0
output = f(ptr, None).contents[counter]
while output:
result.append(output)
counter += 1
output = f(ptr, None).contents[counter]
Run Code Online (Sandbox Code Playgroud)
output结果数组在哪里,ptr是指向打开的 GDALRaster 的 …
我有Python代码和C代码的结构.我填写这些字段
("bones_pos_vect",((c_float*4)*30)),
("bones_rot_quat",((c_float*4)*30))
Run Code Online (Sandbox Code Playgroud)
在具有正确值的python代码中,但是当我在C代码中请求它们时,我从所有数组单元格中得到0.0.为什么我会失去价值观?我的结构的所有其他领域工作正常.
class SceneObject(Structure):
_fields_ = [("x_coord", c_float),
("y_coord", c_float),
("z_coord", c_float),
("x_angle", c_float),
("y_angle", c_float),
("z_angle", c_float),
("indexes_count", c_int),
("vertices_buffer", c_uint),
("indexes_buffer", c_uint),
("texture_buffer", c_uint),
("bones_pos_vect",((c_float*4)*30)),
("bones_rot_quat",((c_float*4)*30))]
typedef struct
{
float x_coord;
float y_coord;
float z_coord;
float x_angle;
float y_angle;
float z_angle;
int indexes_count;
unsigned int vertices_buffer;
unsigned int indexes_buffer;
unsigned int texture_buffer;
float bones_pos_vect[30][4];
float bones_rot_quat[30][4];
} SceneObject;
Run Code Online (Sandbox Code Playgroud)