[IronPython] PyBonjour through Ctypes

Clemens Nylandsted Klokmose clemensklokmose at gmail.com
Wed Sep 15 18:28:55 CEST 2010


Thanks for the reply,
Here are the relevant lines from pybonjour.py:

line 84:
_CFunc = ctypes.WINFUNCTYPE

line 579 - 824 (some omitted):
(Start code)
def _create_function_bindings():

    ERRCHECK    = True
    NO_ERRCHECK = False

    OUTPARAM    = (lambda index: index)
    NO_OUTPARAM = None

    specs = {

        #'funcname':
        #(
        #    return_type,
        #    errcheck,
        #    outparam,
        #    (
        # param_1_type,
        # param_2_type,
        # ...
        # param_n_type,
        # )),

        'DNSServiceRefSockFD':
        (
            ctypes.c_int,
            NO_ERRCHECK,
            NO_OUTPARAM,
            (
                DNSServiceRef, # sdRef
                )),

        ### ... 15 definitions in between here ... ###

        'DNSServiceConstructFullName':
        (
            ctypes.c_int,
            ERRCHECK,
            OUTPARAM(0),
            (
                ctypes.c_char * kDNSServiceMaxDomainName, # fullName
                _utf8_char_p, # service
                _utf8_char_p_non_null, # regtype
                _utf8_char_p_non_null, # domain
                )),

        }


    for name, (restype, errcheck, outparam, argtypes) in specs.iteritems():
        prototype = _CFunc(restype, *argtypes)

        paramflags = [1] * len(argtypes)
        if outparam is not None:
            paramflags[outparam] = 2
        paramflags = tuple((val,) for val in paramflags)

        func = prototype((name, _libdnssd), paramflags)

        if errcheck:
            func.errcheck = BonjourError._errcheck

        globals()['_' + name] = func


# Only need to do this once
_create_function_bindings()
(End code)

The line ' func = prototype((name, _libdnssd), paramflags) ' fails with the
following exception:
  File "test_pybonjour.py", line 34, in <module>
  File
"C:\Users\clemens\Downloads\pybonjour-1.1.1\pybonjour-1.1.1\pybonjour.py",
line 824, in <module>
  File
"C:\Users\clemens\Downloads\pybonjour-1.1.1\pybonjour-1.1.1\pybonjour.py",
line 815, in _create_function_bindings
TypeError: WinFunctionType() takes at most 2 arguments (3 given)

Cheers,
Clemens

On Mon, Sep 13, 2010 at 11:55 PM, Dino Viehland <dinov at microsoft.com> wrote:

>  It’s not that WINFUNCTYPE is always changing in CPython – it’s that each
> time you run it’ll have a different ID.  In IronPython we allocate object
> IDs sequentially starting at 43.  So if the only thing you call id() on
> (which printing the repr does implicitly) then the 1st object will always
> have an id of 43.  If you did “id(100)” first and then looked at
> ctypes.WINFUNCTYPE then you’d see it’d be 0x2C.
>
>
>
> Can you include the line of code where the call is happening and an setup
> to get into the call?  It does look like there’s a 3rd argument that’s
> optional because:
>
>
>
> from ctypes import *
>
> WINFUNCTYPE(c_int, c_int, c_int)(100, 'foo', '100')
>
>
>
> Starts reporting a different error message on CPython than IronPython.  My
> guess is we need to implement this but there don’t seem to be any tests for
> it so if we can see at least 1 call site that’d be helpful J  can you open
> a bug on CodePlex for this as well?
>
>
>
> *From:* users-bounces at lists.ironpython.com [mailto:
> users-bounces at lists.ironpython.com] *On Behalf Of *Clemens Nylandsted
> Klokmose
> *Sent:* Sunday, September 12, 2010 7:48 AM
> *To:* users at lists.ironpython.com
> *Subject:* [IronPython] PyBonjour through Ctypes
>
>
>
> Hi,
>
> I am experimenting with getting a Python library running in IronPython that
> relies on PyBonjour (http://code.google.com/p/pybonjour/).
>
> PyBonjour is a pure python library interfacing with the systems bonjour
> installation. I am completely unexperienced with CTypes, so what I am
> proposing now might seem naïve.
>
>
>
> At the top of the pybonjour.py is code looking like this:
>
> if sys.platform == 'win32':
>
>     # Need to use the stdcall variants
>
>     _libdnssd = ctypes.windll.dnssd
>
>     _CFunc = ctypes.WINFUNCTYPE
>
>
>
> IronPython's sys.platform evaluates to 'cli', so I have tried to change the
> check to if sys.platform == 'win32' or sys.platform == 'cli':
>
> This seems to work, but everytime a C function is called now, Python
> complains that theres an argument too much.
>
> E.g. TypeError: CFunctionType() takes at most 2 arguments (3 given). My
> suggestion is that ctypes.WINFUNCTYPE in IronPython might be a wrong value?
> (In IronPython it seems to always be 0x000000000000002B, whereas in CPython
> the value varies)
>
>
>
> Does anyone have an idea of how to go about this?
>
>
>
> It should be noted that I have pybonjour running with the standard CPython
> on Windows.
>
>
>
> Thanks,
>
> Clemens
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100915/df532731/attachment.html>


More information about the Ironpython-users mailing list