如何获取Python异常文本 [英] How to get Python exception text

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

问题描述

我想在我的C ++应用程序中嵌入python。我使用Boost库 - 伟大的工具。但我有一个问题。



如果python函数抛出一个异常,我想抓住它,并在我的应用程序打印错误或得到一些详细的信息,如python脚本中的行号, p>

我该怎么办呢?我找不到任何函数来获取Python API或Boost中的详细异常信息。

  try {
module = import(MyModule); //如果MyModule包含一个错误,这行会抛出excetion
} catch(error_already_set const&){
//这里我可以说我有错误,但我无法确定是什么导致错误
std :: cout<< 错误! << std :: endl;
}



PyErr_Print()只是将错误文本打印到stderr并清除错误,

解决方案

好吧,我找到了怎么办。



没有提升(只有错误消息,因为从traceback提取信息的代码太重,不能在这里张贴):

  PyObject * ptype,* pvalue,* ptraceback; 
PyErr_Fetch(& ptype,& pvalue,& ptraceback);
// pvalue包含错误消息
// ptraceback包含堆栈快照和许多其他信息
//(参见python traceback结构)

//获取错误消息
char * pStrErrorMessage = PyString_AsString(pvalue);

和BOOST版本

  try {
//一些抛出错误的代码
} catch(error_already_set&){

PyObject * ptype,* pvalue,* ptraceback;
PyErr_Fetch(& ptype,& pvalue,& ptraceback);

句柄<> hType(ptype);
object extype(hType);
句柄<> hTraceback(ptraceback);
object traceback(hTraceback);

//提取错误消息
string strErrorMessage = extract< string>(pvalue);

//提取行号(调用堆栈的顶部条目)
//如果要提取另一级别的调用堆栈
//也可以处理traceback.attr(tb_next )recurently
long lineno = extract< long> (traceback.attr(tb_lineno));
string filename = extract< string>(traceback.attr(tb_frame)。attr(f_code)。attr(co_filename));
string funcname = extract< string>(traceback.attr(tb_frame)。attr(f_code)。attr(co_name));
... // cleanup here


I want to embed python in my C++ application. I'm using Boost library - great tool. But i have one problem.

If python function throws an exception, i want to catch it and print error in my application or get some detailed information like line number in python script that caused error.

How can i do it? I can't find any functions to get detailed exception information in Python API or Boost.

try {
module=import("MyModule"); //this line will throw excetion if MyModule contains an   error
} catch ( error_already_set const & ) {
//Here i can said that i have error, but i cant determine what caused an error
std::cout << "error!" << std::endl;
}

PyErr_Print() just prints error text to stderr and clears error so it can't be solution

解决方案

Well, I found out how to do it.

Without boost (only error message, because code to extract info from traceback is too heavy to post it here):

PyObject *ptype, *pvalue, *ptraceback;
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
//pvalue contains error message
//ptraceback contains stack snapshot and many other information
//(see python traceback structure)

//Get error message
char *pStrErrorMessage = PyString_AsString(pvalue);

And BOOST version

try{
//some code that throws an error
}catch(error_already_set &){

    PyObject *ptype, *pvalue, *ptraceback;
    PyErr_Fetch(&ptype, &pvalue, &ptraceback);

    handle<> hType(ptype);
    object extype(hType);
    handle<> hTraceback(ptraceback);
    object traceback(hTraceback);

    //Extract error message
    string strErrorMessage = extract<string>(pvalue);

    //Extract line number (top entry of call stack)
    // if you want to extract another levels of call stack
    // also process traceback.attr("tb_next") recurently
    long lineno = extract<long> (traceback.attr("tb_lineno"));
    string filename = extract<string>(traceback.attr("tb_frame").attr("f_code").attr("co_filename"));
    string funcname = extract<string>(traceback.attr("tb_frame").attr("f_code").attr("co_name"));
... //cleanup here

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

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