如何捕获ctypes中抛出的异常? [英] How to catch an exception thrown in ctypes?

查看:232
本文介绍了如何捕获ctypes中抛出的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ctypes从Python调用的一些C代码。在C库的某处,发生异常和/或中止()被调用。有什么办法可以在我的Python调用者代码中找到这个吗? (平台是Linux)

解决方案

您可能可以在SIGABRT上设置一个信号处理程序来处理由abort()引起的信号, 。
但是,失败的断言可能与损坏的内存和其他坏的东西 - 通常有一个原因,断言失败。因此,通常终止应用程序是您可以做的最好的事情(除非在终止前显示/记录处理程序中的错误)。


I am working with some C code called from Python using ctypes. Somewhere in the bowels of the C library, an exception is occurring and/or abort() is being called. Is there any way I can catch this in my Python caller code? (Platform is Linux)

解决方案

You might be able to setup a signal handler on SIGABRT to handle the signal caused by abort(). However, failed assertions might go along with corrupted memory and other bad things - there's usually a reason why an assertion failed. So usually terminating the applications is the best thing you can do (except displaying/logging an error in your handler before terminating).

这篇关于如何捕获ctypes中抛出的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆