Author: Jonas Devlieghere
Date: 2025-08-04T10:44:32-07:00
New Revision: ad623a813e6d9ffdef62270656245b35ffceda37

URL: 
https://github.com/llvm/llvm-project/commit/ad623a813e6d9ffdef62270656245b35ffceda37
DIFF: 
https://github.com/llvm/llvm-project/commit/ad623a813e6d9ffdef62270656245b35ffceda37.diff

LOG: [lldb] Eliminate PyGILState_Check (NFC) (#152006)

Eliminate calls to PyGILState_Check, which is not part of the Python
Limited C API. In the Locker, we can use PyGILState_Ensure directly. We
could do something similar to replace the assert, but I don't think it's
worth it. We don't assert that we hold the GIL anywhere else.

Added: 
    

Modified: 
    lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
    lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp

Removed: 
    


################################################################################
diff  --git 
a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp 
b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
index b07415e899bf5..e124954057520 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
@@ -261,7 +261,6 @@ PythonObject 
PythonObject::GetAttributeValue(llvm::StringRef attr) const {
 }
 
 StructuredData::ObjectSP PythonObject::CreateStructuredObject() const {
-  assert(PyGILState_Check());
   switch (GetObjectType()) {
   case PyObjectType::Dictionary:
     return PythonDictionary(PyRefType::Borrowed, m_py_obj)

diff  --git 
a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp 
b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
index adc172c4a3744..24d604f22a765 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -139,11 +139,12 @@ struct InitializePythonRAII {
     PyConfig_Clear(&config);
 
     // The only case we should go further and acquire the GIL: it is unlocked.
-    if (PyGILState_Check())
+    PyGILState_STATE gil_state = PyGILState_Ensure();
+    if (gil_state != PyGILState_UNLOCKED)
       return;
 
     m_was_already_initialized = true;
-    m_gil_state = PyGILState_Ensure();
+    m_gil_state = gil_state;
     LLDB_LOGV(GetLog(LLDBLog::Script),
               "Ensured PyGILState. Previous state = {0}",
               m_gil_state == PyGILState_UNLOCKED ? "unlocked" : "locked");


        
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to