Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Cython中将NULL传递给包装的C函数_Python_Cython - Fatal编程技术网

Python 在Cython中将NULL传递给包装的C函数

Python 在Cython中将NULL传递给包装的C函数,python,cython,Python,Cython,我有一个用Cython包装的函数: cdef extern from "myheader.h": int c_my_func (const_char *a, const_char* b) 以及一个暴露于Python的函数: def my_func(a, b): c_my_func(a, b) 函数c_my_func接受参数a和b为空。当我从口译员那里调用它时: my_func(None, None) 它抛出异常: TypeError: expected string or

我有一个用Cython包装的函数:

cdef extern  from "myheader.h":
   int c_my_func (const_char *a, const_char* b)
以及一个暴露于Python的函数:

def my_func(a, b):
    c_my_func(a, b)
函数
c_my_func
接受参数
a
b
为空。当我从口译员那里调用它时:

my_func(None, None)
它抛出异常:

TypeError: expected string or Unicode object, NoneType found

如何使此函数接受
None
并将
NULL
传递给
c_my_func
?我不想手动检查
None
然后传递NULL。我还尝试在
c_my_func
的cdef上使用默认参数,但没有成功。

您可以在中找到答案

None
与任何C类型都不兼容。为了适应这一点 默认行为是带有cdefed参数的函数也可以 接受
None

如果您想考虑<代码> NO/<代码>无效的输入,那么您需要编写 检查并引发相应异常的代码
解决方案是在编写时手动检查
None
并传递NULL

只需为c函数调用构建一个包装器函数。它可以处理任意数量的参数

def c_func_call(c_func, *args):
   c_func(*[x if x is not None else NULL for x in args])
示例中的用法:

def my_func(a, b):
    c_func_call(c_my_func, a, b)

像这样使用
而不是None
怎么样

def widen_shrubbery(Shrubbery sh not None, extra_width):
    sh.width = sh.width + extra_width

正如暴露的一样。

对于一种本该为您做这些事情的语言来说,这是一个真正的失败。谢谢你的回答。哪个包或模块提供空值?请出示相应的导入文件。