From: Robert J. <rob...@gm...> - 2005-11-23 21:33:41
|
Yippeeeeee! That works. Ta muchly. So, I need to pass swig the address of the address of the handle? Wow - how come? Ah, but I also see I never allocate the temporary handle object itself, so = I take it that swig sets tmp to point to an already existing object? That object must be buried somewhere inside PyObject? I think this is starting to make a bit more sense! I can't recall where I got the reinterpret_cast stuff from, probably some example somewhere. When would it be appropriate to use that? Would that be connected with using directors at all? Rob. On 23/11/05, Marcelo Matus <mm...@ac...> wrote: > > Robert Jones wrote: > > > the PyInt_AsLong function wasn't my first choice, I'd already tried > > various applications of SWIG_ConvertPtr, but I suspect I'm not getting > > the dereferencing quite right. My best stab at SWIG_ConvertPtr is this > > > > $2 =3D ( HANDLE * ) malloc( ( $1 + 1 ) * sizeof( HANDLE ) ); > > for ( i =3D 0; i < $1; i ++ ) { > > PyObject * obj =3D PyList_GetItem( $input, i ); > > HANDLE tmp; > > if ( ( SWIG_ConvertPtr( obj, SWIG_reinterpret_cast( & tmp, > > void** ), 0, SWIG_POINTER_EXCEPTION | $disown ) ) =3D=3D -1 ) { > > free( $2 ); > > return NULL; > > } > > $2[ i ] =3D tmp; > > I see your problem, well, first you are using 1.3.27 or so, right?, but > in any case, your code should look like > > $2 =3D ( HANDLE * ) malloc( ( $1 + 1 ) * sizeof( HANDLE ) ); > for ( i =3D 0; i < $1; i ++ ) { > PyObject * obj =3D PyList_GetItem( $input, i ); > HANDLE *tmp; > if ( ( SWIG_ConvertPtr( obj, &tmp, 0, SWIG_POINTER_EXCEPTION | > $disown ) ) =3D=3D -1 ) { > free( $2 ); > return NULL; > } > $2[ i ] =3D *tmp; > > note that 'tmp' should be a pointer, not a 'by value' element. > > Marcelo > > > > > > but it doesn't work! > > > > Cheers for you help, Rob. > > > |