[Lldb-commits] [lldb] [lldb][windows] print an error if Python fails to initialize (PR #181160)
Charles Zablit via lldb-commits
lldb-commits at lists.llvm.org
Mon Feb 16 10:17:01 PST 2026
https://github.com/charles-zablit updated https://github.com/llvm/llvm-project/pull/181160
>From 442c48f4f3c75d17b2c41795df01d335e5bcd403 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Thu, 12 Feb 2026 14:57:05 +0000
Subject: [PATCH 1/6] [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.
>From 8ace7e584c325e90e0648b1dfdf10800d7f1492e Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Fri, 13 Feb 2026 12:22:44 +0100
Subject: [PATCH 2/6] remove private API usage
---
.../Python/ScriptInterpreterPython.cpp | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
index 9af480c4113d9..c954f167a22b8 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -120,22 +120,22 @@ struct InitializePythonRAII {
return spec.GetPath();
}();
if (!g_python_home.empty()) {
- PyStatus py_status =
+ PyStatus status =
PyConfig_SetBytesString(&config, &config.home, g_python_home.c_str());
- if (py_status._type == PyStatus::_PyStatus_TYPE_ERROR) {
+ if (PyStatus_Exception(status)) {
PyConfig_Clear(&config);
- llvm::WithColor::error() << "Failed to set the Python config: '"
- << py_status.err_msg << "'.\n";
+ llvm::WithColor::error()
+ << "Failed to set the Python config: '" << status.err_msg << "'.\n";
return;
}
}
config.install_signal_handlers = 0;
- PyStatus py_status = Py_InitializeFromConfig(&config);
+ PyStatus status = Py_InitializeFromConfig(&config);
PyConfig_Clear(&config);
- if (py_status._type == PyStatus::_PyStatus_TYPE_ERROR) {
+ if (PyStatus_Exception(status)) {
llvm::WithColor::error()
- << "Python failed to initialize: '" << py_status.err_msg << "'.\n";
+ << "Python failed to initialize: '" << status.err_msg << "'.\n";
return;
}
#else
>From 83418baf076ecc100a5e5d41f7dc7eb86b91a721 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Mon, 16 Feb 2026 16:38:27 +0100
Subject: [PATCH 3/6] create standalone method instead of initializing in
constructor
---
.../ScriptInterpreter/Python/ScriptInterpreterPython.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
index c954f167a22b8..bd2ffa4cb0349 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -91,7 +91,7 @@ namespace {
// save off initial state at the beginning, and restore it at the end
struct InitializePythonRAII {
public:
- InitializePythonRAII() {
+ void DoInitialize() {
// The table of built-in modules can only be extended before Python is
// initialized.
if (!Py_IsInitialized()) {
@@ -3039,6 +3039,7 @@ void ScriptInterpreterPythonImpl::Initialize() {
// restoring various other pieces of state that can get mucked with during
// initialization.
InitializePythonRAII initialize_guard;
+ initialize_guard.DoInitialize();
LLDBSwigPyInit();
>From 86631a7cb1850563c351090657200f3846991c26 Mon Sep 17 00:00:00 2001
From: Charles Zablit <c_zablit at apple.com>
Date: Mon, 16 Feb 2026 16:49:03 +0100
Subject: [PATCH 4/6] switch to llvm::report_fatal_error
---
.../Python/ScriptInterpreterPython.cpp | 21 ++++++-------------
1 file changed, 6 insertions(+), 15 deletions(-)
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
index bd2ffa4cb0349..016b4f5c600f9 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -122,28 +122,19 @@ struct InitializePythonRAII {
if (!g_python_home.empty()) {
PyStatus status =
PyConfig_SetBytesString(&config, &config.home, g_python_home.c_str());
- if (PyStatus_Exception(status)) {
- PyConfig_Clear(&config);
- llvm::WithColor::error()
- << "Failed to set the Python config: '" << status.err_msg << "'.\n";
- return;
- }
+ if (PyStatus_Exception(status))
+ llvm::report_fatal_error(llvm::Twine("Failed to set the Python config: '") + status.err_msg + "'.");
}
config.install_signal_handlers = 0;
PyStatus status = Py_InitializeFromConfig(&config);
PyConfig_Clear(&config);
- if (PyStatus_Exception(status)) {
- llvm::WithColor::error()
- << "Python failed to initialize: '" << status.err_msg << "'.\n";
- return;
- }
+ if (PyStatus_Exception(status))
+ llvm::report_fatal_error(llvm::Twine("Python failed to initialize: '") + status.err_msg + "'.");
#else
Py_InitializeEx(/*install_sigs=*/0);
- if (!Py_IsInitialized()) {
- llvm::WithColor::error() << "Python failed to initialize.\n";
- return;
- }
+ if (!Py_IsInitialized())
+ llvm::report_fatal_error("Python failed to initialize.");
#endif
// The only case we should go further and acquire the GIL: it is unlocked.
>From 1bf62ae724637f5b986178a388351d62084abb21 Mon Sep 17 00:00:00 2001
From: Charles Zablit <zablitcharles at gmail.com>
Date: Mon, 16 Feb 2026 19:16:25 +0100
Subject: [PATCH 5/6] Update
lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
Co-authored-by: Jonas Devlieghere <jonas at devlieghere.com>
---
.../ScriptInterpreter/Python/ScriptInterpreterPython.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
index 016b4f5c600f9..464fa6c499cdc 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -123,7 +123,7 @@ struct InitializePythonRAII {
PyStatus status =
PyConfig_SetBytesString(&config, &config.home, g_python_home.c_str());
if (PyStatus_Exception(status))
- llvm::report_fatal_error(llvm::Twine("Failed to set the Python config: '") + status.err_msg + "'.");
+ llvm::report_fatal_error(llvm::Twine("failed to set the Python config: '") + status.err_msg + "'");
}
config.install_signal_handlers = 0;
>From 6862b7e6f4f288ed458bd58ffa65ef92195b34b5 Mon Sep 17 00:00:00 2001
From: Charles Zablit <zablitcharles at gmail.com>
Date: Mon, 16 Feb 2026 19:16:51 +0100
Subject: [PATCH 6/6] Apply suggestions from code review
Co-authored-by: Jonas Devlieghere <jonas at devlieghere.com>
---
.../ScriptInterpreter/Python/ScriptInterpreterPython.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
index 464fa6c499cdc..4f929029ebcea 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -130,11 +130,11 @@ struct InitializePythonRAII {
PyStatus status = Py_InitializeFromConfig(&config);
PyConfig_Clear(&config);
if (PyStatus_Exception(status))
- llvm::report_fatal_error(llvm::Twine("Python failed to initialize: '") + status.err_msg + "'.");
+ llvm::report_fatal_error(llvm::Twine("Python failed to initialize: '") + status.err_msg + "'");
#else
Py_InitializeEx(/*install_sigs=*/0);
if (!Py_IsInitialized())
- llvm::report_fatal_error("Python failed to initialize.");
+ llvm::report_fatal_error("python failed to initialize");
#endif
// The only case we should go further and acquire the GIL: it is unlocked.
More information about the lldb-commits
mailing list