Traceback of getting Python exceptions in C

Source: Internet
Author: User

1, if the General console program, you can use Pyerr_print (); Error messages will print directly to the console
2, if not console program, you need to use Pyerr_fetch (pyobject**,pyobject**,pyobject**,pyobject**)
Here is the code example: (from Stack Overflow)

char* Full_backtrace; Save Traceback
pyobject* Ptype,*pvalue,*ptraceback;
pyobject* Pystr,*module_name,*pyth_module,*pyth_func;
Char *str;
Pyerr_fetch (&ptype,&pvalue,&ptraceback);
Pystr = Pyobject_str (pvalue);
Str= pystring_asstring (PYSTR);
char* error_description = StrDup (str);
Module_name = pystring_fromstring ("Traceback");
Pyth_module = Pyimport_import (module_name);
Py_decref (module_name);
if (Pyth_module = = NULL) {
Full_backtrace = NULL;
Return
}
Pyth_func = pyobject_getattrstring (Pyth_module, "format_exception");
if (Pyth_func && Pycallable_check (pyth_func)) {
Pyobject *pyth_val;
Pyth_val = Pyobject_callfunctionobjargs (Pyth_func, PType, Pvalue, Ptraceback, NULL);
Pystr = Pyobject_str (pyth_val);
str = pystring_asstring (PYSTR);
Full_backtrace = StrDup (str);
Py_decref (Pyth_val);
}

C Gets the traceback of the python exception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.