[C++-sig] boost.python call_method+thread => wrong exceptionhandling?

2011-05-20 Thread Nox 7Bitfaster


Hi,
Im currently working with boost.python and stumpled into a strang behavior. If 
I call a not existing method with call_method from the main thread everything 
works as excepted (exception is raised and handled in python) but if I call it 
from a seperate thread call_method does not raise a c++ runtime exception and 
the hole program crashes. Both cases can be switched by exchanging do_file1 
with do_file2 in the testcode. Has someone an idea what I am doing wrong?
Minimal testcase: http://codepad.org/7OMMBZGQ 
Tested with: boost 1.46.1 (statically linked) and python 2.6.5 under windows 
vista 32 bit
Or as a plain code:
#include 
#include "boost/thread.hpp"
#include "boost/python.hpp"
using boost::noncopyable;
using namespace boost::python;
struct Interface
{
PyObject *self;
public:
Interface(PyObject* s) : self(s) {}
static void Thread(Interface* obj)
{
obj->func();
}
void start_thread(void)
{
boost::thread::thread(Interface::Thread, this);
}
void func(void)
{
PyGILState_STATE gstate = PyGILState_Ensure();
call_method(self, "not_existing_function_should_raise_an_exception");
PyGILState_Release(gstate);
}
};
// specialize has_back_reference for Entity
namespace boost { namespace python
{
template <> struct has_back_reference : mpl::true_ {};
}}

BOOST_PYTHON_MODULE(test)
{
class_("Interface")
.def("start_thread", &Interface::start_thread)
.def("func", &Interface::func)
;
}
int main( int argc, char ** argv )
{
PyImport_AppendInittab( "test", &inittest );
Py_Initialize();
PyEval_InitThreads();
const char* do_file1 = "import testnobj = test.Interface()nobj.func()"; 
//raises the correct exception
const char* do_file2 = "import testnobj = 
test.Interface()nobj.start_thread()nwhile 1: pass"; //raises no exception at all
PyRun_SimpleString(do_file2);
return 0;
}
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] boost.python call_method+thread => wrong exceptionhandling?

2011-05-20 Thread Ralf W. Grosse-Kunstleve
A very superficial remark: the boost/python.hpp include has to appear first, 
before any other includes (this goes back to a Python.h requirement, which has 
to appear before any system include).
I only give a small chance of fixing your problem, but it will also only take a 
few seconds to try out...
Ralf




>
>From: Nox 7Bitfaster 
>To: [email protected]
>Sent: Friday, May 20, 2011 1:11 AM
>Subject: [C++-sig] boost.python call_method+thread => wrong exceptionhandling?
>
>
>Message
>
>
>Hi,
>Im currently working with boost.python and stumpled into a strang behavior. 
>If I call a not existing method with call_method from the main thread 
>everything works as excepted (exception is raised and handled in python) but 
>if I call it from a seperate thread call_method does not raise a c++ runtime 
>exception and the hole program crashes. Both cases can be switched by 
>exchanging do_file1 with do_file2 in the testcode. Has someone an idea what I 
>am doing wrong?
>Minimal testcase: http://codepad.org/7OMMBZGQ 
>Tested with: boost 1.46.1 (statically linked) and python 2.6.5 under windows 
>vista 32 bit
>Or as a plain code:
>#include
>#include"boost/thread.hpp"
>#include"boost/python.hpp"
>usingboost::noncopyable;
>usingnamespaceboost::python;
>structInterface{
>PyObject *self;
>public:Interface(PyObject* s) : self(s) {}
>staticvoidThread(Interface* obj){
>obj->func();
>}
>voidstart_thread(void){
>boost::thread::thread(Interface::Thread, this);}
>voidfunc(void){
>PyGILState_STATE gstate = PyGILState_Ensure();
>call_method(self, 
>"not_existing_function_should_raise_an_exception");PyGILState_Release(gstate);
>}
>};// specialize has_back_reference for Entity
>namespaceboost { namespacepython{
>template<> structhas_back_reference : mpl::true_ {};}}
>BOOST_PYTHON_MODULE(test)
>{
>class_("Interface")
>.def("start_thread", &Interface::start_thread)
>.def("func", &Interface::func);
>}
>intmain( intargc, char** argv ){
>PyImport_AppendInittab( "test", &inittest );Py_Initialize();
>PyEval_InitThreads();
>constchar* do_file1 = "import testnobj = test.Interface()nobj.func()"; 
>//raises the correct exception
>constchar* do_file2 = "import testnobj = 
>test.Interface()nobj.start_thread()nwhile 1: pass"; //raises no exception at 
>allPyRun_SimpleString(do_file2);
>return0;}
>
>___
>Cplusplus-sig mailing list
>[email protected]
>http://mail.python.org/mailman/listinfo/cplusplus-sig
>
>___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig