Re: ctypes-users Digest, Vol 49, Issue 4

Adam Morris <[email protected]>
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
>
> AFAIK ctypes should handle GIL correctly automatically, also when used from
> threads.


Sure am glad to hear that!

To me it sounds more like a problem with bad alignment or bad parameter

profile of the callback function.


I don't know about bad alignment, but you may be right around the bad
parameter profile. I think I've narrowed down the problem to the context
argument that is being passed to the SetCallback function of the dll. If I
pass it null or 0 or pointer() or even byref(), the result is a bus error of
0x00 when it actually calls the callback, but if I pass it 10 it gives a bus
error with address of 0x0a. I'm confused because the example files of the
SDK show the C code passing a "0" for the context argument, so then why
would it spell trouble using that from Python? Even weirder, if I don't pass
any context parameter at all to the SetCallback function, I get the exact
same error (or is this because I don't understand how parameter lists work
in C?)


Here's all the relevant typedefs as defined in the SDK:


#Storage declarations:

typedef signed int      ax_int32_t

typedef unsigned short  ax_char16_t

typedef unsigned int    ax_handle_t

# can't find AX_CALLCONV nor AX_API in the SDK


#Callback declaration:

typedef void(AX_CALLCONV * ax_vote_callback_t)(ax_handle_t hubHandle,
ax_int32_t deviceSerialNumber, ax_int32_t vote, void *pContext)


Here's the SetCallback method I have to use, notice the context argument:


AX_API ax_result_t AX_CALLCONV  Ax_SetVoteCallback (ax_vote_callback_t
pfnCallback, void *pContext


Here's what I do in my own code, python 3.1 on Mac OS X, after "from cytpes
import *":


    def set_vote_callback(self, python_function):

        self.keep_wrap_function_reference = CFUNCTYPE(c_void_p, c_uint,
c_int, c_int, c_void_p)

        context = c_int(0)  # no idea what to do here, honestly

        result =
self.cdll.Ax_SetVoteCallback(self.keep_wrap_function_reference(python_function),
context)

        # Or do I need a pointer to the function??


Here's the callback I've defined (at the module-level), which gets passed as
"python_function" above:


def standard_vote_callback(handle, serialNumber, vote, context):

    print("Inside callback!")


I thank anybody who can help me out understanding what I've done wrong here.
If anybody knows how to send the "self" using an instance method using that
there same context argument that seems to be giving me trouble, I would also
be very grateful. I know about using the @staticmethod decorator but since I
don't understand what's going on I'm reluctant to play around!

------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate 
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the 
lucky parental unit.  See the prize list and enter to win: 
http://p.sf.net/sfu/thinkgeek-promo

_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.