Adding attributes to incomplete structures with Python

View: New views
3 Messages — Rating Filter:   Alert me  

Adding attributes to incomplete structures with Python

by Valeriy Zamarayev :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I am wrapping a C library, that is written in OO-like style, were structures
are not exposed in the .h file, but opaque pointers (aka handles) are used
everywhere, and all member  access is done via functions.

The structure itself is just declared,  only pointers are used and are
never dereferenced
in client code. That is ok with SWIG, it handles it fine. However,
what I want, is to
add __getattr__ and __setattr__ to the type of the pointer to
structure, so that I
can use these functions instread of accessors functions.

So, instead of something like:

s = mymodule.LibCreateStruct()
s.LibSetStructField1(fieldvalue1)
s.LibSetStructField2(fieldvalue2)

I could write just

s.field1 = value1
s.field2 = value2

I tried using the '%extend' feature, and placed C functions
__getattr__ and __setattr__ in it,
but it doesn't seem to work for opaque pointers.

There are, of course, alternatives such as exporting all the accessor
functions to Python and
writing a wrapper class or exposing the structure details to SWIG.

But I don't like the writing Python boilerplate in addition to SWIG
and C code in the .i file.
Besides, these accessors return value via an output parameter, so
typemaps will probably
be needed. I think I'll have to take this approach if nothing else helps.

Likewise, I don't like the idea of exposing internals of the
structures to SWIG just to make
%extend work. This completely breaks encapsulation.

I anyone could help me, if my approach of adding __getattr__ and
__setattr__ can be at all
implemented in SWIG, I'd very much appreciate that.

Thanks,
Valeriy

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: Adding attributes to incomplete structures with Python

by Josh Cherry :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


> I tried using the '%extend' feature, and placed C functions
> __getattr__ and __setattr__ in it,
> but it doesn't seem to work for opaque pointers.

If the issue is that they're opaque to SWIG (SWIG doesn't see a structure declaration), you might just need to add an empty declaration for your class, like

struct MyStruct {};

Josh

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Ответ: Adding attributes to incomplete structures with Python

by Valeriy Zamarayev :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks,

I tried, and, in fact, it didn't help with __getattr__ and
__setattr__, These are just not being called by SWIG. But SWIG
provides a better way to add members, using %extend: just add new
members to the structure with %extend, and write _get and _set
functions. You can use typemaps for argument convertion in those _get
and _set functions as usual. It is in the manual paragraph "5.5.6
Adding member functions to C structures" at the end of the paragraph.
Now the module works fine.

Regards,
Valeriy


2008/9/22, Cherry, Josh (NIH/NLM/NCBI) [E] <jcherry@...>:

>
>> I tried using the '%extend' feature, and placed C functions
>> __getattr__ and __setattr__ in it,
>> but it doesn't seem to work for opaque pointers.
>
> If the issue is that they're opaque to SWIG (SWIG doesn't see a structure
> declaration), you might just need to add an empty declaration for your
> class, like
>
> struct MyStruct {};
>
> Josh
>

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user
LightInTheBox - Buy quality products at wholesale price!