Skip to content

Dead code in tkinter #140494

Open
Open

Description

@mdehoon

Bug report

Bug description:

Modules/_tkinter.c always defines TCL_THREADS:

/* If Tcl is compiled for threads, we must also define TCL_THREAD. We define
   it always; if Tcl is not threaded, the thread functions in
   Tcl are empty.  */ 
#define TCL_THREADS

but in two places in Modules/_tkinter.c , we check if TCL_THREADS is defined:

#ifdef TCL_THREADS
static Tcl_ThreadDataKey state_key;
typedef PyThreadState *ThreadSpecificData;
#define tcl_tstate \
    (*(PyThreadState**)Tcl_GetThreadData(&state_key, sizeof(PyThreadState*)))
#else
static PyThreadState *tcl_tstate = NULL;
#endif

and

#ifndef TCL_THREADS
    if (v->threaded) {
        PyErr_SetString(PyExc_RuntimeError,
                        "Tcl is threaded but _tkinter is not");
        Py_DECREF(v);
        return 0;
    }
#endif

Especially the latter is confusing, as this code block is not compiled and the test is never executed.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Activity

  1. terryjreedy commented on Oct 23, 2025

    @terryjreedy
    Member

    tcl/tk 8.5 was by default compiled without thread_support. 8.6 by default is, and perhaps the option is gone. Thread support was once optional in python, but now is mandatory so _tkinter should support them also.

    I suspect that the define and the comment before could be eliminated (if these are the only two uses) and the code between #ifdef TCL_THREADS and #ELSE# made unconditional and the other code eliminated. @serhiy-storchaka ?

  2. added
    type-refactorCode refactoring (with no changes in behavior)
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Oct 23, 2025
  3. serhiy-storchaka commented on Oct 23, 2025

    @serhiy-storchaka
    Member

    It was unconditionally enabled in b5bfb9f, 23 years ago. Well, I think we can remove this code now. Note that some comments may be need to be updated.

  4. mdehoon commented on Oct 24, 2025

    @mdehoon
    ContributorAuthor

    We still need to #define TCL_THREADS , otherwise tkinter won't compile.

    Should we require that tcl/tk was compiled with thread support, or should we still allow tcl/tk without thread support?

  5. serhiy-storchaka commented on Jul 17, 2026

    @serhiy-storchaka
    Member

    I think we can drop support of non-threaded Tcl/Tk in main. AFAIK, 8.6 is threaded by default, and 9.0 is always threaded. But the minimal supported version was still 8.5.12. Some distributors can still need to build without threaded support, they may even patch #define TCL_THREADS. So this is main-only change.

    To make backporting easier, it is better to hold these changes until we fix other issues.

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

    3.16new features, bugs and security fixesextension-modulesC modules in the Modules dirtopic-tkintertype-refactorCode refactoring (with no changes in behavior)

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions