Closed tmontes closed 2 years ago
Thanks for the report!
I'm able to reproduce this on an M1 using macOS 12. That seemingly rules out a macOS targeting bug. My stack from a REPL is slightly different:
python/bin/python3
Python 3.10.4 (main, May 28 2022, 14:22:07) [Clang 14.0.3 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> root = tkinter.Tk()
>>> 2022-06-26 10:26:27.805 python3[30525:3649599] -[TKApplication tkProcessEvent:]: unrecognized selector sent to instance 0x12da807f0
2022-06-26 10:26:27.818 python3[30525:3649599] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TKApplication tkProcessEvent:]: unrecognized selector sent to instance 0x12da807f0'
*** First throw call stack:
(
0 CoreFoundation 0x00000001c2ec9198 __exceptionPreprocess + 240
1 libobjc.A.dylib 0x00000001c2c13e04 objc_exception_throw + 60
2 CoreFoundation 0x00000001c2f5cf40 -[NSObject(NSObject) __retain_OA] + 0
3 CoreFoundation 0x00000001c2e28544 ___forwarding___ + 1764
4 CoreFoundation 0x00000001c2e27da0 _CF_forwarding_prep_0 + 96
5 libpython3.10.dylib 0x0000000101741468 TkMacOSXEventsCheckProc + 332
6 libpython3.10.dylib 0x0000000101647030 Tcl_DoOneEvent + 268
7 libpython3.10.dylib 0x0000000101a28914 EventHook + 180
8 libpython3.10.dylib 0x0000000101abbfb8 readline_until_enter_or_signal + 264
9 libpython3.10.dylib 0x0000000101abb650 call_readline + 136
10 libpython3.10.dylib 0x000000010178aba4 PyOS_Readline + 172
11 libpython3.10.dylib 0x000000010178d8d0 tok_underflow_interactive + 112
12 libpython3.10.dylib 0x000000010178d148 tok_get + 5048
13 libpython3.10.dylib 0x00000001017571d0 _PyPegen_fill_token + 56
14 libpython3.10.dylib 0x000000010175bd4c _PyPegen_parse + 764
15 libpython3.10.dylib 0x000000010175992c _PyPegen_run_parser + 24
16 libpython3.10.dylib 0x0000000101759e24 _PyPegen_run_parser_from_file_pointer + 184
17 libpython3.10.dylib 0x000000010193c03c PyRun_InteractiveOneObjectEx + 392
18 libpython3.10.dylib 0x000000010193b340 _PyRun_InteractiveLoopObject + 116
19 libpython3.10.dylib 0x000000010193ae9c _PyRun_AnyFileObject + 76
20 libpython3.10.dylib 0x000000010193d434 PyRun_AnyFileExFlags + 68
21 libpython3.10.dylib 0x000000010195be28 pymain_run_stdin + 160
22 libpython3.10.dylib 0x000000010195b4b4 Py_RunMain + 1108
23 libpython3.10.dylib 0x000000010195c614 Py_BytesMain + 40
24 dyld 0x00000001001c508c start + 520
)
libc++abi: terminating with uncaught exception of type NSException
zsh: abort python/bin/python3
TkMacOSXEventsCheckProc
is the common function. But that function simply proxies NSEvents to X events.
The crash seemingly occurs on this line in TkMacOSXEventsCheckProx()
: NSEvent *processedEvent = [NSApp tkProcessEvent:currentEvent];
.
It doesn't look like CPython changed anything major with regards to tk integration between 3.10.0 and 3.10.2. So my money is on this being some kind of regression between tk 8.6.10 and 8.6.12.
I'm not great at Obj-C debugging. But given this seemingly has to do with tk's internal Obj-C message handling for TKApplication
, I think appropriate next steps are to report a bug against tk and get someone more experienced with tk's internals to look into it.
It might also be worth reproducing without a statically linked tcl/tk. While static linking should be supported, it isn't common and has a history of tickling bugs due to common assumptions that libraries are shared libraries.
CPython 3.10.1 did upgrade tcl/tk to 8.6.12. So it is good they are running the same version as us with their official macOS installers.
I looked at the CPython tcl/tk macOS build code and didn't see any glaring differences beyond the static/shared library difference.
I compiled tk with TK_MAC_DEBUG_EVENTS
defined to get some more forensics logging. Also moved TKLog(@" event: %@", currentEvent);
line in TkMacOSXEventsCheckProc()
to before the crashing NSEvent *processedEvent = [NSApp tkProcessEvent:currentEvent];
line. That produced the following:
% python/install/bin/python3
Python 3.10.5 (main, Jun 26 2022, 12:07:50) [Clang 14.0.3 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> tkinter.Tk()
<tkinter.Tk object .>
>>> 2022-06-26 12:10:17.922 python3[20924:3832525] event: NSEvent: type=Kitdefined loc=(0,228) time=1102562.7 flags=0 win=0x153a65dc0 winNum=364 ctxt=0x0 subtype=4 data1=1128792064 data2=1130627072
2022-06-26 12:10:17.922 python3[20924:3832525] -[TKApplication tkProcessEvent:]: unrecognized selector sent to instance 0x142e92b50
2022-06-26 12:10:17.929 python3[20924:3832525] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TKApplication tkProcessEvent:]: unrecognized selector sent to instance 0x142e92b50'
So it crashes on a Kitdefined
event. But I think the more important takeaway is it is crashing on the first event. I think the bug here is tkProcessEvent
isn't being registered with NSApp
/ TKApplication
. Why, I'm not sure. But the answer seemingly involves macosx/tkMacOSXInit.c
.
I have a release staged with this fix at https://github.com/indygreg/python-build-standalone/releases/tag/20220630. Should hopefully remove the draft label within a few hours once PyOxidizer CI comes back clean.
@indygreg,
Wow, thank you very much for such a speedy fix. I do confirm that Tk based applications work nicely on macOS (and Windows and Linux) now!
Amazing! :-)
Hey @indygreg,
It's me again, exploring your wonderful project, and experiencing a
tkinter
failure on macOS.My minimal test:
Works up to 20220222. On that release, with 3.10.2, fails with:
What else I tested:
Failing -- macOS 10.14.6:
pgo
,lto
,pgo+lto
, anddebug
builds.pgo
andlto
builds.pgo
build of the most recent release, 20220528.Working -- CentOS 7 Linux and Windows, both x86-64:
Release notes for 20220222 indicate that Tcl/Tk was upgraded from 8.6.10 to 8.6.12, so this provides at least some explanation to my observations.
Can you please help in getting Tk back in shape on macOS? How can I assist in the process?
Again, thanks a lot for sharing the project and, in advance, for helping address this.