ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ctypes-users
ctypes-users
Re: [ctypes-users] Function pointers as arguments or structure members
by Thomas Heller other posts by this author
Nov 21 2006 10:44AM messages near this date
[ctypes-users] Function pointers as arguments or structure members | [ctypes-users] PYFUNCTYPE, py_object and reference counting
Daniele Varrazzo schrieb:
>  Hello,
>  
>  i am trying (mostly successfully, thank you! :D) to wrap the 
>  SpiderMonkey library (http://www.mozilla.org/js/spidermonkey/) into 
>  Python using ctypes. The wrapper has been built using the ctypeslib 
>  CodeGenerator.
>  
>  I have to set up a structure, basically a vtable, so i have to fill a 
>  structure members with function pointers. These function are provided by 
>  the library itself, i don't have to setup Python callbacks here.
>  
>  The structure defined like:
>  
>       struct JSClass {
>           const char          *name;
>           uint32              flags;
>           JSPropertyOp        addProperty;
>           JSPropertyOp        delProperty;
>           ... and so on...
>       }
>  
>  I have to do the C equivalent of:
>  
>       JSClass global_class = {
>           "global",0,
>           JS_PropertyStub,JS_PropertyStub,
>           ... etc
>       };
>  
>  Up to now, the only way i managed to make it work is:
>  
>       global_class = JSClass('global', 0,
>           JSPropertyOp(JS_PropertyStub),
>           JSPropertyOp(JS_PropertyStub),
>           ...
>  
>  while the most obvious solution (directly passing JS_PropertyStub to the 
>  Python wrapped structure) leads to a TypeError (incompatible types, 
>  _FuncPtr instance instead of CFunctionType instance).
>  
>  Even if the above solution works, i am concerned that, when C code will 
>  access this vtable, it will call a Python function which in turn will 
>  call the desired function, resulting in an extra Python roundtrip for 
>  each call where only C code should have been involved.
>  
>  Is it true? If so, how can i pass a pointer to a C function without such 
>  double wrapping?

You could probably use cast.  Something like this (I assume JS_PropertyStub is
a foreign function loaded from a shared lib), and JS_PropertyOp is something created
with a call to CFUNCTYPE(...):

  JSClass('global', 0,
    cast(JS_PropertyStub, JSPropertyOp),

>  Thanks in advance,
>  
>  Daniele
>  
>  P.S. what is the best place to report a bug in the CodeGenerator?

You can post it here.

Thomas


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
ctypes-users mailing list
ctypes-users@[...].net
https://lists.sourceforge.net/lists/listinfo/ctypes-users
Thread:
Daniele Varrazzo
Thomas Heller

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved