[Lldb-commits] [lldb] [lldb][windows] print an error if Python fails to initialize (PR #181160)
via lldb-commits
lldb-commits at lists.llvm.org
Thu Feb 12 07:02:46 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
Author: Charles Zablit (charles-zablit)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/181160.diff
1 Files Affected:
- (modified) lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp (+18-2)
``````````diff
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.
``````````
</details>
https://github.com/llvm/llvm-project/pull/181160
More information about the lldb-commits
mailing list