Skip to content

Commit d5698cb

Browse files
committed
Fix initialization of the faulthandler module
faulthandler requires the importlib if "-X faulthandler" option is present on the command line, so initialize faulthandler after importlib. Add also an unit test.
1 parent 60622ed commit d5698cb

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

‎Lib/test/test_faulthandler.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import subprocess
88
import sys
99
from test import support, script_helper
10+
from test.script_helper import assert_python_ok
1011
import tempfile
1112
import unittest
1213

@@ -256,6 +257,20 @@ def test_is_enabled(self):
256257
finally:
257258
sys.stderr = orig_stderr
258259

260+
def test_disabled_by_default(self):
261+
# By default, the module should be disabled
262+
code = "import faulthandler; print(faulthandler.is_enabled())"
263+
rc, stdout, stderr = assert_python_ok("-c", code)
264+
stdout = (stdout + stderr).strip()
265+
self.assertEqual(stdout, b"False")
266+
267+
def test_sys_xoptions(self):
268+
# Test python -X faulthandler
269+
code = "import faulthandler; print(faulthandler.is_enabled())"
270+
rc, stdout, stderr = assert_python_ok("-X", "faulthandler", "-c", code)
271+
stdout = (stdout + stderr).strip()
272+
self.assertEqual(stdout, b"True")
273+
259274
def check_dump_traceback(self, filename):
260275
"""
261276
Explicitly call dump_traceback() function and check its output.

‎Python/pythonrun.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,6 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
356356

357357
_PyImportHooks_Init();
358358

359-
/* initialize the faulthandler module */
360-
if (_PyFaulthandler_Init())
361-
Py_FatalError("Py_Initialize: can't initialize faulthandler");
362-
363359
/* Initialize _warnings. */
364360
_PyWarnings_Init();
365361

@@ -368,6 +364,10 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
368364

369365
import_init(interp, sysmod);
370366

367+
/* initialize the faulthandler module */
368+
if (_PyFaulthandler_Init())
369+
Py_FatalError("Py_Initialize: can't initialize faulthandler");
370+
371371
_PyTime_Init();
372372

373373
if (initfsencoding(interp) < 0)

0 commit comments

Comments
 (0)