python / python/cpython

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

オープン
#157,672 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

OS-mac topic-tkinter type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

_tkinter の Tk_Init() 呼び出しから開始し、macOS のシグナルハンドラがどのような影響を受けるかを追跡します。提供された tkinter スクリプトで問題を再現し、その後、Tk インスタンスを作成しても Python のハンドラが保持され、Ctrl-C によって KeyboardInterrupt が発生することを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
desktop
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。