From 4920ff060ab2a7c37eb41b98c57ba819e9194399 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 14 Nov 2026 23:30:23 +0300 Subject: [PATCH] gh-161863: Fix possible `NULL` deref in `_PyErr_Display` (GH-161979) (cherry picked from commit 3403de5283fd44bba842638414e07999e0449723) Co-authored-by: sobolevn --- Python/pythonrun.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 698ee23780ae108..42582fced27f227 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1149,6 +1149,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",