Re: [C++-sig] Cplusplus-sig Digest, Vol 15, Issue 3

2009-12-07 Thread meiko rachimow
when i: "fun(**kwargs)"
I get following runtime error:
TypeError: No to_python (by-value) converter found for C++ type:
boost::python::detail::kwds_proxy

thx again

meiko



> Message: 1
> Date: Thu, 3 Dec 2009 13:11:41 +0100
> From: meiko rachimow 
> To: cplusplus-sig@python.org
> Subject: [C++-sig] boost.python: call python function with kwargs from
>c++
> Message-ID:
><6a5b23630912030411m5be57968t9d8a2a7baf43f...@mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Hi
>
> How can I call a function defined in a python module from c++,
> if I have to give **kwargs parameter...
>
> example.py:
>
> def hello(**kwargs):
>for key, value in kwargs.items():
>print key, " - ",  value
>
>
> c++:
> using namespace boost::python;
>
> object fun = import("example").attr("hello")
> fun()
>
> int number = 42
> std::wstring name = "M?hre"
>
> dict kwargs;
> kwargs["Number"] = number;
> kwargs["Name"] = name;
>
> fun(...) ???
>
> What should i do ?
>
> Meiko
> -- next part --
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/cplusplus-sig/attachments/20091203/f7b0db6e/attachment-0001.htm
> >
>
> --
>
> Message: 2
> Date: Thu, 03 Dec 2009 08:11:36 -0500
> From: Stefan Seefeld 
> To: cplusplus-sig@python.org
> Subject: Re: [C++-sig] boost.python: call python function with kwargs
>from c++
> Message-ID: <4b17b908.7000...@sympatico.ca>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 12/03/2009 07:11 AM, meiko rachimow wrote:
> > Hi
> >
> > How can I call a function defined in a python module from c++,
> > if I have to give **kwargs parameter...
> >
> > example.py:
> >
> > def hello(**kwargs):
> > for key, value in kwargs.items():
> > print key, " - ",  value
> >
> >
> > c++:
> > using namespace boost::python;
> >
> > object fun = import("example").attr("hello")
> > fun()
> >
> > int number = 42
> > std::wstring name = "M?hre"
> >
> > dict kwargs;
> > kwargs["Number"] = number;
> > kwargs["Name"] = name;
> >
> > fun(...) ???
> >
> > What should i do ?
>
>
> fun(**kwargs);
>
>
> Stefan
>
> --
>
>   ...ich hab' noch einen Koffer in Berlin...
>
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

[C++-sig] Python C++ extension (multi threaded) raises access violation exception

2009-12-07 Thread Jadhav, Alok
I am using SWIG to extend Python to C++.   But the questions I have
consists of basic Python C API related. I hope this list will be able to
help me. 

I am trying to solve a simple problem which many of you must have
encountered. I wonder what I am doing different than others that I am
facing a problem.

Background
---

A c++ library is wrapped in my C++ Class. This new C++ class is extended
in Python using SWIG. I have to call a python function from C++ code (
which is on a new thread spawned from C++ main thread).

Issue
-
I am able to call the function. No issues. I could pass basic data types
such as int. No issues. But when I try to pass a C++ class object (eg
RFAMessageWrapper) to my python function I get an exception. It is an
Access violation exception. The memory could not be "read".  I am not
even able to catch this exception.



Some code snippets


C++ function which calls python function. ... 

[code]
static void PythonCallBack(RFAMessage *msg, void *clientdata)
{
   PyObject *func, *arglist;
   int blah = 1;
  
   PyGILState_STATE state; 
   state = PyGILState_Ensure();
   func = (PyObject *) clientdata;   // Get Python function
   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..."

Re: [C++-sig] Python C++ extension (multi threaded) raises access violation exception

2009-12-07 Thread Gustavo Carneiro
2009/12/8 Jadhav, Alok 

>  I am using SWIG to extend Python to C++.   But the questions I have
> consists of basic Python C API related. I hope this list will be able to
> help me.
>
> I am trying to solve a simple problem which many of you must have
> encountered. I wonder what I am doing different than others that I am facing
> a problem.
>
> Background
> ---
>
> A c++ library is wrapped in my C++ Class. This new C++ class is extended in
> Python using SWIG. I have to call a python function from C++ code ( which is
> on a new thread spawned from C++ main thread).
>
> Issue
> -
> I am able to call the function. No issues. I could pass basic data types
> such as int. No issues. But when I try to pass a C++ class object (eg
> RFAMessageWrapper) to my python function I get an exception. It is an Access
> violation exception. The memory could not be "read".  I am not even able to
> catch this exception.
>
>
> Some code snippets
> 
>
> C++ function which calls python function. ...
>
> [code]
> static void PythonCallBack(RFAMessage *msg, void *clientdata)
> {
>PyObject *func, *arglist;
>int blah = 1;
>
>PyGILState_STATE state;
>state = PyGILState_Ensure();
>func = (PyObject *) clientdata;   // Get Python function
>RFAMessageWrapper msg2(msg);
> try {
> arglist = Py_BuildValue("(O)",msg2); // Build
> argument list
>
This is bad.   Py_BuildValue("(O)", is expecting to find a PyObject, but msg2
is not a PyObject.

Additionally, PyObjects must be created with PyObject_New, and never stack
allocated like msg2 is.

I cannot offer any solution, as it would be SWIG specific and I am not
familiar with SWIG.


> //arglist = Py_BuildValue("(i)", blah);
> PyEval_CallObject(func,arglist); // Call Python
> } catch (...) {
> cout<<"Unknown exception..."< }
>   Py_XDECREF(arglist);   // Trash arglist
>PyGILState_Release(state);
> }
> [/code]
>
> Python function call..
>
> [code]
> def callback_fun(msg):
> try:
> print "RFAMessage received for service:"+msg.service
> except Exception:
> print "Exception handled"
> [/code]
>
> Whenever I try to access msg (of type RFAMessageWrapper .. C++ object) I
> get exception. msg is not None. I checked that in if condition. even
> type(msg) raises exception.
>
> RFAMessageWrapper class has been wrapped properly by SWIG as I could create
> an object manually and use it in Python. But only when I pass it from C++ to
> python I get this exception.
>
> I have spent long time to solve this issue but in vain. I hope I get some
> help from this Forum.
>
> Regards,
> Alok
>
>
> ==
> Please access the attached hyperlink for an important electronic
> communications disclaimer:
> http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
>
> ==
>
>
> ___
> Cplusplus-sig mailing list
> Cplusplus-sig@python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig