如何用Cython从time.h调用time? [英] How to call time from time.h with Cython?
问题描述
我正在尝试直接用Cython加载time.h而不是Python的导入时间
,但这不起作用。
I am trying to load time.h directly with Cython instead of Python's import time
but it doesn't work.
我得到的只是一个错误
Call with wrong number of arguments (expected 1, got 0)
具有以下代码
cdef extern from "time.h" nogil:
ctypedef int time_t
time_t time(time_t*)
def test():
cdef int ts
ts = time()
return ts
和
Cannot assign type 'long' to 'time_t *'
具有以下代码
cdef extern from "time.h" nogil:
ctypedef int time_t
time_t time(time_t*)
def test():
cdef int ts
ts = time(1)
return ts
具有数学日志I可以简单地做
with math log I can simply do
cdef extern from "math.h":
double log10(double x)
为什么不是这样 time 可能吗?
How comes it is not possible with time?
推荐答案
time $ c的参数$ c>是
time_t
值的地址(即指针)以填充或NULL 。
The parameter to time
is the address (i.e.: "pointer") of a time_t
value to fill or NULL.
要引用人2次:
time_t time(time_t * t);
time_t time(time_t *t);
[ ...]
如果t为非NULL,则返回值也存储在t指向的内存中。
If t is non-NULL, the return value is also stored in the memory pointed to by t.
有些标准函数既返回值,又(可能)在提供的地址中存储相同的值,这很奇怪。像大多数体系结构中 NULL 一样,将 0
作为参数传递是绝对安全的,它等效于((void *)0)
。在这种情况下,时间
将仅返回结果,而不会尝试将其存储在提供的地址中。
It is an oddity of some standard functions to both return a value and (possibly) store the same value in a provided address. It is perfectly safe to pass 0
as parameter as in most architecture NULL is equivalent to ((void*)0)
. In that case, time
will only return the result, and will not attempt to store it in the provided address.
这篇关于如何用Cython从time.h调用time?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!