Alexandria  2.25.0
SDC-CH common library for the Euclid project
Exceptions.cpp
Go to the documentation of this file.
1 
19 #include "Pyston/Exceptions.h"
20 #include "Pyston/GIL.h"
21 #include <Python.h>
22 #include <boost/python/extract.hpp>
23 #include <boost/python/handle.hpp>
24 #include <boost/python/object.hpp>
25 
26 namespace py = boost::python;
27 
28 namespace Pyston {
29 
31  GILLocker locker;
32 
33  PyObject *ptype, *pvalue, *ptraceback;
34  PyErr_Fetch(&ptype, &pvalue, &ptraceback);
35  PyErr_NormalizeException(&ptype, &pvalue, &ptraceback);
36 
37  py::handle<> handle_type(ptype);
38  py::handle<> handle_value(pvalue);
39  py::handle<> handle_traceback(py::allow_null(ptraceback));
40 
41  // Get only the error message
42  py::object err_msg_obj(py::handle<>(PyObject_Str(pvalue)));
43  m_error_msg = py::extract<std::string>(err_msg_obj);
44  if (m_error_msg.empty()) {
45  py::object err_repr_obj(py::handle<>(PyObject_Repr(pvalue)));
46  m_error_msg = py::extract<std::string>(err_repr_obj);
47  }
48 
49  // Generate traceback
50  if (ptraceback) {
51  py::object traceback(handle_traceback);
52  while (traceback) {
53  Location loc;
54  loc.lineno = py::extract<long>(traceback.attr("tb_lineno"));
55  loc.filename = py::extract<std::string>(traceback.attr("tb_frame").attr("f_code").attr("co_filename"));
56  loc.funcname = py::extract<std::string>(traceback.attr("tb_frame").attr("f_code").attr("co_name"));
57 
58  m_traceback.emplace_back(loc);
59 
60  traceback = traceback.attr("tb_next");
61  }
62  }
63 
64  // Done
65  PyErr_Clear();
66 }
67 
68 auto Exception::getTraceback() const -> const std::list<Location>& {
69  return m_traceback;
70 }
71 
73  for (auto& trace : m_traceback) {
75  msg << "File \"" << trace.filename << "\", line " << trace.lineno << ", in " << trace.funcname;
76  logger.log(level, msg.str());
77  }
78  return *this;
79 }
80 
81 } // end of namespace Pyston
static Elements::Logging logger
Logger.
Definition: Example.cpp:55
std::string m_error_msg
void log(log4cpp::Priority::Value level, const std::string &logMessage)
std::list< Location > m_traceback
Definition: Exceptions.h:59
const std::list< Location > & getTraceback() const
Definition: Exceptions.cpp:68
const Exception & log(log4cpp::Priority::Value level, Elements::Logging &logger) const
Log error message and traceback.
Definition: Exceptions.cpp:72
T empty(T... args)
boost::variant< bool, int64_t, double, AttributeSet > Value
Definition: Node.h:88
STL namespace.
T str(T... args)
Traceback location.
Definition: Exceptions.h:35