[C++-sig] Re: Defining Policies for unknown data handles

Lutz Paelike lutz_p at gmx.net
Sun Apr 6 14:30:37 CEST 2003

> Lutz Paelike wrote:
>> Hi all,
> Hi Lutz,
>> i'm starting with boost python and also tried the great pyste tool
>>  to make some python bindings.
>> I have a question, which seems not too difficult for me, but i'm 
>> not sure which is the right way to go..
>> I have a commercial application with an interface i like to use. 
>> There are a couple of structures and functions defined. Some of 
>> them use a pointer to a hidden structure returned by a initalizing
>>  function. I don't need to know anything about the struct , i just
>>  pass it on to other functions to refer to the internal data.
>> It's going like this:
>> 1. Definition of  data handle:
>> typedef struct _CapChannel *CapChannel;    /* Opaque pointer to a 
>> channel*/
>> 2. Get Handle
>> CapChannel CapCreateChannel _PROTO((char *name, CapChannelUsage 
>> usage, int data_type)); 3. Use Handle
>> int    CapSetData _PROTO((CapChannel channel, void *data));
>> You get the picture...
>> So when i use pyste to generate the boost python interface code it
>>  gives a warning:
>> ---> Error: CapCreateChannel returns a pointer or a reference, but
>>  no policy was specified.
>> This makes perfect sense for me, as Pyste/Boost Python don't know 
>> how to deal with this reference. The question for me is now which 
>> is the right policy to use and how do i define this correctly? I 
>> don't need access to _CapChannel from Python. I just want to store
>>  the pointer.
> Just set the correct policy for CapCreateChannel:
> f = Function('CapCreateChannel', ...) set_policy(f, 
> return_value_policy(return_opaque_pointer))
> Note: pyste didn't support return_opaque_pointer until version 0.6.5,
>  which I just commited to CVS.
> Hope that helps, Nicodemus.

I'm sorry, that did not work completely. And maybe i found a (small) bug
in pyste..

When i use the following as you suggested:

f= Function("CapCreateChannel","mocapserver.h")
set_policy(f, return_value_policy(return_opaque_pointer))

i get
Traceback (most recent call last):
File "p:\Dev\boost_1_30_0\libs\python\pyste\src\pyste.py", line 194, in ?
status = Main()
File "p:\Dev\boost_1_30_0\libs\python\pyste\src\pyste.py", line 151,
in Main
execfile(interface, context)
File "mcp.pyste", line 42, in ?
set_policy(f, return_value_policy(return_opaque_pointer))
NameError: name 'return_opaque_pointer' is not defined

i figured out that you have a defined a variable called
and this var contains just the string "return_opaque_pointer"
so i used this instead:

f= Function("CapCreateChannel","mocapserver.h")
set_policy(f, return_value_policy('return_opaque_pointer'))

Now Pyste works, but still yields following warning:

mocapserver.h:205: type specifier omitted for parameter
mocapserver.h:205: parse error before `,' token
mocapserver.h:211: type specifier omitted for parameter
mocapserver.h:211: parse error before `)' token

---> Error: _CapChannel is forward declared. Please include the
appropriate header with its definition

Module mocap generated
2.19 seconds

This is ok, because Pyste is right there is no definition of _CapChannel
as it's resolved by linking to the appropriate lib.
Pyste generated code, though. It looks like:

def("CapCreateChannel", &CapCreateChannel,
return_value_policy<return_opaque_pointer >());

But if i now compile this i get:


cl /Zm800 -nologo -GX -c -DBOOST_PYTHON_DYNAMIC_LIB
/IP:\Dev\STLport-4.5.3\stlport /IP:\Dev\boost_1_30_0
/IC:\Python22\include /IV:\include /I. /c mocap.cpp /Fomocap.obj

P:\Dev\boost_1_30_0\boost/python/opaque_pointer_converter.hpp(88) :
error C2440: 'return' : cannot convert from 'struct _CapChannel' to
'struct _CapChannel *& '
A reference that is not to 'const' cannot be bound to a non-lvalue

P:\Dev\boost_1_30_0\boost/python/opaque_pointer_converter.hpp(87) :
while compiling class-template member function 'struct _CapChannel
*&__cdecl boost::python::opaque_pointer_converter<struct _CapChannel
*>::execute(struct boost::python::opaque_pointer_converter<struct
_CapChannel *>::instance &) '


Thanks a lot,

Lutz Paelike

More information about the Cplusplus-sig mailing list