Re: [Pyobjc-dev] Return value "handler" / NSEvent.addLocalMonitorForEventsMatchingMask_handler_
Brought to you by:
ronaldoussoren
|
From: Ronald O. <ron...@ma...> - 2017-12-03 09:44:33
|
> On 1 Dec 2017, at 18:30, Just van Rossum <jus...@gm...> wrote:
>
> From a running PyObjC/Cocoa app, I try to do the following:
>
>
> from AppKit import NSKeyDownMask, NSEvent
>
> def myHandler(event):
> print(event)
> return event
>
> mask = NSKeyDownMask
> monitor = NSEvent.addLocalMonitorForEventsMatchingMask_handler_(mask, myHandler)
>
>
> (one could call this in an applicationDidFinishLaunching_() app delegate method)
>
>
> The handler indeed gets called when I press a key, but the caller complains it’s returning None:
>
> ValueError: <function myHandler at 0x1146b4c80>: returned None, expecting a value
Looking a the metadata the error message is wrong, the handler should not return a value (retval for the last argument is ‘v’):
:>>> pprint.pprint(Cocoa.NSEvent.addLocalMonitorForEventsMatchingMask_handler_.__metadata__())
{'arguments': ({'_template': True, 'type': b'@'},
{'_template': True, 'type': b':'},
{'_template': True, 'type': b'Q'},
{'callable': {'arguments': ({'null_accepted': True,
'type': b'^v'},
{'type': b'@'}),
'retval': {'type': b'v'}},
'callable_retained': True,
'type': b'@?'}),
'classmethod': True,
'hidden': False,
'retval': {'_template': True, 'type': b'@'}}
However…. that is a second bug, the return value should be an NSEvent value. That is, your code is correct, but PyObjC contains two bugs: 1) the metadata incorrectly says that the handler should not return a value, and 2) the error message when returning a value from a block where no return value is expected is wrong.
I’ve committed a fix for the second problem, and will shortly commit a fix for the incorrect metadata as well.
A quick workaround: Look for “addLocalMonitorForEventsMatchingMask:handler:” in Lib/AppKit/_metadata.py and replace ‘retval’: ‘v’ by ‘retval’: ‘@‘.
Ronald |