|
From: Bob H. <bob...@gm...> - 2008-01-15 07:01:40
|
Hmmm - I thought that was too easy! After
stat, my_foo = foo.foo_init()
... I see that type(my_foo) is <type 'int'> - but I need 'FOO *' so that I
can pass my_foo into
ret = foo_use(my_foo);
I guess it's something to do with the line
o = PyLong_FromVoidPtr(*$1);
If I could see something of the type (FOO *) then I guess I could do a *
PyObject_Type*() on it - and then, what?
No - I think I need something from SWIG to do this, but what? Maybe another
good sleep will fix it ... and another read through the manuals.
Bob
On Jan 15, 2008 9:47 AM, Bob Hepple <bob...@gm...> wrote:
> Cool - I'll try that in a minute - in the meantime (it's marvellous the
> wonders a night's sleep will work) I found that I can get this to work:
>
> stat, my_foo = foo.foo_init()
>
> with this as foo.i:
>
> %module foo
> %include "typemaps.i"
> %typemap(argout) FOO **foo {
> PyObject *o, *o2, *o3;
> o = PyLong_FromVoidPtr(*$1);
> if ((!$result) || ($result == Py_None)) {
> $result = o;
> } else {
> if (!PyTuple_Check($result)) {
> PyObject *o2 = $result;
> $result = PyTuple_New(1);
> PyTuple_SetItem($result,0,o2);
> }
> o3 = PyTuple_New(1);
> PyTuple_SetItem(o3,0,o);
> o2 = $result;
> $result = PySequence_Concat(o2,o3);
> Py_DECREF(o2);
> Py_DECREF(o3);
> }
> }
> %typemap(in,numinputs=0) FOO **foo(void *temp) {
> $1 = (FOO **)&temp;
> }
>
> %{
> #include "foo.h"
> %}
> %include "foo.h"
>
>
> On Jan 15, 2008 9:42 AM, David Beazley < dav...@da...> wrote:
>
> > It's probably much easier to get the status if you organize the
> > function like this:
> >
> > Foo *create_Foo(int *status) {
> > Foo *f = 0;
> > *status = foo_init(&f);
> > return f;
> > }
> >
> > You can then apply one of the existing OUTPUT typemaps to the status
> > parameter.
> >
> > -- Dave
> >
> >
> >
> >
> > On Jan 14, 2008, at 5:36 PM, Bob Hepple wrote:
> >
> > > Thanks, David, that works _but_ I also need to get the status out
> > > of it - that's why I was hoping to be able to use the "pointer to
> > > the handle" approach.
> > > I'll have a go at trying to get create_Foo() return both
> > > parameters, and post my results here.
> > >
> > > BTW if I get this polished up, is there a good place to post it in
> > > the doco as a worked example (apart from here, that is) ? I'm sure
> > > it'd be useful to others.
> > >
> > > Cheers
> > >
> > >
> > > Bob
> > >
> > >
> > > On Jan 14, 2008 6:38 PM, David Beazley < dav...@da...> wrote:
> > > What about just replacing foo_init with a helper function?
> > >
> > > %inline %{
> > > Foo *create_Foo() {
> > > Foo *f = 0;
> > > foo_init(&f);
> > > return f;
> > > }
> > > %}
> > >
> > > Then, use this to create a Foo * object.
> > >
> > > -- Dave
> > >
> > >
> > > On Jan 14, 2008, at 12:46 AM, Bob Hepple wrote:
> > >
> > > > I dug through the doco and maillist archive for a few hours but I'm
> > > > still stuck. It seems to me that this should be a fairly simple
> > > > thing to do, but ...
> > > >
> > > > I have a function in a shared library libfoo.so defined with foo.h:
> > > >
> > > > foo.h:
> > > > typedef struct foo_st FOO;
> > > > int foo_init(FOO **foo); /* malloc memory for structure */
> > > > int foo_use(FOO *foo); /* do something with the structure */
> > > > int foo_fini(FOO *foo); /* free up the structure */
> > > >
> > > > ... that I would normally call from C like this:
> > > > FOO *foo = NULL;
> > > > int status = 0;
> > > > status = foo_init(&foo);
> > > > ...
> > > > foo_use(foo); /* etc */
> > > > foo_fini(foo);
> > > >
> > > > ... ie a fairly typical abstract opaque data structure
> > > >
> > > >
> > > > When I swig this with the following simple foo.i file:
> > > >
> > > > %module foo
> > > > %{
> > > > #include "foo.h"
> > > > %}
> > > > %include " foo.h"
> > > >
> > > > ... I get python errors:
> > > >
> > > > import foo
> > > > foo.foo_init ()
> > > > =>
> > > > foo.foo_init()
> > > > TypeError: foo_init() takes exactly 1 argument (0 given)
> > > > .. which is understandable, but how do I give it an abstract
> > > > pointer? This also fails:
> > > >
> > > > import foo
> > > > my_foo = foo.FOO ()
> > > > foo.foo_init(my_foo)
> > > > =>
> > > > my_foo = foo.FOO()
> > > > AttributeError: 'module' object has no attribute 'FOO'
> > > >
> > > > I _had _thought that I could %typemap the need for an argument out
> > > > of it with this (scoured from the doco):
> > > >
> > > > %module foo
> > > > %include "typemaps.i"
> > > > %typemap(argout) FOO **foo {
> > > > PyObject *o, *o2, *o3;
> > > > o = PyLong_FromVoidPtr(*$1);
> > > > if ((!$result) || ($result == Py_None)) {
> > > > $result = o;
> > > > } else {
> > > > if (!PyTuple_Check($result)) {
> > > > PyObject *o2 = $result;
> > > > $result = PyTuple_New(1);
> > > > PyTuple_SetItem($result,0,o2);
> > > > }
> > > > o3 = PyTuple_New(1);
> > > > PyTuple_SetItem(o3,0,o);
> > > > o2 = $result;
> > > > $result = PySequence_Concat(o2,o3);
> > > > Py_DECREF(o2);
> > > > Py_DECREF(o3);
> > > > }
> > > > }
> > > > %{
> > > > #include " foo.h"
> > > > %}
> > > > %include " foo.h"
> > > >
> > > > ... but it gives exactly the same errors.
> > > >
> > > > Any ideas out there?
> > > >
> > > > For completeness, here's the build chain that I used:
> > > >
> > > > cc -c foo.c
> > > > ld -shared -o libfoo.so foo.o
> > > > swig -python foo.i
> > > > cc -c foo_wrap.c -I /usr/include/python2.5
> > > > ld -shared -rpath "`pwd`" -o _foo.so foo_wrap.o libfoo.so
> > > >
> > > > And the source code foo.c:
> > > >
> > > > struct foo_st {
> > > > int i;
> > > > int b;
> > > > };
> > > >
> > > > #include " foo.h"
> > > > #include "malloc.h"
> > > > #define FOO_SIZE 10
> > > >
> > > > int foo_init(FOO **foo)
> > > > {
> > > > *foo = malloc(FOO_SIZE);
> > > > return(0);
> > > > }
> > > >
> > > > int foo_use(FOO *foo)
> > > > {
> > > > foo->i = 1;
> > > > return(0);
> > > > }
> > > >
> > > > int foo_fini(FOO *foo)
> > > > {
> > > > free(foo);
> > > > return(0);
> > > > }
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > ----------------------------------------------------------------------
> >
> > > > ---
> > > > Check out the new SourceForge.net Marketplace.
> > > > It's the best place to buy or sell services for
> > > > just about anything Open Source.
> > > > http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/
> > > > marketplace_______________________________________________
> > > > Swig-user mailing list
> > > > Swi...@li...
> > > > https://lists.sourceforge.net/lists/listinfo/swig-user
> > >
> > >
> > > ----------------------------------------------------------------------
> >
> > > ---
> > > Check out the new SourceForge.net Marketplace.
> > > It's the best place to buy or sell services for
> > > just about anything Open Source.
> > > http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/
> > > marketplace_______________________________________________
> > > Swig-user mailing list
> > > Swi...@li...
> > > https://lists.sourceforge.net/lists/listinfo/swig-user
> >
> >
>
|