https://github.com/charles-zablit created 
https://github.com/llvm/llvm-project/pull/181160

Add checks around the `Py_init*` calls to check if Python was initialized 
correctly. If any of the calls fails, print an error and return early.

Python will fail to initialize if `%SystemRoot%` is not defined which causes 
lldb to crash. This patch prints an error if the initialization fails and 
returns early. lldb still crashes but gives a clue as to why that's the case.

>From 442c48f4f3c75d17b2c41795df01d335e5bcd403 Mon Sep 17 00:00:00 2001
From: Charles Zablit <[email protected]>
Date: Thu, 12 Feb 2026 14:57:05 +0000
Subject: [PATCH] [lldb][windows] print an error if Python fails to initialize

---
 .../Python/ScriptInterpreterPython.cpp        | 20 +++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git 
a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp 
b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
index 35a772c1454df..9af480c4113d9 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -120,14 +120,30 @@ struct InitializePythonRAII {
       return spec.GetPath();
     }();
     if (!g_python_home.empty()) {
-      PyConfig_SetBytesString(&config, &config.home, g_python_home.c_str());
+      PyStatus py_status =
+          PyConfig_SetBytesString(&config, &config.home, 
g_python_home.c_str());
+      if (py_status._type == PyStatus::_PyStatus_TYPE_ERROR) {
+        PyConfig_Clear(&config);
+        llvm::WithColor::error() << "Failed to set the Python config: '"
+                                 << py_status.err_msg << "'.\n";
+        return;
+      }
     }
 
     config.install_signal_handlers = 0;
-    Py_InitializeFromConfig(&config);
+    PyStatus py_status = Py_InitializeFromConfig(&config);
     PyConfig_Clear(&config);
+    if (py_status._type == PyStatus::_PyStatus_TYPE_ERROR) {
+      llvm::WithColor::error()
+          << "Python failed to initialize: '" << py_status.err_msg << "'.\n";
+      return;
+    }
 #else
     Py_InitializeEx(/*install_sigs=*/0);
+    if (!Py_IsInitialized()) {
+      llvm::WithColor::error() << "Python failed to initialize.\n";
+      return;
+    }
 #endif
 
     // The only case we should go further and acquire the GIL: it is unlocked.

_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to