mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-07-11 22:03:01 -06:00
bazaar: BoostPy: means of exception catch with info, ValueCtrl: prints ErrorValue
git-svn-id: svn://ultimatepp.org/upp/trunk@3898 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
6d0781f55d
commit
6e96b9a399
9 changed files with 104 additions and 15 deletions
|
|
@ -46,8 +46,19 @@ void BoostPyTest::EvalCB()
|
|||
{
|
||||
String s = ev.GetData();
|
||||
|
||||
object o = eval(s.Begin(), main_namespace, main_namespace);
|
||||
Value v = extract<Value>(o);
|
||||
Value v;
|
||||
try
|
||||
{
|
||||
object o = eval(s.Begin(), main_namespace, main_namespace);
|
||||
v = extract<Value>(o);
|
||||
}
|
||||
catch(boost::python::error_already_set const &)
|
||||
{
|
||||
// Parse and output the exception
|
||||
std::string perror_str = parse_py_exception();
|
||||
String es = perror_str;
|
||||
v = ErrorValue(es);
|
||||
}
|
||||
|
||||
evr.SetData(v);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,26 @@
|
|||
|
||||
NAMESPACE_UPP
|
||||
|
||||
Value PyEvalConvert::Format(const Value& q) const
|
||||
{
|
||||
Value v;
|
||||
try
|
||||
{
|
||||
object arg(q);
|
||||
locals["arg"] = arg;
|
||||
object o = eval(expr.Begin(), locals, globals);
|
||||
v = extract<Value>(o);
|
||||
}
|
||||
catch(boost::python::error_already_set const &)
|
||||
{
|
||||
// Parse and output the exception
|
||||
std::string perror_str = parse_py_exception();
|
||||
String es = perror_str;
|
||||
v = ErrorValue(es);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
void export_Core()
|
||||
{
|
||||
ONCELOCK
|
||||
|
|
|
|||
|
|
@ -16,14 +16,7 @@ public:
|
|||
mutable object locals; //added to by Format
|
||||
String expr;
|
||||
|
||||
virtual Value Format(const Value& q) const
|
||||
{
|
||||
object arg(q);
|
||||
locals["arg"] = arg;
|
||||
object o = eval(expr.Begin(), locals, globals);
|
||||
Value v = extract<Value>(o);
|
||||
return v;
|
||||
}
|
||||
virtual Value Format(const Value& q) const;
|
||||
};
|
||||
|
||||
void export_Core();
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
uses
|
||||
CtrlLib,
|
||||
Py,
|
||||
CoreBoostPy;
|
||||
CoreBoostPy,
|
||||
ValueCtrl;
|
||||
|
||||
file
|
||||
Common.h,
|
||||
|
|
|
|||
|
|
@ -3,4 +3,5 @@
|
|||
#include "CtrlLib/init"
|
||||
#include "Py/init"
|
||||
#include "CoreBoostPy/init"
|
||||
#include "ValueCtrl/init"
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -5,5 +5,61 @@ using namespace boost::python;
|
|||
|
||||
NAMESPACE_UPP
|
||||
|
||||
#ifdef flagBOOSTPY
|
||||
// Parses the value of the active python exception
|
||||
// NOTE SHOULD NOT BE CALLED IF NO EXCEPTION
|
||||
std::string parse_py_exception()
|
||||
{
|
||||
PyObject *type_ptr = NULL, *value_ptr = NULL, *traceback_ptr = NULL;
|
||||
// Fetch the exception info from the Python C API
|
||||
PyErr_Fetch(&type_ptr, &value_ptr, &traceback_ptr);
|
||||
|
||||
// Fallback error
|
||||
std::string ret("Unfetchable Python error");
|
||||
|
||||
// If the fetch got a type pointer, parse the type into the exception string
|
||||
if(type_ptr != NULL){
|
||||
handle<> h_type(type_ptr);
|
||||
str type_pstr(h_type);
|
||||
// Extract the string from the boost::python object
|
||||
extract<std::string> e_type_pstr(type_pstr);
|
||||
// If a valid string extraction is available, use it
|
||||
// otherwise use fallback
|
||||
if(e_type_pstr.check())
|
||||
ret = e_type_pstr();
|
||||
else
|
||||
ret = "Unknown exception type";
|
||||
}
|
||||
// Do the same for the exception value (the stringification of the exception)
|
||||
if(value_ptr != NULL){
|
||||
handle<> h_val(value_ptr);
|
||||
str a(h_val);
|
||||
extract<std::string> returned(a);
|
||||
if(returned.check())
|
||||
ret += ": " + returned();
|
||||
else
|
||||
ret += std::string(": Unparseable Python error: ");
|
||||
}
|
||||
// Parse lines from the traceback using the Python traceback module
|
||||
if(traceback_ptr != NULL){
|
||||
handle<> h_tb(traceback_ptr);
|
||||
// Load the traceback module and the format_tb function
|
||||
object tb(import("traceback"));
|
||||
object fmt_tb(tb.attr("format_tb"));
|
||||
// Call format_tb to get a list of traceback strings
|
||||
object tb_list(fmt_tb(h_tb));
|
||||
// Join the traceback strings into a single string
|
||||
object tb_str(str("\n").join(tb_list));
|
||||
// Extract the string, check the extraction, and fallback in necessary
|
||||
extract<std::string> returned(tb_str);
|
||||
if(returned.check())
|
||||
ret += ": " + returned();
|
||||
else
|
||||
ret += std::string(": Unparseable Python traceback");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
END_UPP_NAMESPACE
|
||||
|
||||
|
|
|
|||
|
|
@ -19,4 +19,14 @@
|
|||
|
||||
#include <Core/Core.h>
|
||||
|
||||
NAMESPACE_UPP
|
||||
|
||||
#ifdef flagBOOSTPY
|
||||
// Parses the value of the active python exception
|
||||
// NOTE SHOULD NOT BE CALLED IF NO EXCEPTION
|
||||
std::string parse_py_exception();
|
||||
#endif
|
||||
|
||||
END_UPP_NAMESPACE
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -9,6 +9,3 @@ file
|
|||
ValueAccessorEdit.cpp,
|
||||
ValueAccessorEdit.lay;
|
||||
|
||||
mainconfig
|
||||
"" = "GUI SSE2";
|
||||
|
||||
|
|
|
|||
|
|
@ -209,7 +209,7 @@ void ValueCtrl::OnAction()
|
|||
|
||||
void ValueCtrl::Updated()
|
||||
{
|
||||
if(v.IsNull())
|
||||
if(v.GetType() == VOID_V)
|
||||
SetText("#Nil#");
|
||||
else
|
||||
SetText(v.ToString());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue