python / python/cpython

tkinter: Tk on macOS replaces Python's SIGINT, SIGHUP and SIGTERM handlers, so Ctrl-C exits instead of raising KeyboardInterrupt

Aberta
#157,672 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

OS-mac topic-tkinter type-bug
Linguagem predominante
Python
Estrelas
77.2k
Forks
35.9k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

Bug report

Since Tk 8.6.11, TkpInit() on macOS installs its own C-level handler for SIGINT, SIGHUP and SIGTERM, which calls Tcl_Exit(1) (tkMacOSXInit.c, Tk ticket f3e96942ed). It does this unconditionally, so Python's handler is replaced as soon as a Tk instance is created. signal.getsignal() still returns the Python handler, but it is never called.

import signal, time, tkinter
print(signal.getsignal(signal.SIGINT))  # <built-in function default_int_handler>
root = tkinter.Tk()
time.sleep(60)  # press Ctrl-C

On macOS the process exits silently with status 1 instead of raising KeyboardInterrupt. Python-level handlers set with signal.signal() for SIGTERM and SIGHUP are disabled the same way.

This also affects IDLE: with #157662, which interrupts user code with a real SIGINT, Ctrl-C in the Shell would terminate the user process on macOS once the user's code has created a Tk window. On the macOS CI the test_idle worker exits with status 1 for the same reason, since GUI tests earlier in the same process created Tk windows.

The fix on the Tk side is on branch mac-signal-handlers, but installed Tk builds will not have it for a long time. _tkinter should save the handlers of these signals before Tk_Init() and restore them after it.

Linked PRs
  • gh-157673

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece em _tkinter, na chamada a Tk_Init(), e rastreie como os manipuladores de sinais do macOS são afetados. Reproduza o problema com o script tkinter fornecido e, em seguida, verifique se criar uma instância de Tk preserva os manipuladores do Python e se Ctrl-C gera uma KeyboardInterrupt.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
desktop
Tipo de issue
Bug
Dificuldade
3/5
Tempo estimado
1-2 dias
Status de atividade
Estagnada
Clareza
Claramente especificada
Facilidade para iniciantes
35/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.