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
|