2
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License as
6
* published by the Free Software Foundation; version 2 of the
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21
# include <Python/Python.h>
26
#include <frameobject.h>
27
#include "base/python_utils.h"
28
#include "base/string_utilities.h"
32
std::string format_python_traceback(PyObject *tb)
34
PyTracebackObject *trace = (PyTracebackObject*)tb;
37
stack = "Traceback:\n";
38
while (trace && trace->tb_frame)
40
PyFrameObject *frame = (PyFrameObject*)trace->tb_frame;
41
stack += base::strfmt(" File \"%s\", line %i, in %s\n",
42
PyString_AsString(frame->f_code->co_filename),
44
PyString_AsString(frame->f_code->co_name));
45
PyObject *code = PyErr_ProgramText(PyString_AsString(frame->f_code->co_filename), trace->tb_lineno);
48
stack += base::strfmt(" %s", PyString_AsString(code));
51
trace = trace->tb_next;
57
std::string base::format_python_exception(std::string &summary)
59
std::string reason, stack;
60
PyObject *exc, *val, *tb;
62
PyErr_Fetch(&exc, &val, &tb);
63
PyErr_NormalizeException(&exc, &val, &tb);
67
PyObject *tmp = PyObject_Str(val);
70
reason = PyString_AsString(tmp);
76
stack = format_python_traceback(tb);
78
stack = "No stack information. ";
80
PyErr_Restore(exc, val, tb);
84
return stack + reason+"\n";