简体   繁体   English

在Python C API代码中引发异常并停止执行

[英]Raise exception and halt execution in Python C API code

I have a code that has two Python functions, run(), and dudt(u, t, ...). 我有一个代码,有两个Python函数,run()和dudt(u,t,...)。 run calls a C function, odepack_odeint, which calls a Fortran subroutine, LSODA, which calls another C function, ode_function, which calls dudt. run调用一个C函数odepack_odeint,它调用一个Fortran子例程LSODA,它调用另一个调用dudt的C函数ode_function。 If there is an error in dudt, it can be printed by ode_function using PyErr_Print(), but I can't figure out how to tell the code to stop execution. 如果dudt中有错误,可以使用PyErr_Print()通过ode_function打印,但我无法弄清楚如何告诉代码停止执行。 I tried, 我试过了,

PyErr_Print()
PyErr_SetString(errobj, message)

in ode_function, but then the code segfaults. 在ode_function中,但代码段错误。 As I understand it, what's supposed to happen next is for ode_function to return NULL or -1 or something. 据我了解,接下来应该发生的是ode_function返回NULL或-1或其他东西。 However, LSODA expects ode_function to be void. 但是,LSODA期望ode_function无效。

This is an attempt to patch scipy.integrate.odeint, based on this question that I asked previously. 这是尝试修补scipy.integrate.odeint,基于我之前问过的这个问题

pv on this Github issue for Scipy explained what the issues are. 关于Scipy的这个Github问题的 pv解释了问题所在。 In short, setjump() and longjump() in C could do this in principle, but this is error-prone, notably not being threadsafe. 简而言之,C中的setjump()和longjump()原则上可以做到这一点,但这很容易出错,特别是不是线程安全的。 This may be of use in other similar applications, but using f2py is generally the better way to get Fortran code to communicate with Python. 这可能在其他类似的应用程序中使用,但使用f2py通常是让Fortran代码与Python通信的更好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM