From ec62ba9aa7bfc7a1747a9718f96d21c8494de4c5 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 14 Nov 2026 08:33:08 +0300 Subject: [PATCH] gh-161863: Fix possible `NULL` deref in `_PyErr_Display` --- Python/pythonrun.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 5e026a93d5c1806..38003e60e985ca2 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1157,6 +1157,13 @@ _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb) } } + // gh-161863: Interpreter can be not fully initialized + // when calling this function. May be not safe to call the import. + PyInterpreterState *interp = _PyInterpreterState_GET(); + if (interp->imports.importlib == NULL) { + goto fallback; + } + // Try first with the stdlib traceback module PyObject *print_exception_fn = PyImport_ImportModuleAttrString( "traceback",