[C++-sig] Boost python exception translation failure on BlueGene/P

2010-02-18 Thread James Amundson
Boost Python seems to be failing to catch and translate C++ exceptions 
for me on a BlueGene/P installation. I really don't know what to do next 
in debugging the problem, so I would appreciate any advice.


I have a simple test module, pyexcept

#include 
#include 
#include 


void
foo()
{
std::cout << "in foo, about to raise exception\n";
throw std::runtime_error("runtime_error from foo");
std::cout << "this should never be seen\n";
}

using namespace boost::python;

BOOST_PYTHON_MODULE(pyexcept)
{
def("foo",foo);
}


I test it with the following python script:

#!/usr/bin/env python

import pyexcept

print "about to run pyexcept.foo, catching exception"
try:
pyexcept.foo()
except RuntimeError,e:
print "caught RuntimeError,",e


On my Linux machine I see:

about to run pyexcept.foo, catching exception
in foo, about to raise exception
caught RuntimeError, runtime_error from foo


On the BlueGene/P machine I see:

about to run pyexcept.foo, catching exception
in foo, about to raise exception
terminate called after throwing an instance of 'std::runtime_error'
  what():  runtime_error from foo


I have (cross-)compiled boost myself using the system's installed 
version of the gnu compilers, 4.1.2:


|login2>mpicxx.gnu --version
powerpc-bgp-linux-g++ (GCC) 4.1.2 (BGP)


(The mpicxx.gnu script is a wrapper around the cross-compiling g++.) Has 
anyone seen a problem like this? Any ideas as to how to debug it?


Thanks for any advice.

--Jim Amundson

___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Boost python exception translation failure on BlueGene/P

2010-02-18 Thread Ralf W. Grosse-Kunstleve
Your problem may be that an exception thrown in one .so isn't correctly caught 
in another .so.

In the dark past we had problems like this, too. To this day I don't import 
extensions
directly, but use a small wrapper function, import_ext(). The essential bit is

sys.setdlopenflags(0x100|0x2)

The hex values are platform specific. IIRC it is RTLD_NOW | RTLD_GLOBAL (may be 
backwards).
You want to insert something like this right before importing your extension.


Full code:

http://cci.lbl.gov/cctbx_sources/boost_adaptbx/boost/python.py

Example boilerplate code for imports:

import boost.python
ext = boost.python.import_ext("scitbx_lbfgs_ext")
from scitbx_lbfgs_ext import *
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Boost python exception translation failure on BlueGene/P

2010-02-18 Thread James Amundson

On 02/18/2010 05:19 PM, Ralf W. Grosse-Kunstleve wrote:

Your problem may be that an exception thrown in one .so isn't correctly caught 
in another .so.
   
Hmmm. I only have one module in my test -- pyexcept. Is the fact that it 
has to link to libboost_python enough to cause that problem?

In the dark past we had problems like this, too. To this day I don't import 
extensions
directly, but use a small wrapper function, import_ext(). The essential bit is

 sys.setdlopenflags(0x100|0x2)

The hex values are platform specific. IIRC it is RTLD_NOW | RTLD_GLOBAL (may be 
backwards).
You want to insert something like this right before importing your extension.
   

OK. I tried this:
-
#!/usr/bin/env python
import sys
import dl
sys.setdlopenflags(dl.RTLD_NOW | dl.RTLD_GLOBAL)
import pyexcept

print "about to run pyexcept.foo, catching exception"
try:
pyexcept.foo()
except RuntimeError,e:
print "caught RuntimeError,",e

-

I saw the same behaviour as before -- I never catch the exception.

Thanks for the suggestion, though. It was new to me.

--Jim Amundson
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig