Skip to content

Python modules such as pyglet or pygame crash Python when tkinter message boxes are opened on MacOS. #90731

Closed

Description

@remyrfouquette
BPO 46573
Nosy @ronaldoussoren, @ned-deily
Files
  • issue.py: A short test I put together showing the issue.
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2022-02-03.21:05:49.571>
    created_at = <Date 2022-01-29.18:48:13.856>
    labels = ['OS-mac', 'expert-tkinter', '3.10', 'type-crash']
    title = 'Python modules such as pyglet or pygame crash Python when tkinter message boxes are opened on MacOS.'
    updated_at = <Date 2022-02-03.21:05:49.570>
    user = 'https://bugs.python.org/remyrfouquette'

    bugs.python.org fields:

    activity = <Date 2022-02-03.21:05:49.570>
    actor = 'ned.deily'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-02-03.21:05:49.571>
    closer = 'ned.deily'
    components = ['macOS', 'Tkinter']
    creation = <Date 2022-01-29.18:48:13.856>
    creator = 'remyrfouquette'
    dependencies = []
    files = ['50597']
    hgrepos = []
    issue_num = 46573
    keywords = []
    message_count = 12.0
    messages = ['412093', '412309', '412310', '412311', '412312', '412344', '412349', '412392', '412399', '412423', '412425', '412469']
    nosy_count = 5.0
    nosy_names = ['ronaldoussoren', 'ned.deily', 'culler', 'Marc.Culler', 'remyrfouquette']
    pr_nums = []
    priority = 'normal'
    resolution = 'third party'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue46573'
    versions = ['Python 3.10']

    Activity

    1. remyrfouquette commented on Jan 29, 2022

      remyrfouquettemannequin
      MannequinAuthor

      The same problem doesn't exist on Windows.

    2. ronaldoussoren commented on Feb 1, 2022

      @ronaldoussoren
      Contributor

      Error output when running the script:

      2022-02-01 22:15:32.213 Python[5468:5726153] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to /var/folders/d3/rc5nx4v12y96knh2px3bpqsc0000gn/T/org.python.python.savedState
      2022-02-01 22:15:32.362 Python[5468:5726153] -[NSApplication macOSVersion]: unrecognized selector sent to instance 0x105624bc0
      2022-02-01 22:15:32.367 Python[5468:5726153] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication macOSVersion]: unrecognized selector sent to instance 0x105624bc0'
      *** First throw call stack:
      (
      0 CoreFoundation 0x000000018b4c01cc __exceptionPreprocess + 240
      1 libobjc.A.dylib 0x000000018b2117b8 objc_exception_throw + 60
      2 CoreFoundation 0x000000018b5531a0 -[NSObject(NSObject) __retain_OA] + 0
      3 CoreFoundation 0x000000018b420360 ___forwarding___ + 1728
      4 CoreFoundation 0x000000018b41fbe0 _CF_forwarding_prep_0 + 96
      5 libtk8.6.dylib 0x0000000100940a1c GetRGBA + 64
      6 libtk8.6.dylib 0x00000001009404a8 SetCGColorComponents + 140
      7 libtk8.6.dylib 0x00000001009408e0 TkpGetColor + 544
      8 libtk8.6.dylib 0x00000001008a2454 Tk_GetColor + 220
      9 libtk8.6.dylib 0x0000000100895aa0 Tk_Get3DBorder + 204
      10 libtk8.6.dylib 0x00000001008958ac Tk_Alloc3DBorderFromObj + 144
      11 libtk8.6.dylib 0x00000001008a3810 DoObjConfig + 832
      12 libtk8.6.dylib 0x00000001008a33cc Tk_InitOptions + 348
      13 libtk8.6.dylib 0x00000001008a32c8 Tk_InitOptions + 88
      14 libtk8.6.dylib 0x00000001008cacf0 CreateFrame + 1448
      15 libtk8.6.dylib 0x00000001008cafe8 TkListCreateFrame + 156
      16 libtk8.6.dylib 0x00000001008c3ec0 Initialize + 1848
      17 _tkinter.cpython-310-darwin.so 0x00000001008762d4 Tcl_AppInit + 92
      18 _tkinter.cpython-310-darwin.so 0x0000000100875f6c Tkapp_New + 548
      19 _tkinter.cpython-310-darwin.so 0x0000000100875d44 _tkinter_create_impl + 268
      20 _tkinter.cpython-310-darwin.so 0x000000010087597c _tkinter_create + 240
      21 Python 0x000000010109b1f0 cfunction_vectorcall_FASTCALL + 88
      22 Python 0x0000000101169f48 call_function + 132
      23 Python 0x0000000101161fac _PyEval_EvalFrameDefault + 23072
      24 Python 0x000000010115ad5c _PyEval_Vector + 360
      25 Python 0x0000000101033dac _PyObject_FastCallDictTstate + 96
      26 Python 0x00000001010c7cbc slot_tp_init + 196
      27 Python 0x00000001010be950 type_call + 312
      28 Python 0x0000000101033af4 _PyObject_MakeTpCall + 136
      29 Python 0x000000010116a040 call_function + 380
      30 Python 0x0000000101160a18 _PyEval_EvalFrameDefault + 17548
      31 Python 0x000000010115ad5c _PyEval_Vector + 360
      32 Python 0x0000000101169f48 call_function + 132
      33 Python 0x0000000101160a18 _PyEval_EvalFrameDefault + 17548
      34 Python 0x000000010115ad5c _PyEval_Vector + 360
      35 Python 0x0000000101169f48 call_function + 132
      36 Python 0x0000000101161404 _PyEval_EvalFrameDefault + 20088
      37 Python 0x000000010115ad5c _PyEval_Vector + 360
      38 Python 0x000000010115cb3c _PyEval_EvalFrameDefault + 1456
      39 Python 0x000000010115ad5c _PyEval_Vector + 360
      40 Python 0x0000000101169f48 call_function + 132
      41 Python 0x0000000101160a18 _PyEval_EvalFrameDefault + 17548
      42 Python 0x000000010115ad5c _PyEval_Vector + 360
      43 Python 0x000000010115cb3c _PyEval_EvalFrameDefault + 1456
      44 Python 0x000000010115ad5c _PyEval_Vector + 360
      45 Python 0x000000010115cb3c _PyEval_EvalFrameDefault + 1456
      46 Python 0x000000010115ad5c _PyEval_Vector + 360
      47 Python 0x0000000101169f48 call_function + 132
      48 Python 0x0000000101161404 _PyEval_EvalFrameDefault + 20088
      49 Python 0x000000010115ad5c _PyEval_Vector + 360
      50 Python 0x0000000101169f48 call_function + 132
      51 Python 0x0000000101161404 _PyEval_EvalFrameDefault + 20088
      52 Python 0x000000010115ad5c _PyEval_Vector + 360
      53 Python 0x0000000101169f48 call_function + 132
      54 Python 0x0000000101161404 _PyEval_EvalFrameDefault + 20088
      55 Python 0x000000010115ad5c _PyEval_Vector + 360
      56 Python 0x0000000101169f48 call_function + 132
      57 Python 0x0000000101161fac _PyEval_EvalFrameDefault + 23072
      58 Python 0x000000010115ad5c _PyEval_Vector + 360
      59 Python 0x00000001011c52c8 pyrun_file + 308
      60 Python 0x00000001011c4a0c _PyRun_SimpleFileObject + 336
      61 Python 0x00000001011c4058 _PyRun_AnyFileObject + 216
      62 Python 0x00000001011f0280 pymain_run_file_obj + 180
      63 Python 0x00000001011ef920 pymain_run_file + 72
      64 Python 0x00000001011eef08 pymain_run_python + 300
      65 Python 0x00000001011eed9c Py_RunMain + 24
      66 Python 0x00000001011f0428 pymain_main + 56
      67 Python 0x00000001011f06ec Py_BytesMain + 40
      68 dyld 0x00000001005bd0f4 start + 520
      )
      libc++abi: terminating with uncaught exception of type NSException

    3. ronaldoussoren commented on Feb 1, 2022

      @ronaldoussoren
      Contributor

      The last bit of the call stack according to lldb:

    4. ronaldoussoren commented on Feb 1, 2022

      @ronaldoussoren
      Contributor

      If I interpret the information I posted earlier correctly this is a bug in Tkinter, it calls a non-existing Objecive-C method (-[NSApplication macOSVersion]) and that results in an uncaught Objective-C exception and hence an abort().

      BTW. All of this is using python 3.10.2 using the installer on python.org.

    5. ned-deily commented on Feb 1, 2022

      @ned-deily
      Member

      If I interpret the information I posted earlier correctly this is a bug in Tkinter [...]

      Tkinter? Or Tk itself?

    6. ronaldoussoren commented on Feb 2, 2022

      @ronaldoussoren
      Contributor

      Oops, sorry: In Tk itself.

    7. ronaldoussoren commented on Feb 2, 2022

      @ronaldoussoren
      Contributor

      I've done some more research and this is an integration issue between pyglet and Tk. In particular:

      • Both pyglet and Tk use AppKit to implement their GUI
      • AppKit uses an NSApplication class, and in particular a singleton instance of that class, to represent the application, which will get instantiated during application startup
      • The Tk library uses a sublclass of NSApplication with additional functionality (TkApplication).
      • When pyglet is started first the NSApplication singleton is an instance of NSApplication and not of TkApplication, but Tk's implementation assumes that the singleton is an instance of TkApplicationo.

      A workaround for this issue is to make sure Tk is initialised before initialising pyglet. For example using this code at the start of the script: "from tkinter import Tk; root = Tk()"

      There's not much we can do about this in CPython, other than maybe filing an issue with the Tk project about this. I haven't looked at the Tk codebase and there cannot tell how hard it would be for then to avoid a dependency on TkApplication as a separate class (for example by implementing the additional functionality they need in a category on NSApplication).

    8. ned-deily commented on Feb 2, 2022

      @ned-deily
      Member

      Thanks for looking into this, Ronald. Before we close this as a Python issue, perhaps Marc might have a suggestion on what should be done here from a Tk perspective.

    9. MarcCuller commented on Feb 2, 2022

      MarcCullermannequin
      Mannequin

      The TKApplication class is a subclass of NSApplication which adds both
      attributes and methods. Those are used throughout the macOS port of Tk,
      and as Ronald says, the unique instance of NSApplication in Tk is actually
      a TKApplication. Every macOS Application must have a unique NSApplication
      object which is referenced by the global variable NSApp. The TKApplication
      subclass was part of the first Cocoa port of Tk, long before I became
      involved, and it may have even been there in the Carbon days. Subclassing
      NSApplication appears to be something that Apple expects.

      I don't know enough about Categories to understand how they could be used
      to solve this problem. Do they somehow make it possible to add new methods
      and attributes to an existing NSApplication object at runtime? I have
      always thought of them as being syntactical in nature, as opposed to
      allowing runtime modifications of an object's class after the object has
      been instantiated. But I am not an expert on Categories by any means.

      • Marc

      On Wed, Feb 2, 2022 at 1:54 PM Ned Deily <[email protected]> wrote:

      Ned Deily <[email protected]> added the comment:

      Thanks for looking into this, Ronald. Before we close this as a Python
      issue, perhaps Marc might have a suggestion on what should be done here
      from a Tk perspective.

      ----------
      nosy: +culler
      status: pending -> open


      Python tracker <[email protected]>
      <https://bugs.python.org/issue46573\>


    10. ronaldoussoren commented on Feb 3, 2022

      @ronaldoussoren
      Contributor

      Categories make it possible to add methods to an existing class, but not new attributes. The latter shouldn't be a problem in principle for an NSApplication subclass because only a single instance of the application class will get created and you may as well use global variables for those.

      In some sense categories are syntactic sugar for monkey patching existing classes, commonly used to add convenience methods to Cocoa classes.

    11. ronaldoussoren commented on Feb 3, 2022

      @ronaldoussoren
      Contributor

      Something I forgot to mention: I don't think this is a bug in Tk now that I've looked into this more deeply.

      The integration problem between Tk and other users of the Cocoa event loop is unfortunate but not a bug.

    12. ned-deily commented on Feb 3, 2022

      @ned-deily
      Member

      Thanks, Marc and Ronald, for the analysis.

      And thanks for bringing up the issue, Remy. Perhaps you can verify that Ronald's suggestion of importing and launching Tk before calling pyglet or pygame works for you and, if so, follow up with those projects to, if nothing else, document that requirement for their users on macOS (I would guess it wouldn't hurt on other platforms as well but I've guessed wrong before). In any case, I'm closing this issue now.

    13. transferred this issue fromon Apr 10, 2022
    14. mdehoon commented on Jun 4, 2026

      @mdehoon
      Contributor

      @ronaldoussoren

      Something I forgot to mention: I don't think this is a bug in Tk now that I've looked into this more deeply.

      The integration problem between Tk and other users of the Cocoa event loop is unfortunate but not a bug.

      I do think that this is a bug in Tk, as this can be fixed in Tk simply by not subclassing NSApplication, and using categories and some global variables instead.

      See https://core.tcl-lang.org/tk/tktview/637e74c1e7 for a bug report in Tk.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions