Re: Return value "handler" / NSEvent.addLocalMonitorForEventsMatchingMask_handler_

Ronald Oussoren <[email protected]> Sun, 03 Dec 2017 10:44:06 +0100
Newsgroups gmane.comp.python.pyobjc.devel
Message-ID <[email protected]>
--===============2610045846757323231==
Content-type: multipart/alternative;
 boundary="Apple-Mail=_E00DD335-1F3D-4445-AF42-2DF9A4F4D4EE"


--Apple-Mail=_E00DD335-1F3D-4445-AF42-2DF9A4F4D4EE
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8



> On 1 Dec 2017, at 18:30, Just van Rossum <[email protected]> =
wrote:
>=20
> =46rom a running PyObjC/Cocoa app, I try to do the following:
>=20
>=20
> from AppKit import NSKeyDownMask, NSEvent
>=20
> def myHandler(event):
>   print(event)
>   return event
>=20
> mask =3D NSKeyDownMask
> monitor =3D =
NSEvent.addLocalMonitorForEventsMatchingMask_handler_(mask, myHandler)
>=20
>=20
> (one could call this in an applicationDidFinishLaunching_() app =
delegate method)
>=20
>=20
> The handler indeed gets called when I press a key, but the caller =
complains it=E2=80=99s returning None:
>=20
> 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 =E2=80=98v=E2=80=99):

:>>> =
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=E2=80=A6. 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=E2=80=99ve committed a fix for the second problem, and will shortly =
commit a fix for the incorrect metadata as well.

A quick workaround: Look for =
=E2=80=9CaddLocalMonitorForEventsMatchingMask:handler:=E2=80=9D in =
Lib/AppKit/_metadata.py and replace =E2=80=98retval=E2=80=99: =E2=80=98v=E2=
=80=99 by =E2=80=98retval=E2=80=99: =E2=80=98@=E2=80=98.

Ronald=

--Apple-Mail=_E00DD335-1F3D-4445-AF42-2DF9A4F4D4EE
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=utf-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; =
charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; line-break: after-white-space;" class=3D""><br =
class=3D""><div><br class=3D""><blockquote type=3D"cite" class=3D""><div =
class=3D"">On 1 Dec 2017, at 18:30, Just van Rossum &lt;<a =
href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a>&gt; wrote:</div><br =
class=3D"Apple-interchange-newline"><div class=3D""><div class=3D"">=46rom=
 a running PyObjC/Cocoa app, I try to do the following:<br class=3D""><br =
class=3D""><br class=3D"">from AppKit import NSKeyDownMask, NSEvent<br =
class=3D""><br class=3D"">def myHandler(event):<br class=3D""> =
&nbsp;&nbsp;print(event)<br class=3D""> &nbsp;&nbsp;return event<br =
class=3D""><br class=3D"">mask =3D NSKeyDownMask<br class=3D"">monitor =3D=
 NSEvent.addLocalMonitorForEventsMatchingMask_handler_(mask, =
myHandler)<br class=3D""><br class=3D""><br class=3D"">(one could call =
this in an applicationDidFinishLaunching_() app delegate method)<br =
class=3D""><br class=3D""><br class=3D"">The handler indeed gets called =
when I press a key, but the caller complains it=E2=80=99s returning =
None:<br class=3D""><br class=3D"">ValueError: &lt;function myHandler at =
0x1146b4c80&gt;: returned None, expecting a value<br =
class=3D""></div></div></blockquote><div><br class=3D""></div>Looking a =
the metadata the error message is wrong, the handler should not return a =
value (retval for the last argument is =E2=80=98v=E2=80=99):</div><div><br=
 class=3D""></div><div>:<span style=3D"font-family: Menlo; font-size: =
11px; background-color: rgb(255, 255, 255);" class=3D"">&gt;&gt;&gt; =
pprint.pprint(Cocoa.NSEvent.addLocalMonitorForEventsMatchingMask_handler_.=
__metadata__())</span><div style=3D"margin: 0px; font-stretch: normal; =
font-size: 11px; line-height: normal; font-family: Menlo; =
background-color: rgb(255, 255, 255);" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">{'arguments': ({'_template': True, 'type': =
b'@'},</span></div><div style=3D"margin: 0px; font-stretch: normal; =
font-size: 11px; line-height: normal; font-family: Menlo; =
background-color: rgb(255, 255, 255);" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
{'_template': True, 'type': b':'},</span></div><div style=3D"margin: =
0px; font-stretch: normal; font-size: 11px; line-height: normal; =
font-family: Menlo; background-color: rgb(255, 255, 255);" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
{'_template': True, 'type': b'Q'},</span></div><div style=3D"margin: =
0px; font-stretch: normal; font-size: 11px; line-height: normal; =
font-family: Menlo; background-color: rgb(255, 255, 255);" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
{'callable': {'arguments': ({'null_accepted': True,</span></div><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo; background-color: rgb(255, 255, =
255);" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'type': =
b'^v'},</span></div><div style=3D"margin: 0px; font-stretch: normal; =
font-size: 11px; line-height: normal; font-family: Menlo; =
background-color: rgb(255, 255, 255);" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; {'type': b'@'}),</span></div><div style=3D"margin: =
0px; font-stretch: normal; font-size: 11px; line-height: normal; =
font-family: Menlo; background-color: rgb(255, 255, 255);" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'retval': {'type': =
b'v'}},</span></div><div style=3D"margin: 0px; font-stretch: normal; =
font-size: 11px; line-height: normal; font-family: Menlo; =
background-color: rgb(255, 255, 255);" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" class=3D"">&nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'callable_retained': =
True,</span></div><div style=3D"margin: 0px; font-stretch: normal; =
font-size: 11px; line-height: normal; font-family: Menlo; =
background-color: rgb(255, 255, 255);" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" class=3D"">&nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'type': =
b'@?'}),</span></div><div style=3D"margin: 0px; font-stretch: normal; =
font-size: 11px; line-height: normal; font-family: Menlo; =
background-color: rgb(255, 255, 255);" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">&nbsp;'classmethod': True,</span></div><div style=3D"margin: =
0px; font-stretch: normal; font-size: 11px; line-height: normal; =
font-family: Menlo; background-color: rgb(255, 255, 255);" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">&nbsp;'hidden': False,</span></div><div style=3D"margin: 0px; =
font-stretch: normal; font-size: 11px; line-height: normal; font-family: =
Menlo; background-color: rgb(255, 255, 255);" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">&nbsp;'retval': {'_template': True, 'type': =
b'@'}}</span></div><div class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D""><br class=3D""></span></div><div =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D""><br class=3D""></span></div><div class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">However=E2=80=A6. 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.</span></div><div class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D""><br class=3D""></span></div><div =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">I=E2=80=99ve committed a fix for the second problem, and will =
shortly commit a fix for the incorrect metadata as =
well.</span></div><div class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D""><br class=3D""></span></div><div =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">A quick workaround: Look for =
=E2=80=9CaddLocalMonitorForEventsMatchingMask:handler:=E2=80=9D in =
Lib/AppKit/_metadata.py and replace =E2=80=98retval=E2=80=99: =E2=80=98v=E2=
=80=99 by =E2=80=98retval=E2=80=99: =E2=80=98@=E2=80=98.</span></div><div =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D""><br class=3D""></span></div><div =
class=3D"">Ronald</div></div></body></html>=

--Apple-Mail=_E00DD335-1F3D-4445-AF42-2DF9A4F4D4EE--


--===============2610045846757323231==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
--===============2610045846757323231==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Pyobjc-dev mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyobjc-dev

--===============2610045846757323231==--