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

Nicodemus nicodemus at globalite.com.br
Sun Apr 6 15:43:52 CEST 2003


Lutz Paelike wrote:

>
>> 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
> return_opaque_pointer
> 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'))


Ouch! Stupid, stupid, stupid! (that would be me). Thanks for reporting 
this. Fixed in version 0.6.6.

> 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 


Hmm, those are errors emitted by gccxml about your code... can't really 
help if you don't show some of it. 8)

> ---> 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 >());

Forward declaration normally generates compiler errors with 
Boost.Python, hence the warning. You should include its definition like 
this:

    Include('CapChannel.h')

Pyste will always generate code if gccxml could produce an xml file. 
Problem is, if you get some compilation errors, the definitions in the 
xml file can be incomplete, like missing methods and classes. You should 
try to remove those errors reported by GCCXML.

> But if i now compile this i get:
>
> -----------------------------------------
>
> cl /Zm800 -nologo -GX -c -DBOOST_PYTHON_DYNAMIC_LIB
> -D_STLP_DO_IMPORT_CSTD_FUNCTIONS=1 -D_STLP_DEBUG=1
> -D_STLP_DEBUG_UNINITIALIZED=1 /Z7 /Od /Ob0 /GX /GR /MDd
> /IP:\Dev\STLport-4.5.3\stlport /IP:\Dev\boost_1_30_0
> /IC:\Python22\include /IV:\include /I. /c mocap.cpp /Fomocap.obj
> mocap.cpp
>
> 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 &) '
> -------------------------------------------------
>
> Suggestions?
>
I don't know, the generated code seems to be ok. Perhaps someone else 
can help?

> Thanks a lot,
>
> Lutz Paelike

Thanks Lutz,
Nicodemus.







More information about the Cplusplus-sig mailing list