I implemented delegate support for pyobjus

195 views
Skip to first unread message

Olli Wang

unread,
Jan 31, 2014, 2:11:03 PM1/31/14
to kivy-...@googlegroups.com
Hey guy, it seems pyobjus doesn't update for three months. So I implemented my own delegate support for pyobjus and submitted a pull request: https://github.com/kivy/pyobjus/pull/8  It works now but has some little issues described as follow.

The delegate support is achieved by a objc_delegate() function that converts a Python object to the corresponded Objective C delegate instance. A list of protocols intended to conform are also passed to the objc_delegate() function.

The objc_delegate() dynamically creates a new Objective C class inherited by NSObject, then adds desired protocols methods and instantiates the instance. The instance is converted to Python object before returned so it can be passed to other MetaObjcClass methods that requires delegate instance.

In order to achieve dynamic implementation of protocol methods. A delegate_register dict is added to remember the class name and corresponded Python object. So the dynamic function can dispatch the call to real Python object's method. However, currently registered delegates won't get released. Still looking for a way to free unused objects.

This commit also adds an example at exampls/delegate.py. However, because the protocol method will be called asynchronously. I needs to write the example as a simple Kivy app so the execution won't quit before the protocol instance gets called. There may be a simpler way to implement a event loop but I failed.

There is another problem about dynamically creating an Objective C class. That is, we need to use the objc_getProtocol() runtime function to find interested protocol methods (for signature and types). However, it seems not all protocols can be found through this function. More information about this issue can be found here: http://goo.gl/zA116F

Mathieu Virbel

unread,
Jan 31, 2014, 5:47:36 PM1/31/14
to kivy-...@googlegroups.com
Hey Olli

You are a god. Few days ago, i was experimenting with Systray on OSX,
and understood that pyobjus was missing delegates. I looked at it, but
i've seen it would take me too much time to dig in / understand /
implement. And just few hours ago, you did a PR. Great!

I've few remarqs with the syntax used, and would like to get something
more close to how pyjnius did. But it can be done in top of your work.

Tomorrow is FOSDEM, so maybe i'll review your PR at this time, or sunday.

Thanks a lot for your contribution!

Mathieu
> --
> You received this message because you are subscribed to the Google
> Groups "Kivy users support" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to kivy-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Olli Wang

unread,
Jan 31, 2014, 10:44:59 PM1/31/14
to kivy-...@googlegroups.com
So glad I can help. :)

ZenCODE

unread,
Feb 1, 2014, 12:58:17 AM2/1/14
to kivy-...@googlegroups.com
Awesome! (Just another arbitrary word of thanks + encouragement). If Mathieu calls you a god, you are + 10^10^10 in my books ;-)

Mathieu Virbel

unread,
Feb 4, 2014, 9:42:44 AM2/4/14
to kivy-...@googlegroups.com
I'm currently trying to work with BLE / OSX. The delegate was necessary, and now i can make a little step into the right direction.
And i'm stuck on the same issue as the one you raise in your last sentence: you cannot get a protocol definition if the protocol is not used during.. compilation time.

So i looked how Pyobjc is doing it. They have basically a "bridgesupport" xml file where you can find all the protocols defintinion and much more. But nothing for iOS. Instead of reusing thoses files (which are not always here in the .frameworks), i took the way to parsing all the C headers to look at @protocol, method definition, until @end. The script named "tools/buildprotocols.py" will scan all the headers in /System/Library/Frameworks, to generate a pyobjus/protocols.py file with all the possible protocols. And now, if the protocol wanted is not found with objc_getProtocol, i'm looking in the protocols.py, before raising an exception.

This is far from beeing complete and accurate, but the base is here to anyone who want to enhance it. iOS support can be possible as well, if nobody does it, i'll focus on it when i'll test BLE on iOS.

Mathieu
Reply all
Reply to author
Forward
0 new messages