   Boost python help
  A) Can anyone tell me what's wrong with my code. Program runs fine, no crash. But "object data = import("data");" causes an exception and python code doesn't run.B) Can anyone tell me how to properly display what's wrong in my catch block? My code below never calls the messagebox(last line of catch block) that would tell me the error.Thx ahead of time.CallPython() in WM_CREATE of main windowvoid MsgBox() { ::MessageBox(NULL,NULL, NULL, MB_OK);};BOOST_PYTHON_MODULE(Msg){def("MsgBox",MsgBox); }class PyInitializer{public:  PyInitializer()  { Py_Initialize(); }  ~PyInitializer() { Py_Finalize(); }private:  PyInitializer(const PyInitializer &);  PyInitializer & operator=(const PyInitializer &);};void CallPython(){::MessageBox(NULL, L"call python",NULL, MB_OK);PyInitializer py;try{object main_module((handle<>(borrowed(PyImport_AddModule("__main__")))));object main_namespace = main_module.attr("__dict__");PyImport_AppendInittab("Msg", &initMsg );object data = import("data");object o_getdata = extract<object>(data.attr("getdata"));o_getdata();::MessageBox(NULL, L"end call python",NULL, MB_OK);}catch (boost::python::error_already_set){::MessageBox(NULL, L"error",NULL, MB_OK);PyErr_Print();boost::python::object sys(boost::python::handle<>(PyImport_ImportModule("sys")));boost::python::object err = sys.attr("stderr");std::wstring err_text = boost::python::extract<std::wstring>(err.attr("getvalue")());MessageBox(0, err_text.c_str(), L"Python Error", MB_OK);}};data.pyimport Msgdef getdata():Msg.MsgBox()return [Edited by - Jacob Jingle on May 30, 2010 2:56:44 PM]
  In your python file you have:def getdata(object hwnd):when you probably meantdef getdata():since you try calling it without any arguments. As for getting error information, the code you've posted looks suspiciously like the code I posted in  this thread where you've only copied the exception handling part, but didn't copy the set up portion. In order for it to work you need to set sys.stderr to a string IO object, which is what the commands in the try block are intended to do.   ;Quote:Original post by SiCraneIn your python file you have:def getdata(object hwnd):when you probably meantdef getdata():Yeah that was actually a typo. Sry.This is actually a simple kiss attempt to find out what is going wrong with other code that was working and for some reason(didn't alter code) just stopped. I dunno, I'm beginning to wonder if it might be VC and maybe a simple reboot would fix it. (Sadly I can't reboot right now)Quote:Original post by SiCraneAs for getting error information, the code you've posted looks suspiciously like the code I posted in  this thread where you've only copied the exception handling part, but didn't copy the set up portion. In order for it to work you need to set sys.stderr to a string IO object, which is what the commands in the try block are intended to do.Doah! Thx. I will work that in and see what I get.Note: It was your code. You seemed like you knew what you were doing, because you clearly do, so I went back and look at your other post on python. Clearly I didn't read that one closely enough.;  Rebooted and it didn't fix it. Turned it completely off and then on. Fixed it. <Weird> Oh well.Massive thx for error code SiCrane(can't say it enough). Works like a charm.Jacob Jingle   ;  Note that python function arguments are untyped. It's not valid python to dodef foo(int bar, float baz):. Instead you'd just do def foo(bar, baz):and make certain that your calling convention is that bar is an int and baz is a float. There are some variants of Python that are typed; Cython is a notable example (note that I didn't say CPython). However, these aren't "real" Python.   ;Writer and designer Chuck Jordan (Sam &  Max series) delves subtly into the world of game narrative and explains why the myth that narrative and gameplay are incompatible is so wrong, by using the language of horror film.  View the full article