小编Sim*_*mon的帖子

如何在Cython中将函数指针传递给外部程序

我正在尝试编写一个C程序的包装器,以便我可以从Python调用它.我正在使用Cython来做到这一点.C函数将回调函数作为参数,但这个回调函数只能在python程序的运行时知道.我一直在寻找如何做到这一点,似乎没有简单的解决方案,但以下似乎工作:

#python.py

libc = cdll.LoadLibrary("myfunc.so") #Callback function is defined in myfunc.so 
....
c_wrapper(libc.fun, ...)
Run Code Online (Sandbox Code Playgroud)

.

#c_wrapper.pyx

cdef extern void mainfunction(void *F, ...) #The intial C function we are wrapping
ctypedef void (*myfuncptr) () 

def c_wrapper(f, ...) # our function pointer is passed to the wrapper as a Python object
    cdef myfuncptr thisfunc
    thisfunc = (<myfuncptr*><size_t>addressof(f))[0]
    mainfunction(thisfunc, ...)
Run Code Online (Sandbox Code Playgroud)

这个方法适用于C和FORTRAN函数(我假设它适用于大多数编译的语言)和Python函数(使用C类型),但它似乎有点尴尬.在Cython中有没有更简单的方法呢?

谢谢

编辑:我无法更改我想要包装的C库

python cython

5
推荐指数
1
解决办法
3190
查看次数

GFortran相当于ieee_exceptions

我正在尝试编写一个程序,无论何时使用GFortran编译,无论何时执行无效操作都会停止.有了ifort,我可以这样做:

use ieee_exceptions
....
logical      :: halt
....
call ieee_get_halting_mode(IEEE_USUAL,halt)
call ieee_set_halting_mode(IEEE_USUAL,.True.)
....
! Something that may stop the program
....
call ieee_set_halting_mode(IEEE_USUAL,halt)    
Run Code Online (Sandbox Code Playgroud)

GFortran有一个类似于ifort的模块ieee_exceptions吗?或者更好的是有没有办法停止停止模式而不知道程序将如何编译或将使用哪个编译器?

fortran gfortran intel-fortran

4
推荐指数
1
解决办法
850
查看次数

如何学习Fortran.问题?

我刚刚找到一份工作,从一个月开始,要求我使用fortran.

我带了几本书,但他们似乎没有任何问题或问题,这就是我学得最好的方法.

我想知道你是否可以推荐和提供我可以练习的问题的书籍或网站.

谢谢

fortran

3
推荐指数
1
解决办法
9346
查看次数

标签 统计

fortran ×2

cython ×1

gfortran ×1

intel-fortran ×1

python ×1