From: Jadhav, A. <alo...@cr...> - 2009-12-08 09:26:13
|
I used SWIG_NewPointerObj method to create a PyObject. It removed all my exceptions and seems to be working fine. [code] PyObject *argobj = SWIG_NewPointerObj(SWIG_as_voidptr(&msg2), SWIGTYPE_p_RFAMessageWrapper, SWIG_POINTER_NEW | 0 ); try { arglist = Py_BuildValue("(O)",argobj); // Build argument list PyEval_CallObject(func,arglist); // Call Python } catch (...) { cout<<"Unknown exception..."<<endl; } [\code] Callback is called properly. But now, in Python code Access to the passed object raises exception. I am expecting RFAMessageWrapper object in python code. But using SWIG_NewPointerObj passes SwigPyObject to python instead. Python call back function is [code] def callback_fun(msg): try: print "RFAMessage received for service:"+msg.service except Exception: print "Exception handled" print type(msg) traceback.print_exc(file=sys.stdout) [/code] Result: [code] Exception handled <type 'SwigPyObject'> Traceback (most recent call last): File "<stdin>", line 3, in callback_fun AttributeError: 'SwigPyObject' object has no attribute 'service' [/code] I am expecting the object type as RFAMessageWrapper here. Even when the PyObject created in C code uses SWIGTYPE_p_RFAMessageWrapper as the typeinfo, still the object created is not RFAMessageWrapper. What should I pass to callback PyEval_CallObject(func,arglist); so that I can access RFAMessageWrapper object in Python code. RFAMessageWrapper class is wrapped using SWIG properly. Any help is appreciated. Regards, Alok ________________________________ From: Jadhav, Alok Sent: Tuesday, December 08, 2009 9:42 AM To: Jadhav, Alok; 'Brian Cole' Cc: 'swi...@li...' Subject: RE: [Swig-user] Python C++ extension (multi threaded environment) (Somethign useful ? ) Someone from Python-C++ mailing list told me that the way I create argument list is incorrect. They did not have knowledge of SWIG so they couldn't help more. In the function below RFAMessageWrapper msg2(msg); try { arglist = Py_BuildValue("(O)",msg2); // Build argument list //arglist = Py_BuildValue("(i)", blah); PyEval_CallObject(func,arglist); // Call Python } catch (...) { cout<<"Unknown exception..."<<endl; } The code arglist = Py_BuildValue("(O)",msg2); Here I create msg2 object as RFAMessageWrapper msg2(msg); which is on stack. Other person told me that this is incorrect. I should create this object as PyObject and then call Py_BuildValue function. It should be created using PyObject_New. Since RFAMessageWrapper is already wrapped using SWIG, I believe there should already be a function which would help me in creating a new PyObject for RFAMessageWrapper. Is this correct? Can someone tell me which function I should use to create a new PyObject type object for RFAMessageWrapper? Regards, Alok ________________________________ From: Jadhav, Alok Sent: Tuesday, December 08, 2009 8:21 AM To: Jadhav, Alok; Brian Cole Cc: swi...@li... Subject: RE: [Swig-user] Python C++ extension (multi threaded environment) I still don't have any progress. Funny thing is that, I am not even able to catch this exception !!!! I put a try catch block in C code like below RFAMessageWrapper msg2(msg); try { arglist = Py_BuildValue("(O)",msg2); // Build argument list //arglist = Py_BuildValue("(i)", blah); PyEval_CallObject(func,arglist); // Call Python } catch (...) { cout<<"Unknown exception..."<<endl; } While debugging, PyEval_CallObject(func,arglist); is executed and it raises exception but it is not cought in this code. I put a try catch block in my python callback function as well ... def callback_fun(msg): try: print "RFAMessage received for service:"+msg.service except Exception: print "Exception handled" Here again exception is not handled. msg.service accesss raises Access violation error. The memory at some address could not be read. Any access to msg object (of type RFAMessageWrapper) which is passed from C++ code raises this exception. It doesn't occurs for basic data types. I wonder what I am missing here. Regards, Alok ________________________________ From: Jadhav, Alok Sent: Monday, December 07, 2009 11:02 AM To: Brian Cole Cc: swi...@li... Subject: Re: [Swig-user] Python C++ extension (multi threaded environment) In the example below, the example of int is working fine. The callback is called properly in Python and the int value is displayed. All good. But when I pass the object of type RFAMessageWrapper, even type(msg) raises an exception. I am not able to send user defined c++ object to Python. Not sure what is wrong here. Also the usage of SWIG_PYTHON_THREAD_BEGIN_BLOCK; SWIG_PYTHON_THREAD_END_BLOCK; doesn't raise any exception. But I wonder if it succeeds in acquiring the GIL. Please advice. Regards, Alok ________________________________ From: Jadhav, Alok Sent: Saturday, December 05, 2009 12:32 PM To: Brian Cole Cc: swi...@li... Subject: Re: [Swig-user] Python C++ extension (multi threaded environment) I will try to use that but before using the SWIG macros I should be able to build the arguments list from the C objects. arglist = Py_BuildValue("(O)",msg2); // Build argument list Above line raises an Access violation exception. msg2 is non nul C class object. where as int blah =1; arglist = Py_BuildValue("i",blah); // Build argument list this line doesn't raise any exception. So it means it has to do with creating a PyObject from C object. (Works fine for basic datatypes.) What is the issue in this case? How can this be solved? Regards, Alok ________________________________ From: Brian Cole [mailto:co...@ey...] Sent: Saturday, December 05, 2009 3:52 AM To: Jadhav, Alok Cc: swi...@li... Subject: Re: [Swig-user] Python C++ extension (multi threaded environment) This is a shot in the dark, but have you tried using SWIG's macros for acquiring and releasing the GIL? SWIG_PYTHON_THREAD_BEGIN_BLOCK; SWIG_PYTHON_THREAD_END_BLOCK; -Brian On Dec 3, 2009, at 11:57 PM, Jadhav, Alok wrote: Guys, I created a new wrapper class RFAMesageWrapper which is defined in SWIG interface file. Now the new code looks like RFAMessageWrapper msg2(msg); arglist = Py_BuildValue("(O)",msg2); // Build argument list Where as msg2 is identified by Python. Still this function raises access violation exception. So my thinking that it was because RFAMessage is not identified as RFAMessage was incorrect. Now back to square one. . What is wrong here? Why does this line of code raises exception? Still couldn't identify why state = PyGILState_Ensure(); blocks the thread. Regards, Alok -----Original Message----- From: Jadhav, Alok Sent: Friday, December 04, 2009 3:05 PM To: swi...@li... Subject: [Swig-user] Python C++ extension (multi threaded environment) Hello, I have made some progress in using C library to call from Python. In next step I have to register a callback function so that C can call ProcessMessage() function from which is written in python. My code is not working properly and I have couple of questions regarding this. Backgroun ---------- Python creates RFAClient object. This happens in Python main thread. Now Python calls a function to start postoffice. Postoffice spawns a new C++ thread and it runs in a while loop listening to some data. Now when a message arrives on this thread it should call Python function to process this data. Looks simple .. But multi threaded environment, C++ and python all together makes it a bit messy :) Issues ------ My callback registed in C looks like this static void PythonCallBack(RFAMessage msg, void *clientdata) { PyObject *func, *arglist; PyGILState_STATE state; state = PyGILState_Ensure(); func = (PyObject *) clientdata; // Get Python function arglist = Py_BuildValue("(O)",msg); // Build argument list PyEval_CallObject(func,arglist); // Call Python Py_DECREF(arglist); // Trash arglist PyGILState_Release(state); } Here 2 lines are giving me exceptions.. (Remember this is called from postoffice thread.. which is not the main Python thread. .. I have called PyEval_InitThreads() function; at the start of main thread) -> arglist = Py_BuildValue("(O)",msg); - Raises Access Violation error. I have no idea why this is occuring. My reasoning : msg is of type RFAMessage. RFAMessage is not part of my swig interface file, but it is part of the library used by my C++ wrapper project. Can RAFMessage object be converted to PyObject if it is not part of SWIG interface file? What are your suggestions on this? -> state = PyGILState_Ensure(); The thread just gets blocked on this call. It doesn't do anything after this. After searching on internet I couldn't find anything which can describe why postOffice thread may get blocked here. Any reasons? solutions? Please advice. I have spent more than 2 days trying to tackle this single issue. Regards, Alok -- View this message in context: http://old.nabble.com/Python-C%2B%2B-extension-%28multi-threaded-environ ment%29-tp26635775p26635775.html Sent from the swig-user mailing list archive at Nabble.com. ------------------------------------------------------------------------ ------ Join us December 9, 2009 for the Red Hat Virtual Experience, a free event focused on virtualization and cloud computing. Attend in-depth sessions from your desk. Your couch. Anywhere. http://p.sf.net/sfu/redhat-sfdev2dev _______________________________________________ Swig-user mailing list Swi...@li... https://lists.sourceforge.net/lists/listinfo/swig-user ======================================================================== ======= Please access the attached hyperlink for an important electronic communications disclaimer: http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html ======================================================================== ======= ------------------------------------------------------------------------ ------ Join us December 9, 2009 for the Red Hat Virtual Experience, a free event focused on virtualization and cloud computing. Attend in-depth sessions from your desk. Your couch. Anywhere. http://p.sf.net/sfu/redhat-sfdev2dev _______________________________________________ Swig-user mailing list Swi...@li... https://lists.sourceforge.net/lists/listinfo/swig-user ======================================================================== ====== Please access the attached hyperlink for an important electronic communications disclaimer: http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html ======================================================================== ====== ======================================================================== ====== Please access the attached hyperlink for an important electronic communications disclaimer: http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html ======================================================================== ====== =============================================================================== Please access the attached hyperlink for an important electronic communications disclaimer: http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html =============================================================================== |