Error Handling
[
Front page
] [
New
|
List of pages
|
Search
|
Recent changes
|
Help
]
Start:
[[SIGVerse with Python]]
The aim of this tutorial is to show how to use exception ...
It will become more clear by the using the following exam...
The function for exception handling was taken from the [[...
Please use the following code and name it as "ControllerS...
#highlight(c){{
#include "ControllerEvent.h"
#include "Controller.h"
#include "Logger.h"
#include <iostream>
#include <boost/python.hpp>
#include <Python.h>
#include <dlfcn.h>
namespace py = boost::python; // create namespace variabl...
using namespace std;
class MyController : public Controller {
public:
void onInit(InitEvent &evt);
double onAction(ActionEvent&);
void onRecvMsg(RecvMsgEvent &evt);
void onCollision(CollisionEvent &evt);
};
void MyController::onInit(InitEvent &evt) {
SimObj *my = getObj(myname());
dlopen("libpython2.7.so", RTLD_LAZY | RTLD_GLOBAL);
Py_Initialize(); //initialization of the python interp...
}
double MyController::onAction(ActionEvent &evt) {
double x=2.0;
Py_Initialize();
py::object main_module = py::import("__main__");
// load the dictionary object out of the main mod...
py::object main_namespace = main_module.attr("__d...
// run simple code within the main namespace usin...
// importing a fake module
py::exec("import some_module", main_namespace);
return 1.0;
}
void MyController::onRecvMsg(RecvMsgEvent &evt) {
}
void MyController::onCollision(CollisionEvent &evt) {
}
extern "C" Controller * createController() {
return new MyController;
}
}}
Please set the following environment variables to set the...
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/p...
Now, execute "make" it will create a ControllerSample.so ...
make
Please copy the following xml file and name it as "WorldS...
#highlight(xml){{
<?xml version="1.0" encoding="utf8"?>
<world name="myworld1">
<gravity x="0.0" y="-980.7" z="0.0"/>
<instanciate class="seToy_D.xml">
<set-attr-value name="name" value="toy_D"/>
<set-attr-value name="language" value="c++"/>
<set-attr-value name="implementation"
value="./ControllerSample.so"/>
<set-attr-value name="dynamics" value="true"/>
<set-attr-value name="x" value="0.0"/>
<set-attr-value name="y" value="60.0"/>
<set-attr-value name="z" value="0.0"/>
<set-attr-value name="mass" value="1.0"/>
<set-attr-value name="collision" value="true"/>
</instanciate>
</world>
}}
Please copy the following Makefile file and name it as "M...
#highlight(c){{
#sigverse header
SIG_SRC = $(SIGVERSE_PATH)/include/sigverse
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/raghav/pool...
CC = gcc
CPP = g++
AS = as
LD = g++
AR = ar
RANLIB = ranlib
OBJCOPY = objcopy
# external libraries and headers
# change the next line to point to the location of your b...
EXTERN_DIR = /home/raghav/pool
EXTERN_LIBDIR = $(EXTERN_DIR)/lib
EXTERN_INCDIR = $(EXTERN_DIR)/include
EXTERN_BINDIR = $(EXTERN_DIR)/bin
BOOST_PYTHON_LIB = $(EXTERN_LIBDIR)/libboost_python.a
INCDIRS = .
INCDIRS += $(EXTERN_INCDIR)
# you may also need to change this directory if you want ...
# python version
INCDIRS += /usr/include/python2.7
INCDIRS += /usr/lib/python2.7/dist-packages/numpy/core/in...
INCDIR = $(foreach dir, $(INCDIRS), -I$(dir))
%.o: %.cpp
$(CPP) $(INCDIR) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
#specifying of object file
OBJS = ControllerSample.so
ControllerSample.so: ControllerSample.cpp
g++ -DCONTROLLER -DNDEBUG -DUSE_ODE -DdDOUBLE -I$(S...
#$^ , $+ The names of all the prerequisites, with spa...
# while $+ retains them and preserves their order
clean:
rm -rf *.so *.o $(HWOBJS) hello_world
}}
Also set the PYTHONPATH
export PYTHONPATH=$PYTHONPATH:/path_to_your_work...
Now, please load the world file into Sigverse and push "S...
./sigverse.sh -w ./WorldSample -p write_your_port...
It will output the following error …which is not-so useful
terminate called after throwing an instance o...
In short, any errors that occur in the Python code that b...
#highlight(c){{
try{
py::object main_module = py::import("__main__");
// load the dictionary object out of the main mod...
py::object main_namespace = main_module.attr("__d...
// run simple code within the main namespace usin...
// of course, you can also import functionality f...
py::exec("import some_module", main_namespace);
}
catch(boost::python::error_already_set const &){
// Parse and output the exception
std::string perror_str = parse_python_exception();
std::cout << "Error in Python: " << perror_str <<...
}
}}
We use the parse_python_exception() which is defined bel...
#highlight(c){{
std::string parse_python_exception(){
PyObject *type_ptr = NULL, *value_ptr = NULL, *traceb...
// 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 in...
if(type_ptr != NULL){
py::handle<> h_type(type_ptr);
py::str type_pstr(h_type);
// Extract the string from the boost::python object
py::extract<std::string> e_type_pstr(type_pstr);
// If a valid string extraction is available, use...
// 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 stringifi...
if(value_ptr != NULL){
py::handle<> h_val(value_ptr);
py::str a(h_val);
py::extract<std::string> returned(a);
if(returned.check())
ret += ": " + returned();
else
ret += std::string(": Unparseable Python erro...
}
// Parse lines from the traceback using the Python tr...
if(traceback_ptr != NULL){
py::handle<> h_tb(traceback_ptr);
// Load the traceback module and the format_tb fu...
py::object tb(py::import("traceback"));
py::object fmt_tb(tb.attr("format_tb"));
// Call format_tb to get a list of traceback stri...
py::object tb_list(fmt_tb(h_tb));
// Join the traceback strings into a single string
py::object tb_str(py::str("\n").join(tb_list));
// Extract the string, check the extraction, and ...
py::extract<std::string> returned(tb_str);
if(returned.check())
ret += ": " + returned();
else
ret += std::string(": Unparseable Python trac...
}
return ret;
}
}}
Please add the code snippet shown above and make the exec...
#highlight(c){{
#include "ControllerEvent.h"
#include "Controller.h"
#include "Logger.h"
#include <iostream>
#include <boost/python.hpp>
#include <Python.h>
#include <dlfcn.h>
namespace py = boost::python; // create namespace variabl...
using namespace std;
std::string parse_python_exception(); // functional decla...
class MyController : public Controller {
public:
void onInit(InitEvent &evt);
double onAction(ActionEvent&);
void onRecvMsg(RecvMsgEvent &evt);
void onCollision(CollisionEvent &evt);
};
void MyController::onInit(InitEvent &evt) {
SimObj *my = getObj(myname());
dlopen("libpython2.7.so", RTLD_LAZY | RTLD_GLOBAL);
Py_Initialize(); //initialization of the python interp...
}
double MyController::onAction(ActionEvent &evt) {
double x=2.0;
Py_Initialize();
try{
py::object main_module = py::import("__main__");
// load the dictionary object out of the main mod...
py::object main_namespace = main_module.attr("__d...
// run simple code within the main namespace usin...
// of course, you can also import functionality f...
py::exec("import some_module", main_namespace);
}
catch(boost::python::error_already_set const &){
// Parse and output the exception
std::string perror_str = parse_python_exception();
std::cout << "Error in Python: " << perror_str <<...
}
return 1.0;
}
void MyController::onRecvMsg(RecvMsgEvent &evt) {
}
void MyController::onCollision(CollisionEvent &evt) {
}
extern "C" Controller * createController() {
return new MyController;
}
std::string parse_python_exception(){
PyObject *type_ptr = NULL, *value_ptr = NULL, *traceb...
// 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 in...
if(type_ptr != NULL){
py::handle<> h_type(type_ptr);
py::str type_pstr(h_type);
// Extract the string from the boost::python object
py::extract<std::string> e_type_pstr(type_pstr);
// If a valid string extraction is available, use...
// 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 stringifi...
if(value_ptr != NULL){
py::handle<> h_val(value_ptr);
py::str a(h_val);
py::extract<std::string> returned(a);
if(returned.check())
ret += ": " + returned();
else
ret += std::string(": Unparseable Python erro...
}
// Parse lines from the traceback using the Python tr...
if(traceback_ptr != NULL){
py::handle<> h_tb(traceback_ptr);
// Load the traceback module and the format_tb fu...
py::object tb(py::import("traceback"));
py::object fmt_tb(tb.attr("format_tb"));
// Call format_tb to get a list of traceback stri...
py::object tb_list(fmt_tb(h_tb));
// Join the traceback strings into a single string
py::object tb_str(py::str("\n").join(tb_list));
// Extract the string, check the extraction, and ...
py::extract<std::string> returned(tb_str);
if(returned.check())
ret += ": " + returned();
else
ret += std::string(": Unparseable Python trac...
}
return ret;
}
}}
Please execute on command line
$make
$sigserver.sh -w ./WorldSample.xml -p write_your_port...
The following information about the error is shown now.
Error in Python: <type 'exceptions.ImportError'>: No...
As you can see, we can understand from this error that it...
#highlight(end)
#counter
End:
[[SIGVerse with Python]]
The aim of this tutorial is to show how to use exception ...
It will become more clear by the using the following exam...
The function for exception handling was taken from the [[...
Please use the following code and name it as "ControllerS...
#highlight(c){{
#include "ControllerEvent.h"
#include "Controller.h"
#include "Logger.h"
#include <iostream>
#include <boost/python.hpp>
#include <Python.h>
#include <dlfcn.h>
namespace py = boost::python; // create namespace variabl...
using namespace std;
class MyController : public Controller {
public:
void onInit(InitEvent &evt);
double onAction(ActionEvent&);
void onRecvMsg(RecvMsgEvent &evt);
void onCollision(CollisionEvent &evt);
};
void MyController::onInit(InitEvent &evt) {
SimObj *my = getObj(myname());
dlopen("libpython2.7.so", RTLD_LAZY | RTLD_GLOBAL);
Py_Initialize(); //initialization of the python interp...
}
double MyController::onAction(ActionEvent &evt) {
double x=2.0;
Py_Initialize();
py::object main_module = py::import("__main__");
// load the dictionary object out of the main mod...
py::object main_namespace = main_module.attr("__d...
// run simple code within the main namespace usin...
// importing a fake module
py::exec("import some_module", main_namespace);
return 1.0;
}
void MyController::onRecvMsg(RecvMsgEvent &evt) {
}
void MyController::onCollision(CollisionEvent &evt) {
}
extern "C" Controller * createController() {
return new MyController;
}
}}
Please set the following environment variables to set the...
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/p...
Now, execute "make" it will create a ControllerSample.so ...
make
Please copy the following xml file and name it as "WorldS...
#highlight(xml){{
<?xml version="1.0" encoding="utf8"?>
<world name="myworld1">
<gravity x="0.0" y="-980.7" z="0.0"/>
<instanciate class="seToy_D.xml">
<set-attr-value name="name" value="toy_D"/>
<set-attr-value name="language" value="c++"/>
<set-attr-value name="implementation"
value="./ControllerSample.so"/>
<set-attr-value name="dynamics" value="true"/>
<set-attr-value name="x" value="0.0"/>
<set-attr-value name="y" value="60.0"/>
<set-attr-value name="z" value="0.0"/>
<set-attr-value name="mass" value="1.0"/>
<set-attr-value name="collision" value="true"/>
</instanciate>
</world>
}}
Please copy the following Makefile file and name it as "M...
#highlight(c){{
#sigverse header
SIG_SRC = $(SIGVERSE_PATH)/include/sigverse
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/raghav/pool...
CC = gcc
CPP = g++
AS = as
LD = g++
AR = ar
RANLIB = ranlib
OBJCOPY = objcopy
# external libraries and headers
# change the next line to point to the location of your b...
EXTERN_DIR = /home/raghav/pool
EXTERN_LIBDIR = $(EXTERN_DIR)/lib
EXTERN_INCDIR = $(EXTERN_DIR)/include
EXTERN_BINDIR = $(EXTERN_DIR)/bin
BOOST_PYTHON_LIB = $(EXTERN_LIBDIR)/libboost_python.a
INCDIRS = .
INCDIRS += $(EXTERN_INCDIR)
# you may also need to change this directory if you want ...
# python version
INCDIRS += /usr/include/python2.7
INCDIRS += /usr/lib/python2.7/dist-packages/numpy/core/in...
INCDIR = $(foreach dir, $(INCDIRS), -I$(dir))
%.o: %.cpp
$(CPP) $(INCDIR) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
#specifying of object file
OBJS = ControllerSample.so
ControllerSample.so: ControllerSample.cpp
g++ -DCONTROLLER -DNDEBUG -DUSE_ODE -DdDOUBLE -I$(S...
#$^ , $+ The names of all the prerequisites, with spa...
# while $+ retains them and preserves their order
clean:
rm -rf *.so *.o $(HWOBJS) hello_world
}}
Also set the PYTHONPATH
export PYTHONPATH=$PYTHONPATH:/path_to_your_work...
Now, please load the world file into Sigverse and push "S...
./sigverse.sh -w ./WorldSample -p write_your_port...
It will output the following error …which is not-so useful
terminate called after throwing an instance o...
In short, any errors that occur in the Python code that b...
#highlight(c){{
try{
py::object main_module = py::import("__main__");
// load the dictionary object out of the main mod...
py::object main_namespace = main_module.attr("__d...
// run simple code within the main namespace usin...
// of course, you can also import functionality f...
py::exec("import some_module", main_namespace);
}
catch(boost::python::error_already_set const &){
// Parse and output the exception
std::string perror_str = parse_python_exception();
std::cout << "Error in Python: " << perror_str <<...
}
}}
We use the parse_python_exception() which is defined bel...
#highlight(c){{
std::string parse_python_exception(){
PyObject *type_ptr = NULL, *value_ptr = NULL, *traceb...
// 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 in...
if(type_ptr != NULL){
py::handle<> h_type(type_ptr);
py::str type_pstr(h_type);
// Extract the string from the boost::python object
py::extract<std::string> e_type_pstr(type_pstr);
// If a valid string extraction is available, use...
// 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 stringifi...
if(value_ptr != NULL){
py::handle<> h_val(value_ptr);
py::str a(h_val);
py::extract<std::string> returned(a);
if(returned.check())
ret += ": " + returned();
else
ret += std::string(": Unparseable Python erro...
}
// Parse lines from the traceback using the Python tr...
if(traceback_ptr != NULL){
py::handle<> h_tb(traceback_ptr);
// Load the traceback module and the format_tb fu...
py::object tb(py::import("traceback"));
py::object fmt_tb(tb.attr("format_tb"));
// Call format_tb to get a list of traceback stri...
py::object tb_list(fmt_tb(h_tb));
// Join the traceback strings into a single string
py::object tb_str(py::str("\n").join(tb_list));
// Extract the string, check the extraction, and ...
py::extract<std::string> returned(tb_str);
if(returned.check())
ret += ": " + returned();
else
ret += std::string(": Unparseable Python trac...
}
return ret;
}
}}
Please add the code snippet shown above and make the exec...
#highlight(c){{
#include "ControllerEvent.h"
#include "Controller.h"
#include "Logger.h"
#include <iostream>
#include <boost/python.hpp>
#include <Python.h>
#include <dlfcn.h>
namespace py = boost::python; // create namespace variabl...
using namespace std;
std::string parse_python_exception(); // functional decla...
class MyController : public Controller {
public:
void onInit(InitEvent &evt);
double onAction(ActionEvent&);
void onRecvMsg(RecvMsgEvent &evt);
void onCollision(CollisionEvent &evt);
};
void MyController::onInit(InitEvent &evt) {
SimObj *my = getObj(myname());
dlopen("libpython2.7.so", RTLD_LAZY | RTLD_GLOBAL);
Py_Initialize(); //initialization of the python interp...
}
double MyController::onAction(ActionEvent &evt) {
double x=2.0;
Py_Initialize();
try{
py::object main_module = py::import("__main__");
// load the dictionary object out of the main mod...
py::object main_namespace = main_module.attr("__d...
// run simple code within the main namespace usin...
// of course, you can also import functionality f...
py::exec("import some_module", main_namespace);
}
catch(boost::python::error_already_set const &){
// Parse and output the exception
std::string perror_str = parse_python_exception();
std::cout << "Error in Python: " << perror_str <<...
}
return 1.0;
}
void MyController::onRecvMsg(RecvMsgEvent &evt) {
}
void MyController::onCollision(CollisionEvent &evt) {
}
extern "C" Controller * createController() {
return new MyController;
}
std::string parse_python_exception(){
PyObject *type_ptr = NULL, *value_ptr = NULL, *traceb...
// 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 in...
if(type_ptr != NULL){
py::handle<> h_type(type_ptr);
py::str type_pstr(h_type);
// Extract the string from the boost::python object
py::extract<std::string> e_type_pstr(type_pstr);
// If a valid string extraction is available, use...
// 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 stringifi...
if(value_ptr != NULL){
py::handle<> h_val(value_ptr);
py::str a(h_val);
py::extract<std::string> returned(a);
if(returned.check())
ret += ": " + returned();
else
ret += std::string(": Unparseable Python erro...
}
// Parse lines from the traceback using the Python tr...
if(traceback_ptr != NULL){
py::handle<> h_tb(traceback_ptr);
// Load the traceback module and the format_tb fu...
py::object tb(py::import("traceback"));
py::object fmt_tb(tb.attr("format_tb"));
// Call format_tb to get a list of traceback stri...
py::object tb_list(fmt_tb(h_tb));
// Join the traceback strings into a single string
py::object tb_str(py::str("\n").join(tb_list));
// Extract the string, check the extraction, and ...
py::extract<std::string> returned(tb_str);
if(returned.check())
ret += ": " + returned();
else
ret += std::string(": Unparseable Python trac...
}
return ret;
}
}}
Please execute on command line
$make
$sigserver.sh -w ./WorldSample.xml -p write_your_port...
The following information about the error is shown now.
Error in Python: <type 'exceptions.ImportError'>: No...
As you can see, we can understand from this error that it...
#highlight(end)
#counter
Page: