Python 基于C-exit代码捕获ctypes中的异常
我正在使用Python 基于C-exit代码捕获ctypes中的异常,python,c,ctypes,Python,C,Ctypes,我正在使用ctypes从Python/numpy调用一个用C编写的共享库。但是,当在C中使用exit函数时,在iPython中会出现一些意外的结果 考虑以下示例,其中数组“A”的第一项在C中修改。如果该值为负值,则应引发异常 C-代码: #include <stdlib.h> #include <stdio.h> #include <math.h> extern void cfun(double* A) { // raise exception if
ctypes
从Python/numpy
调用一个用C
编写的共享库。但是,当在C
中使用exit
函数时,在iPython
中会出现一些意外的结果
考虑以下示例,其中数组“A”的第一项在C
中修改。如果该值为负值,则应引发异常
C
-代码:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
extern void cfun(double* A)
{
// raise exception if A[0]<0.0
if ( A[0]<0.0 ) {
printf("Negative value of A[0] encountered\n");
exit(1);
}
// change "A[0]" to it's square
A[0] = pow(A[0],2);
}
import numpy as np
import ctypes
# include shared library
lib = ctypes.CDLL("./test.so")
# link to C-program, including input-typing
cfun = lib.cfun
cfun.restype = None
cfun.argtypes = [ np.ctypeslib.ndpointer(ctypes.c_double,flags="C_CONTIGUOUS") ]
# simple example
A = np.arange((5),dtype='float')+2.
cfun(A)
print A
# expected output: [ 4. 3. 4. 5. 6.]
# simple example
A[0] = -10.0
cfun(A)
print A
# expected output: exception, no output from "print A"
包装Python
-代码:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
extern void cfun(double* A)
{
// raise exception if A[0]<0.0
if ( A[0]<0.0 ) {
printf("Negative value of A[0] encountered\n");
exit(1);
}
// change "A[0]" to it's square
A[0] = pow(A[0],2);
}
import numpy as np
import ctypes
# include shared library
lib = ctypes.CDLL("./test.so")
# link to C-program, including input-typing
cfun = lib.cfun
cfun.restype = None
cfun.argtypes = [ np.ctypeslib.ndpointer(ctypes.c_double,flags="C_CONTIGUOUS") ]
# simple example
A = np.arange((5),dtype='float')+2.
cfun(A)
print A
# expected output: [ 4. 3. 4. 5. 6.]
# simple example
A[0] = -10.0
cfun(A)
print A
# expected output: exception, no output from "print A"
当我从命令行运行此代码时,程序会执行它应该执行的操作。输出:
[ 4. 3. 4. 5. 6.]
Negative value of A[0] encountered
但是,当我从iPython
- 被通缉的行为是引发某种异常
- 当前的行为是,当遇到错误时,
也存在iPython
C
-code。引入错误流会使所有函数之间的依赖关系过于复杂
请帮忙
exit
调用系统的exit函数并终止正在运行的进程,在您的例子中是ipython。在C中执行错误处理的方法是设置一些全局错误变量并返回一个状态标志
#include <math.h>
char *error_string;
extern char* get_error_string() {
return error_string;
}
extern int cfun(double* A)
{
// raise exception if A[0]<0.0
if ( A[0]<0.0 ) {
error_string = "Negative value of A[0] encountered\n";
return -1;
}
// change "A[0]" to it's square
A[0] = pow(A[0],2);
return 0;
}
#包括
字符*错误\u字符串;
外部字符*获取错误字符串(){
返回错误字符串;
}
外部内部cfun(双*A)
{
//如果[0]exit
调用系统的exit函数并终止正在运行的进程(在您的例子中是ipython),则引发异常。在C中执行错误处理的方法是设置一些全局错误变量并返回状态标志
#include <math.h>
char *error_string;
extern char* get_error_string() {
return error_string;
}
extern int cfun(double* A)
{
// raise exception if A[0]<0.0
if ( A[0]<0.0 ) {
error_string = "Negative value of A[0] encountered\n";
return -1;
}
// change "A[0]" to it's square
A[0] = pow(A[0],2);
return 0;
}
#包括
字符*错误\u字符串;
外部字符*获取错误字符串(){
返回错误字符串;
}
外部内部cfun(双*A)
{
//如果[0]发生异常,则引发异常exit
终止您的程序。如果您的程序是Python解释器,那么它将终止Python解释器。如果您不想这样做,请不要调用exit
在Python中是否无法捕获exit
呢?exit
终止您的程序。如果您的程序是Python解释器,那么它将终止Python解释器n解释器。如果您不想这样做,请不要调用exit
是否无法捕获Python中的exit
?谢谢!尽管……我真的希望避免这个额外的“流”。我真的很喜欢系统出口的粗糙行为,因为它正是我想要做的:停止,但不引入额外的变量/参数。这允许我使用返回
进行其他操作。此外,我的一些函数是用Fortran
编写的,其中错误代码需要额外的头变量C
和Fortran
没有异常处理的概念。每种有异常处理的语言都使用单独的技术,因此不可能进行跨语言异常处理。您可以将错误处理程序设置为。谢谢!尽管…我真的希望避免这种额外的“流”。我真的很喜欢系统出口的粗糙行为,因为它正是我想要做的:停止,但不引入额外的变量/参数。这允许我使用返回
进行其他操作。此外,我的一些函数是用Fortran
编写的,其中错误代码需要额外的头变量C
和Fortran
没有异常处理的概念。每种有异常处理的语言都使用单独的技术,因此不可能进行跨语言异常处理。您可以将错误处理程序设置为。