Skip to content

Commit e6a23c8

Browse files
authored
bpo-30557: faulthandler now correctly filters and displays exception codes on Windows (#1924)
* bpo-30557: faulthandler now correctly filters and displays exception codes on Windows * Adds test for non-fatal exceptions. * Adds bpo number to comment.
1 parent 1bced56 commit e6a23c8

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

Lib/test/test_faulthandler.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,32 @@ def test_raise_exception(self):
754754
3,
755755
name)
756756

757+
@unittest.skipUnless(MS_WINDOWS, 'specific to Windows')
758+
def test_raise_nonfatal_exception(self):
759+
# These exceptions are not strictly errors. Letting
760+
# faulthandler display the traceback when they are
761+
# raised is likely to result in noise. However, they
762+
# may still terminate the process if there is no
763+
# handler installed for them (which there typically
764+
# is, e.g. for debug messages).
765+
for exc in (
766+
0x00000000,
767+
0x34567890,
768+
0x40000000,
769+
0x40001000,
770+
0x70000000,
771+
0x7FFFFFFF,
772+
):
773+
output, exitcode = self.get_output(f"""
774+
import faulthandler
775+
faulthandler.enable()
776+
faulthandler._raise_exception(0x{exc:x})
777+
"""
778+
)
779+
self.assertEqual(output, [])
780+
# Actual exception code has bit 4 cleared
781+
self.assertEqual(exitcode, exc & ~0x10000000)
782+
757783
@unittest.skipUnless(MS_WINDOWS, 'specific to Windows')
758784
def test_disable_windows_exc_handler(self):
759785
code = dedent("""

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ Extension Modules
345345
Library
346346
-------
347347

348+
- bpo-30557: faulthandler now correctly filters and displays exception codes
349+
on Windows
350+
348351
- bpo-30526: Add TextIOWrapper.reconfigure() and a TextIOWrapper.write_through
349352
attribute.
350353

Modules/faulthandler.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,8 @@ faulthandler_exc_handler(struct _EXCEPTION_POINTERS *exc_info)
373373
DWORD code = exc_info->ExceptionRecord->ExceptionCode;
374374
DWORD flags = exc_info->ExceptionRecord->ExceptionFlags;
375375

376-
/* only log fatal exceptions */
377-
if (flags & EXCEPTION_NONCONTINUABLE) {
376+
/* bpo-30557: only log fatal exceptions */
377+
if (!(code & 0x80000000)) {
378378
/* call the next exception handler */
379379
return EXCEPTION_CONTINUE_SEARCH;
380380
}
@@ -391,8 +391,8 @@ faulthandler_exc_handler(struct _EXCEPTION_POINTERS *exc_info)
391391
case EXCEPTION_IN_PAGE_ERROR: PUTS(fd, "page error"); break;
392392
case EXCEPTION_STACK_OVERFLOW: PUTS(fd, "stack overflow"); break;
393393
default:
394-
PUTS(fd, "code ");
395-
_Py_DumpDecimal(fd, code);
394+
PUTS(fd, "code 0x");
395+
_Py_DumpHexadecimal(fd, code, 8);
396396
}
397397
PUTS(fd, "\n\n");
398398

0 commit comments

Comments
 (0)