python / python/cpython

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

Đang mở
#157,672 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

OS-mac topic-tkinter type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu trong _tkinter tại lệnh gọi Tk_Init() và theo dõi cách các trình xử lý tín hiệu macOS bị ảnh hưởng. Tái hiện vấn đề bằng script tkinter được cung cấp, sau đó xác minh rằng việc tạo một instance Tk vẫn giữ nguyên các trình xử lý Python và Ctrl-C sẽ gây ra KeyboardInterrupt.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
desktop
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.