通过 ctypes returns 从 Python 调用的 C 函数不正确的值
C function called from Python via ctypes returns incorrect value
我用 C 语言编写了一个简单的函数,可以将给定数字提高到给定的幂。当我在 C 中调用它时,函数 return 是正确的值,但是当我在 Python 中调用它时,它 return 是一个不同的、不正确的值。
我使用以下命令创建了共享文件:
$ gcc -fPIC -shared -o test.so test.c
我已经尝试了 C 函数的不同配置,其中一些 return 是预期值,而另一些则不是。例如,当我的函数是一个使用 return x*x
、没有 for
循环的简单正方形时,它 return 在 Python 中编辑了正确的值。
我希望最终能够在 python 中调用一个 C 函数,它将 return 一个二维 C 数组。
#include <stdio.h>
float power(float x, int exponent)
{
float val = x;
for(int i=1; i<exponent; i++){
val = val*x;
}
return val;
}
from ctypes import *
so_file = '/Users/.../test.so'
functions = CDLL(so_file)
functions.power.argtype = [c_float, c_int]
functions.power.restype = c_float
print(functions.power(5,3))
当我在 C 中调用函数时,我获得了 125.0 的预期输出,但是当我在 python 中调用函数时,它 return 的值为 0.0。
这是我第一次使用ctypes。我是否犯了一个明显的错误导致函数计算错误?
清单[Python 3.Docs]: ctypes - A foreign function library for Python.
为了在调用函数(驻留在 .dll (.so) 中,需要指定 2 件事(留下 x86 调用惯例 (Win) 除了:
- 参数类型
- Return类型
在 CTypes 中,这是通过指定实现的:
- argtypes - 包含每个参数 (CTypes) 的列表(实际上是一个序列)类型(按照它们在函数中出现的顺序 header)
- restype - 单个 CTypes 类型
旁注:上述方法的替代方法是对外部函数进行原型设计(CFUNCTYPE,WINFUNCTYPE, PYFUNCTYPE - 检查函数原型部分(在URL开头)。
总之:
- 指定失败
- 拼写错误(基本上与前面的项目符号相同)
其中任何一个(需要(1)),将导致应用默认值:全部将(C89 样式)视为 ints,(在大多数系统上) 是 32 位长。
这会生成未定义的行为(2)(也适用于错误指定 他们),尤其是 64bit CPU / OS ,其中较大类型的值(例如指针)可能会被截断。
显示的错误可能很多,有时还会产生误导。
您拼错了 argtype(缺少 s 最后)。
纠正一下,你应该没问题
示例:
对于由 libdll.dll 导出的函数 func (libdll.so) 与以下 header:
double func(uint32_t ui, float f, long long vll[8], void *pv, char *pc);
Python 等价于:
func = libdll.func
func.argtypes = (ctypes.c_uint32, ctypes.c_float, ctypes.c_longlong * 8, ctypes.c_void_p, ctypes.POINTER(ctypes.c_char))
'''
It would be a lot easier (nicer, and in most cases recommended)
the last element to be ctypes.c_char_p,
but I chose this form to illustrate pointers in general.
'''
func.restype = ctypes.c_double
相同(或非常相似)场景(还有许多其他场景)的一些(更具破坏性的)结果:
- [SO]: Python ctypes cdll.LoadLibrary, instantiate an object, execute its method, private variable address truncated (@CristiFati's answer)
- [SO]: python ctypes issue on different OSes (@CristiFati's answer)
脚注
#1:从技术上讲,有些情况不需要指定它们。但即便如此,最好还是指定它们以消除任何可能的混淆:
不带参数的函数:
function_from_dll.argtypes = ()
函数返回 void:
function_from_dll.restype = None
#2:未定义的行为([Wikipedia]: Undefined behavior) 顾名思义,是一段代码的结果无法“预测”(或保证)的情况。主要案例:
- 按预期工作
- 没有按预期工作
- 有一些有趣的输出/副作用
- 崩溃
它的“美妙之处”在于有时它看起来完全是随机的,有时它在某些特定情况下“只重现”(不同的机器,不同的 OSes,不同的环境,...)。底线是纯属巧合!问题在于代码(可以是当前代码(最有可能)或它使用的其他代码(库、编译器))。
我用 C 语言编写了一个简单的函数,可以将给定数字提高到给定的幂。当我在 C 中调用它时,函数 return 是正确的值,但是当我在 Python 中调用它时,它 return 是一个不同的、不正确的值。
我使用以下命令创建了共享文件:
$ gcc -fPIC -shared -o test.so test.c
我已经尝试了 C 函数的不同配置,其中一些 return 是预期值,而另一些则不是。例如,当我的函数是一个使用 return x*x
、没有 for
循环的简单正方形时,它 return 在 Python 中编辑了正确的值。
我希望最终能够在 python 中调用一个 C 函数,它将 return 一个二维 C 数组。
#include <stdio.h>
float power(float x, int exponent)
{
float val = x;
for(int i=1; i<exponent; i++){
val = val*x;
}
return val;
}
from ctypes import *
so_file = '/Users/.../test.so'
functions = CDLL(so_file)
functions.power.argtype = [c_float, c_int]
functions.power.restype = c_float
print(functions.power(5,3))
当我在 C 中调用函数时,我获得了 125.0 的预期输出,但是当我在 python 中调用函数时,它 return 的值为 0.0。
这是我第一次使用ctypes。我是否犯了一个明显的错误导致函数计算错误?
清单[Python 3.Docs]: ctypes - A foreign function library for Python.
为了在调用函数(驻留在 .dll (.so) 中,需要指定 2 件事(留下 x86 调用惯例 (Win) 除了:
- 参数类型
- Return类型
在 CTypes 中,这是通过指定实现的:
- argtypes - 包含每个参数 (CTypes) 的列表(实际上是一个序列)类型(按照它们在函数中出现的顺序 header)
- restype - 单个 CTypes 类型
旁注:上述方法的替代方法是对外部函数进行原型设计(CFUNCTYPE,WINFUNCTYPE, PYFUNCTYPE - 检查函数原型部分(在URL开头)。
总之:
- 指定失败
- 拼写错误(基本上与前面的项目符号相同)
其中任何一个(需要(1)),将导致应用默认值:全部将(C89 样式)视为 ints,(在大多数系统上) 是 32 位长。
这会生成未定义的行为(2)(也适用于错误指定 他们),尤其是 64bit CPU / OS ,其中较大类型的值(例如指针)可能会被截断。
显示的错误可能很多,有时还会产生误导。
您拼错了 argtype(缺少 s 最后)。
纠正一下,你应该没问题
示例:
对于由 libdll.dll 导出的函数 func (libdll.so) 与以下 header:
double func(uint32_t ui, float f, long long vll[8], void *pv, char *pc);
Python 等价于:
func = libdll.func
func.argtypes = (ctypes.c_uint32, ctypes.c_float, ctypes.c_longlong * 8, ctypes.c_void_p, ctypes.POINTER(ctypes.c_char))
'''
It would be a lot easier (nicer, and in most cases recommended)
the last element to be ctypes.c_char_p,
but I chose this form to illustrate pointers in general.
'''
func.restype = ctypes.c_double
相同(或非常相似)场景(还有许多其他场景)的一些(更具破坏性的)结果:
- [SO]: Python ctypes cdll.LoadLibrary, instantiate an object, execute its method, private variable address truncated (@CristiFati's answer)
- [SO]: python ctypes issue on different OSes (@CristiFati's answer)
脚注
#1:从技术上讲,有些情况不需要指定它们。但即便如此,最好还是指定它们以消除任何可能的混淆:
不带参数的函数:
function_from_dll.argtypes = ()
函数返回 void:
function_from_dll.restype = None
#2:未定义的行为([Wikipedia]: Undefined behavior) 顾名思义,是一段代码的结果无法“预测”(或保证)的情况。主要案例:
- 按预期工作
- 没有按预期工作
- 有一些有趣的输出/副作用
- 崩溃
它的“美妙之处”在于有时它看起来完全是随机的,有时它在某些特定情况下“只重现”(不同的机器,不同的 OSes,不同的环境,...)。底线是纯属巧合!问题在于代码(可以是当前代码(最有可能)或它使用的其他代码(库、编译器))。