Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.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 基于C-exit代码捕获ctypes中的异常_Python_C_Ctypes - Fatal编程技术网

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
没有异常处理的概念。每种有异常处理的语言都使用单独的技术,因此不可能进行跨语言异常处理。您可以将错误处理程序设置为。