From cef433eccb012d05fb1bd4d04478c61d6c9c02e0 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 21 Apr 2017 12:50:18 +0800 Subject: [PATCH] mgr: move handle_pyerror() from MgrPyModules to MgrPyModule so we can reuse it in a more sane way. Signed-off-by: Kefu Chai --- src/mgr/MgrPyModule.cc | 27 +++++++++++++++++++++++++-- src/mgr/MgrPyModule.h | 2 ++ src/mgr/PyModules.cc | 25 ------------------------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/mgr/MgrPyModule.cc b/src/mgr/MgrPyModule.cc index 35b28f73310..033825855c9 100644 --- a/src/mgr/MgrPyModule.cc +++ b/src/mgr/MgrPyModule.cc @@ -18,8 +18,31 @@ #include "MgrPyModule.h" -// FIXME: import sanely -extern std::string handle_pyerror(void); +//XXX courtesy of http://stackoverflow.com/questions/1418015/how-to-get-python-exception-text +#include +#include "include/assert.h" // boost clobbers this + +// decode a Python exception into a string +std::string handle_pyerror() +{ + using namespace boost::python; + using namespace boost; + + PyObject *exc, *val, *tb; + object formatted_list, formatted; + PyErr_Fetch(&exc, &val, &tb); + handle<> hexc(exc), hval(allow_null(val)), htb(allow_null(tb)); + object traceback(import("traceback")); + if (!tb) { + object format_exception_only(traceback.attr("format_exception_only")); + formatted_list = format_exception_only(hexc, hval); + } else { + object format_exception(traceback.attr("format_exception")); + formatted_list = format_exception(hexc,hval, htb); + } + formatted = str("").join(formatted_list); + return extract(formatted); +} #define dout_context g_ceph_context #define dout_subsys ceph_subsys_mgr diff --git a/src/mgr/MgrPyModule.h b/src/mgr/MgrPyModule.h index 9d50a1b4410..b466d1ac20f 100644 --- a/src/mgr/MgrPyModule.h +++ b/src/mgr/MgrPyModule.h @@ -77,5 +77,7 @@ public: std::stringstream *ss); }; +std::string handle_pyerror(); + #endif diff --git a/src/mgr/PyModules.cc b/src/mgr/PyModules.cc index 247e6b3fcd3..0926757844a 100644 --- a/src/mgr/PyModules.cc +++ b/src/mgr/PyModules.cc @@ -286,31 +286,6 @@ PyObject *PyModules::get_python(const std::string &what) } } -//XXX courtesy of http://stackoverflow.com/questions/1418015/how-to-get-python-exception-text -#include -// decode a Python exception into a string -std::string handle_pyerror() -{ - using namespace boost::python; - using namespace boost; - - PyObject *exc,*val,*tb; - object formatted_list, formatted; - PyErr_Fetch(&exc,&val,&tb); - handle<> hexc(exc),hval(allow_null(val)),htb(allow_null(tb)); - object traceback(import("traceback")); - if (!tb) { - object format_exception_only(traceback.attr("format_exception_only")); - formatted_list = format_exception_only(hexc,hval); - } else { - object format_exception(traceback.attr("format_exception")); - formatted_list = format_exception(hexc,hval,htb); - } - formatted = str("").join(formatted_list); - return extract(formatted); -} - - std::string PyModules::get_site_packages() { std::stringstream site_packages; -- 2.39.5