SIGVerse with Python

The aim of this tutorial is to show how to use exception handling to understand errors that occur in the Python code that boost::python handles. We use Python C API to read the information about the error, because the error message shown by boost::python itself is not so helpful.

It will become more clear by the using the following example, in which shall import a fake python module called as "some_module" (it does not exist, we just want to see the information related with error message).

The function for exception handling was taken from the blog written by Joseph Turner.

Please use the following code and name it as "ControllerSample.cpp".

Please set the following environment variables to set the path to boost library.

         export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/pool/lib

Now, execute "make" it will create a ControllerSample.so file.

        make

Please copy the following xml file and name it as "WorldSample.xml"

Please copy the following Makefile file and name it as "Makefile". We shall be using the same Makefile in subsequent examples.

Also set the PYTHONPATH

        export PYTHONPATH=$PYTHONPATH:/path_to_your_workspace

Now, please load the world file into Sigverse and push "Start" Button.

       ./sigverse.sh -w ./WorldSample -p write_your_port_mumber

It will output the following error …which is not-so useful

           terminate called after throwing an instance of 'boost::python::error_already_set'

In short, any errors that occur in the Python code that boost::python handles will cause the library to raise this exception; unfortunately, the exception does not encapsulate any of the information about the error itself. To extract information about the error, we’re going to have to resort to using the Python C API and some Python itself. First, catch the error:

We use the parse_python_exception() which is defined below:

Please add the code snippet shown above and make the executable again. The complete code is given below for quick copy.

Please execute on command line

   $make
   $sigserver.sh -w ./WorldSample.xml -p write_your_port_number

The following information about the error is shown now.

    Error in Python: <type 'exceptions.ImportError'>: No module named   some_module:   File "<string>", line 1, in <module>

As you can see, we can understand from this error that it is related with importing a module, which is certainly more useful from previous error message. From now, on we shall always use this exception handling in our code.

Counter: 2383, today: 1, yesterday: 0

Front page   Edit Freeze Diff Backup Upload Copy Rename Reload   New List of pages Search Recent changes   Help   RSS of recent changes
Last-modified: 2013-12-28 (Sat) 13:35:38 (3762d)