Hello,
I am attempting to write a python extension in C to "glue" the ASIO SDK
to python 2.5.4. However I am really confused about reference counts.
Below is a code snippet that "seems" to work, however I suspect that
some of the object reference counts have been incremented too many
times.
static PyObject *
getAsioInfo(PyObject *self, PyObject *args) {
PyObject *dict = Py_None;
if (loadAsioDriver (ASIO_DRIVER_NAME)) {
if (ASIOInit (&asioDriverInfo.driverInfo) == ASE_OK){
dict = PyDict_New();
PyDict_SetItem(dict,
PyString_FromString("asioVersion"),
PyInt_FromLong(asioDriverInfo.driverInfo.asioVersion) );
PyDict_SetItem(dict,
PyString_FromString("driverName"),
PyString_FromString(asioDriverInfo.driverInfo.name) );
return dict;
}
}
return Null;
}
As nearly as I can tell from the Python source code, PyDict_SetItem will
do a Py_INCREF on both the key and value parameters. However, it looks
as if PyInt_FromLong will incref the object it is returning - at least
if it is a "small value". Am I on the right track here? Or am I better
off doing something like:
PyObject *str, *lng;
str = PyString_FromString("asioVersion");
lng = PyInt_FromLong(asioDriverInfo.driverInfo.asioVersion)
);
PyDict_SetItem(dict, str, lng);
Py_DECREF(str);
Py_DECREF(lng);
Thanks in advance for all advice!
Dan Colesworthy
PyBindGen is a Python module that is geared to generating C/C++ code that
binds a C/C++ library for Python. It does so without extensive use of either
C++ templates or C pre-processor macros. It has modular handling of C/C++
types, and can be easily extended with Python plugins. The generated code is
almost as clean as what a human programmer would write.
It can be downloaded from:
http://code.google.com/p/pybindgen/
Bug reports should be filed here:
https://bugs.launchpad.net/ <https://bugs.launchpad.net/pybindgen>
pybindgen <https://bugs.launchpad.net/pybindgen>
<https://bugs.launchpad.net/pybindgen>Documentation:
http://packages.python.org/PyBindGen/
NEWS:
- Lots of small bug fixes
- Custodian/ward-style memory management works better now
- Add 'reference_existing_object' and 'return_interal_reference'
options for pointer/reference return values
- New Sphinx based documentation
--
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert
I am having a little bit of trouble getting the Python headers working with mingw-w64. The app I'm developing is C++ based and is being compiled for the x64 platform. When I include Python headers I get errors saying that standard C functions like round() are being redefined. I think it was in PyMath.h. And yes, I am using the 64 bit headers and libs from python.org
- George
_________________________________________________________________
Windows Live: Keep your friends up to date with what you do online.
http://go.microsoft.com/?linkid=9691815
Jadhav, Alok, 08.12.2009 01:09:
> 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).
This might help:
http://www.linuxjournal.com/article/3641
Stefan
Hi,
I don't have any experience with SWIG, but I can see two places where
things may go wrong:
1) If the call back function was garbage collected before you called
it (probably not the case if it is a normal function on the Python
side)
2) I assume that RFAMessageWrapper is an extension of a PyObject that
swig generated; in this case you should probably build arglist using a
pointer to, ie:
arglist = Py_BuildValue("(O)",&msg2);
--
Ulf Worsøe
Mosek ApS
On Tue, Dec 8, 2009 at 1:09 AM, Jadhav, Alok
<alok.jadhav(a)credit-suisse.com> wrote:
> 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..."<<endl;
> }
> 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
> ===============================================================================
>
> _______________________________________________
> capi-sig mailing list
> capi-sig(a)python.org
> http://mail.python.org/mailman/listinfo/capi-sig
>
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..."<<endl;
}
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
===============================================================================