There seems to be a problem with the example on
http://www.boost.org/doc/libs/1_45_0/libs/python/doc/v2/exception_translator.html
when I compile it I get the following message:
--------------------------------------------------------------------------
bradb...@apl-9232eef971d ~/pycppad/boost_py
$ g++  -I/usr/include/python2.6 -g -c exception.cpp
exception.cpp: In function `void translate(const my_exception&)':
exception.cpp:14: error: passing `const my_exception' as `this' argument of `con
st char* my_exception::what()' discards qualifiers
exception.cpp: In function `void something_which_throws()':
exception.cpp:19: error: expected primary-expression before `...' token
exception.cpp:19: error: expected `;' before `...' token
exception.cpp:21: error: expected primary-expression before `...' token
exception.cpp:21: error: expected `;' before `...' token

bradb...@apl-9232eef971d ~/pycppad/boost_py
$ g++ --version
g++ (GCC) 4.3.4 20090804 (release) 1
--------------------------------------------------------------------------

I have figured out a way around this problem, see the attached file exception.sh. This works fine under unix, but under cygwin, the exception does not seem to be caught by the python 'except' section of the 'try' block.

--------------------------------------------------------------------------
Here is the result of running exception.sh under unix:

[bradb...@localhost trash]$ ./exception.sh
cat << EOF > exception.cpp
g++  -I/usr/include/python2.7 -g -c exception.cpp
g++ -shared  \
    -g \
    exception.o \
    -L/usr/lib -L/usr/lib/python2.7/config  \
    -lboost_python-mt -lpython2.7 \
    -o exception.so
cat << EOF > exception.py
exception.sh: OK
...
--------------------------------------------------------------------------
Here is the result of running under cygwin:

$ ./exception.sh
cat << EOF > exception.cpp
g++  -I/usr/include/python2.6 -g -c exception.cpp
g++ -shared -Wl,--enable-auto-image-base \
        -g \
        exception.o \
        -L/usr/lib -L/usr/lib/python2.6/config  \
        -lboost_python-mt -lpython2.6 \
        -o exception.dll
cat << EOF > exception.py
terminate called after throwing an instance of 'my_exception'
  what():  my error message
./exception.sh: line 97: 2108 Aborted (core dumped) python exception.py
exception.sh: Error

# /bin/bash
set -e
# Modified version of http://www.boost.org/doc/libs/1_45_0/libs/python/doc/v2/
#       exception_translator.html
# ----------------------------------------------------------------------
python_version=`ls /usr/include | grep python | sed -e 's/python//'` 
system=`uname | sed -e 's/\(......\).*/\1/'`
if [ "$system" == "CYGWIN" ]
then
        extra_compile="-Wl,--enable-auto-image-base"
        library_extension=".dll"
else
        extra_compile=""
        library_extension=".so"
fi
# ----------------------------------------------------------------------
#
echo "cat << EOF > exception.cpp"
cat << EOF > exception.cpp
# include <boost/python/module.hpp>
# include <boost/python/def.hpp>
# include <boost/python/exception_translator.hpp>
# include <exception>
# include <string>

class my_exception : public std::exception
{       
private : 
        char message_[201];
public :
        my_exception(const char* message)
        {       strncpy(message_, message, 200);
                message_[200] = '\0';
        }
        const char* what(void) const throw()
        {       return message_; }

};

void translate(my_exception const& e)
{
    // Use the Python 'C' API to set up an exception object
    PyErr_SetString(PyExc_RuntimeError, e.what());
}

void something_which_throws()
{
    throw my_exception("my error message");
}

BOOST_PYTHON_MODULE(exception)
{
  boost::python::register_exception_translator<my_exception>(&translate);
  
  boost::python::def("something_which_throws", something_which_throws);
}
EOF
#
echo "g++  -I/usr/include/python$python_version -g -c exception.cpp"
g++  -I/usr/include/python$python_version -g -c exception.cpp 
#
echo "g++ -shared $extra_compile \\"
echo "  -g \\"
echo "  exception.o \\"
echo "  -L/usr/lib -L/usr/lib/python$python_version/config  \\"
echo "  -lboost_python-mt -lpython$python_version \\"
echo "  -o exception$library_extension"
g++ -shared $extra_compile \
        -g \
        exception.o \
        -L/usr/lib -L/usr/lib/python$python_version/config \
        -lboost_python-mt -lpython$python_version \
        -o exception$library_extension
#
echo "cat << EOF > exception.py"
cat << EOF > exception.py
from sys import exit
import exception
try :
        exception.something_which_throws()
except RuntimeError :
        exit(0) 
exit(1)
EOF
if python exception.py
then
        flag=0
        echo "exception.sh: OK"
        for ext in .cpp .o $library_extension .py
        do
                echo "rm exception$ext"
                rm exception$ext
        done
else
        echo "exception.sh: Error"
        flag=1
fi
exit $flag
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to