diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst index d9beda92aba6a3..0a0084cabd20de 100644 --- a/Doc/whatsnew/3.16.rst +++ b/Doc/whatsnew/3.16.rst @@ -86,10 +86,14 @@ New modules Improved modules ================ -module_name ------------ +bdb +--- + +* :exc:`bdb.BdbQuit` is now a subclass of :exc:`BaseException`, instead of + :exc:`Exception`, to allow it to be used for cleanly exiting the debugger without + being accidentally caught by user code. + (Contributed by Tian Gao in :gh:`149337`.) -* TODO .. Add improved modules above alphabetically, not here at the end. diff --git a/Lib/bdb.py b/Lib/bdb.py index 50cf2b3f5b3e45..9d4243d8ff7b72 100644 --- a/Lib/bdb.py +++ b/Lib/bdb.py @@ -13,7 +13,7 @@ GENERATOR_AND_COROUTINE_FLAGS = CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR -class BdbQuit(Exception): +class BdbQuit(BaseException): """Exception to give up completely.""" diff --git a/Lib/test/test_bdb.py b/Lib/test/test_bdb.py index f15dae13eb384e..53fda185eb7677 100644 --- a/Lib/test/test_bdb.py +++ b/Lib/test/test_bdb.py @@ -1237,6 +1237,15 @@ def test_format_stack_entry_no_lineno(self): self.assertIn('Warning: lineno is None', Bdb().format_stack_entry((sys._getframe(), None))) + def test_bdb_quit_base_exception(self): + # gh-149309 + try: + raise _bdb.BdbQuit() + except Exception: + self.fail(f'BdbQuit should not be caught by Exception') + except BaseException: + pass + if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS.d/next/Library/2026-05-03-22-52-07.gh-issue-149309.gXhk9Y.rst b/Misc/NEWS.d/next/Library/2026-05-03-22-52-07.gh-issue-149309.gXhk9Y.rst new file mode 100644 index 00000000000000..69df9f1f6daff3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-05-03-22-52-07.gh-issue-149309.gXhk9Y.rst @@ -0,0 +1 @@ +:exc:`bdb.BdbQuit` is now a subclass of :exc:`BaseException`, instead of :exc:`Exception`, to allow it to be used for cleanly exiting the debugger without being accidentally caught by user code.