python / python/cpython

quitting a debugger based on the bdb module does not always stop code execution

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

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

stdlib type-feature
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

Bug description:

I encountered a bug while working with the Pyzo Python IDE, and the bug itself is located in the bdb module of Python's standard library. The same bug occurs when using the IDLE Python IDE, as it also uses bdb for the debugger.

When the debugger pauses code execution, e.g. at a breakpoint, the user can decide to quit debugging, which normally stops the execution of further code in the script to be debugged. But this does not work when the breakpoint is inside the "try" block of a try-except-clause and when exceptions are caught and ignored.

The Bdb class internally stops debugging by raising a BdbQuit error. But BdbQuit is like almost all exceptions derived from the Exception class, and therefore the script to be debugged can catch and dismiss the stop:

https://github.com/python/cpython/blob/1fc2b38d63170dd065efc2bc60e861f32c033b17/Lib/bdb.py#L15-L18

I think, it would be safer to change the definition of the BdbQuit class to:

class BdbQuit(BaseException):
    """Exception to give up completely."""

Though, it does not prevent the problem when there is an except clause like except: in the code to be debugged, but this is discouraged anyways.

As far as I have seen, the class definition of BdbQuit basically has not changed since Python v2.3, and so it predates BaseExceptions which where added to Python v2.5 via PEP 352.

Here is an example code to reproduce the problem and demonstrate the fix:

"""
* Save this code somewhere as a Python script (e.g. "/tmp/myscript.py").
* Launch IDLE (the Python IDE).
* Open this Python script in IDLE.
* Set breakpoints in the two code lines where indicated via comments.
    You can do this via the context menu when right-clicking on the line.
* Switch to the Python Shell window in IDLE (via menu "Run -> Python Shell".
* Open the Debugger (via menu "Debug -> Debugger" in the Python Shell).
* In the IDLE editor window, run the script (via menu "Run -> Run Module").
* Press the "Go" button in the "Debug Control" window.
* Execution stops at the line with the first breakpoint.
* Pressing the "Quit" button now would not execute any of the remaining lines.
* But we press "Go" instead of "Quit" to reach the second breakpoint.
* Now, really press "Quit", and see that script code execution is NOT stopped.
    There are some further text outputs from the print functions in the Shell
    window.
* Enable monkey-patching by changing "do_monkeypatch = False" to "do_monkeypatch = True".
    Or edit file "bdb.py" in Python's lib directory:
        change "class BdbQuit(Exception):" to "class BdbQuit(BaseException):"
* Do all the previous steps again, and you will see, that there is no more
    code execution in the script when quitting debugging at the second breakpoint.
"""

do_monkeypatch = False

if do_monkeypatch:
    import bdb

    if issubclass(bdb.BdbQuit, Exception):
        class BdbQuit(BaseException): pass

        bdb.BdbQuit = BdbQuit


print('this is before the first breakpoint')
a = 1  # <-- set a breakpoint in this line
print('this is after the first breakpoint')

try:
    print('this is before the second breakpoint')
    a = 2  # <-- set a breakpoint in this line
    print('this is after the second breakpoint, when continuing execution')
except Exception as e:
    print('this is after the second breakpoint, when quitting the debugger')
    print('got an exception, but ignoring it:', repr(e))

print('deleting important files :-(')
CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-149337

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 với Lib/bdb.py tại định nghĩa BdbQuit được liên kết trong issue, sau đó chạy script tái hiện IDLE được cung cấp với breakpoint bên trong khối try. Hoàn thành khi việc thoát khỏi trình gỡ lỗi ngăn không cho phần mã còn lại của script được thực thi ngay cả khi ngoại lệ được bắt; xem lại PR gh-149337 được liên kết trước khi bắt đầu.

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
devtools
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
Khá 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.