[Lldb-commits] [lldb] c154f39 - [lldb/python] Use PythonObject in LLDBSwigPython functions

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Tue Jan 18 01:29:04 PST 2022


Author: Pavel Labath
Date: 2022-01-18T10:28:58+01:00
New Revision: c154f397eeb86ea1a5b8fa46405104ace962cec3

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

LOG: [lldb/python] Use PythonObject in LLDBSwigPython functions

Return our PythonObject wrappers instead of raw PyObjects (obfuscated as
void *). This ensures that ownership (reference counts) of python
objects is automatically tracked.

Differential Revision: https://reviews.llvm.org/D117462

Added: 
    

Modified: 
    lldb/bindings/python/python-wrapper.swig
    lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
    lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
    lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
    lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
    lldb/source/Plugins/ScriptInterpreter/Python/ScriptedProcessPythonInterface.cpp
    lldb/source/Plugins/ScriptInterpreter/Python/ScriptedThreadPythonInterface.cpp
    lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/bindings/python/python-wrapper.swig b/lldb/bindings/python/python-wrapper.swig
index 4f1d65200b10f..626fc47bebb9f 100644
--- a/lldb/bindings/python/python-wrapper.swig
+++ b/lldb/bindings/python/python-wrapper.swig
@@ -152,12 +152,12 @@ bool lldb_private::LLDBSwigPythonCallTypeScript(
   return true;
 }
 
-void *lldb_private::LLDBSwigPythonCreateSyntheticProvider(
+PythonObject lldb_private::LLDBSwigPythonCreateSyntheticProvider(
     const char *python_class_name, const char *session_dictionary_name,
     const lldb::ValueObjectSP &valobj_sp) {
   if (python_class_name == NULL || python_class_name[0] == '\0' ||
       !session_dictionary_name)
-    Py_RETURN_NONE;
+    return PythonObject();
 
   PyErr_Cleaner py_err_cleaner(true);
 
@@ -167,29 +167,29 @@ void *lldb_private::LLDBSwigPythonCreateSyntheticProvider(
       python_class_name, dict);
 
   if (!pfunc.IsAllocated())
-    Py_RETURN_NONE;
+    return PythonObject();
 
   auto sb_value = std::make_unique<lldb::SBValue>(valobj_sp);
   sb_value->SetPreferSyntheticValue(false);
 
   PythonObject val_arg = ToSWIGWrapper(std::move(sb_value));
   if (!val_arg.IsAllocated())
-    Py_RETURN_NONE;
+    return PythonObject();
 
   PythonObject result = pfunc(val_arg, dict);
 
   if (result.IsAllocated())
-    return result.release();
+    return result;
 
-  Py_RETURN_NONE;
+  return PythonObject();
 }
 
-void *lldb_private::LLDBSwigPythonCreateCommandObject(
+PythonObject lldb_private::LLDBSwigPythonCreateCommandObject(
     const char *python_class_name, const char *session_dictionary_name,
     lldb::DebuggerSP debugger_sp) {
   if (python_class_name == NULL || python_class_name[0] == '\0' ||
       !session_dictionary_name)
-    Py_RETURN_NONE;
+    return PythonObject();
 
   PyErr_Cleaner py_err_cleaner(true);
   auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
@@ -198,24 +198,19 @@ void *lldb_private::LLDBSwigPythonCreateCommandObject(
       python_class_name, dict);
 
   if (!pfunc.IsAllocated())
-    return nullptr;
-
-  PythonObject result = pfunc(ToSWIGWrapper(std::move(debugger_sp)), dict);
+    return PythonObject();
 
-  if (result.IsAllocated())
-    return result.release();
-
-  Py_RETURN_NONE;
+  return pfunc(ToSWIGWrapper(std::move(debugger_sp)), dict);
 }
 
-void *lldb_private::LLDBSwigPythonCreateScriptedProcess(
+PythonObject lldb_private::LLDBSwigPythonCreateScriptedProcess(
     const char *python_class_name, const char *session_dictionary_name,
     const lldb::TargetSP &target_sp,
     const lldb_private::StructuredDataImpl &args_impl,
     std::string &error_string) {
   if (python_class_name == NULL || python_class_name[0] == '\0' ||
       !session_dictionary_name)
-    Py_RETURN_NONE;
+    return PythonObject();
 
   PyErr_Cleaner py_err_cleaner(true);
 
@@ -227,7 +222,7 @@ void *lldb_private::LLDBSwigPythonCreateScriptedProcess(
   if (!pfunc.IsAllocated()) {
     error_string.append("could not find script class: ");
     error_string.append(python_class_name);
-    return nullptr;
+    return PythonObject();
   }
 
   PythonObject target_arg = ToSWIGWrapper(target_sp);
@@ -240,7 +235,7 @@ void *lldb_private::LLDBSwigPythonCreateScriptedProcess(
         [&](const llvm::ErrorInfoBase &E) {
           error_string.append(E.message());
         });
-    Py_RETURN_NONE;
+    return PythonObject();
   }
 
   PythonObject result = {};
@@ -249,21 +244,17 @@ void *lldb_private::LLDBSwigPythonCreateScriptedProcess(
   } else {
     error_string.assign("wrong number of arguments in __init__, should be 2 "
                         "(not including self)");
-    Py_RETURN_NONE;
   }
-
-  if (result.IsAllocated())
-    return result.release();
-  Py_RETURN_NONE;
+  return result;
 }
 
-void *lldb_private::LLDBSwigPythonCreateScriptedThread(
+PythonObject lldb_private::LLDBSwigPythonCreateScriptedThread(
     const char *python_class_name, const char *session_dictionary_name,
     const lldb::ProcessSP &process_sp, const StructuredDataImpl &args_impl,
     std::string &error_string) {
   if (python_class_name == NULL || python_class_name[0] == '\0' ||
       !session_dictionary_name)
-    Py_RETURN_NONE;
+    return PythonObject();
 
   PyErr_Cleaner py_err_cleaner(true);
 
@@ -275,7 +266,7 @@ void *lldb_private::LLDBSwigPythonCreateScriptedThread(
   if (!pfunc.IsAllocated()) {
     error_string.append("could not find script class: ");
     error_string.append(python_class_name);
-    return nullptr;
+    return PythonObject();
   }
 
   llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
@@ -286,30 +277,24 @@ void *lldb_private::LLDBSwigPythonCreateScriptedThread(
         [&](const llvm::ErrorInfoBase &E) {
           error_string.append(E.message());
         });
-    Py_RETURN_NONE;
+    return PythonObject();
   }
 
-  PythonObject result = {};
-  if (arg_info.get().max_positional_args == 2) {
-    result = pfunc(ToSWIGWrapper(process_sp), ToSWIGWrapper(args_impl));
-  } else {
-    error_string.assign("wrong number of arguments in __init__, should be 2 "
-                        "(not including self)");
-    Py_RETURN_NONE;
-  }
+  if (arg_info.get().max_positional_args == 2)
+    return pfunc(ToSWIGWrapper(process_sp), ToSWIGWrapper(args_impl));
 
-  if (result.IsAllocated())
-    return result.release();
-  Py_RETURN_NONE;
+  error_string.assign("wrong number of arguments in __init__, should be 2 "
+                      "(not including self)");
+  return PythonObject();
 }
 
-void *lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
+PythonObject lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
     const char *python_class_name, const char *session_dictionary_name,
     const lldb_private::StructuredDataImpl &args_impl,
     std::string &error_string, const lldb::ThreadPlanSP &thread_plan_sp) {
   if (python_class_name == NULL || python_class_name[0] == '\0' ||
       !session_dictionary_name)
-    Py_RETURN_NONE;
+    return PythonObject();
 
   PyErr_Cleaner py_err_cleaner(true);
 
@@ -321,7 +306,7 @@ void *lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
   if (!pfunc.IsAllocated()) {
     error_string.append("could not find script class: ");
     error_string.append(python_class_name);
-    return nullptr;
+    return PythonObject();
   }
 
   PythonObject tp_arg = ToSWIGWrapper(thread_plan_sp);
@@ -334,7 +319,7 @@ void *lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
         [&](const llvm::ErrorInfoBase &E) {
           error_string.append(E.message());
         });
-    Py_RETURN_NONE;
+    return PythonObject();
   }
 
   PythonObject result = {};
@@ -343,7 +328,7 @@ void *lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
     if (args_sb->IsValid()) {
       error_string.assign(
           "args passed, but __init__ does not take an args dictionary");
-      Py_RETURN_NONE;
+      return PythonObject();
     }
     result = pfunc(tp_arg, dict);
   } else if (arg_info.get().max_positional_args >= 3) {
@@ -351,15 +336,13 @@ void *lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
   } else {
     error_string.assign("wrong number of arguments in __init__, should be 2 or "
                         "3 (not including self)");
-    Py_RETURN_NONE;
+    return PythonObject();
   }
 
   // FIXME: At this point we should check that the class we found supports all
   // the methods that we need.
 
-  if (result.IsAllocated())
-    return result.release();
-  Py_RETURN_NONE;
+  return result;
 }
 
 bool lldb_private::LLDBSWIGPythonCallThreadPlan(
@@ -400,14 +383,14 @@ bool lldb_private::LLDBSWIGPythonCallThreadPlan(
   return false;
 }
 
-void *lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
+PythonObject lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
     const char *python_class_name, const char *session_dictionary_name,
     const StructuredDataImpl &args_impl,
     const lldb::BreakpointSP &breakpoint_sp) {
 
   if (python_class_name == NULL || python_class_name[0] == '\0' ||
       !session_dictionary_name)
-    Py_RETURN_NONE;
+    return PythonObject();
 
   PyErr_Cleaner py_err_cleaner(true);
 
@@ -417,7 +400,7 @@ void *lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
       python_class_name, dict);
 
   if (!pfunc.IsAllocated())
-    return nullptr;
+    return PythonObject();
 
   PythonObject result =
       pfunc(ToSWIGWrapper(breakpoint_sp), ToSWIGWrapper(args_impl), dict);
@@ -428,11 +411,9 @@ void *lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
     // Check that __callback__ is defined:
     auto callback_func = result.ResolveName<PythonCallable>("__callback__");
     if (callback_func.IsAllocated())
-      return result.release();
-    else
-      result.release();
+      return result;
   }
-  Py_RETURN_NONE;
+  return PythonObject();
 }
 
 unsigned int lldb_private::LLDBSwigPythonCallBreakpointResolver(
@@ -474,17 +455,17 @@ unsigned int lldb_private::LLDBSwigPythonCallBreakpointResolver(
   return ret_val;
 }
 
-void *lldb_private::LLDBSwigPythonCreateScriptedStopHook(
+PythonObject lldb_private::LLDBSwigPythonCreateScriptedStopHook(
     lldb::TargetSP target_sp, const char *python_class_name,
     const char *session_dictionary_name, const StructuredDataImpl &args_impl,
     Status &error) {
   if (python_class_name == NULL || python_class_name[0] == '\0') {
     error.SetErrorString("Empty class name.");
-    Py_RETURN_NONE;
+    return PythonObject();
   }
   if (!session_dictionary_name) {
     error.SetErrorString("No session dictionary");
-    Py_RETURN_NONE;
+    return PythonObject();
   }
 
   PyErr_Cleaner py_err_cleaner(true);
@@ -497,7 +478,7 @@ void *lldb_private::LLDBSwigPythonCreateScriptedStopHook(
   if (!pfunc.IsAllocated()) {
     error.SetErrorStringWithFormat("Could not find class: %s.",
                                    python_class_name);
-    return nullptr;
+    return PythonObject();
   }
 
   PythonObject result =
@@ -514,23 +495,22 @@ void *lldb_private::LLDBSwigPythonCreateScriptedStopHook(
               "Wrong number of args for "
               "handle_stop callback, should be 2 (excluding self), got: %zu",
               num_args);
-          Py_RETURN_NONE;
+          return PythonObject();
         } else
-          return result.release();
+          return result;
       } else {
         error.SetErrorString("Couldn't get num arguments for handle_stop "
                              "callback.");
-        Py_RETURN_NONE;
+        return PythonObject();
       }
-      return result.release();
+      return result;
     } else {
       error.SetErrorStringWithFormat("Class \"%s\" is missing the required "
                                      "handle_stop callback.",
                                      python_class_name);
-      result.release();
     }
   }
-  Py_RETURN_NONE;
+  return PythonObject();
 }
 
 bool lldb_private::LLDBSwigPythonStopHookCallHandleStop(
@@ -842,12 +822,12 @@ bool lldb_private::LLDBSwigPythonCallCommandObject(
   return true;
 }
 
-void *lldb_private::LLDBSWIGPythonCreateOSPlugin(
+PythonObject lldb_private::LLDBSWIGPythonCreateOSPlugin(
     const char *python_class_name, const char *session_dictionary_name,
     const lldb::ProcessSP &process_sp) {
   if (python_class_name == NULL || python_class_name[0] == '\0' ||
       !session_dictionary_name)
-    Py_RETURN_NONE;
+    return PythonObject();
 
   PyErr_Cleaner py_err_cleaner(true);
 
@@ -857,21 +837,16 @@ void *lldb_private::LLDBSWIGPythonCreateOSPlugin(
       python_class_name, dict);
 
   if (!pfunc.IsAllocated())
-    Py_RETURN_NONE;
-
-  auto result = pfunc(ToSWIGWrapper(process_sp));
-
-  if (result.IsAllocated())
-    return result.release();
+    return PythonObject();
 
-  Py_RETURN_NONE;
+  return pfunc(ToSWIGWrapper(process_sp));
 }
 
-void *lldb_private::LLDBSWIGPython_CreateFrameRecognizer(
+PythonObject lldb_private::LLDBSWIGPython_CreateFrameRecognizer(
     const char *python_class_name, const char *session_dictionary_name) {
   if (python_class_name == NULL || python_class_name[0] == '\0' ||
       !session_dictionary_name)
-    Py_RETURN_NONE;
+    return PythonObject();
 
   PyErr_Cleaner py_err_cleaner(true);
 
@@ -881,14 +856,9 @@ void *lldb_private::LLDBSWIGPython_CreateFrameRecognizer(
       python_class_name, dict);
 
   if (!pfunc.IsAllocated())
-    Py_RETURN_NONE;
-
-  auto result = pfunc();
-
-  if (result.IsAllocated())
-    return result.release();
+    return PythonObject();
 
-  Py_RETURN_NONE;
+  return pfunc();
 }
 
 PyObject *lldb_private::LLDBSwigPython_GetRecognizedArguments(

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
index d68af672ae83e..13dabb2eff3cd 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
@@ -280,7 +280,8 @@ StructuredData::ObjectSP PythonObject::CreateStructuredObject() const {
   case PyObjectType::None:
     return StructuredData::ObjectSP();
   default:
-    return StructuredData::ObjectSP(new StructuredPythonObject(m_py_obj));
+    return StructuredData::ObjectSP(new StructuredPythonObject(
+        PythonObject(PyRefType::Borrowed, m_py_obj)));
   }
 }
 

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
index 9d2cdca45a633..7dd8a7426f15b 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
@@ -83,39 +83,6 @@ class GIL {
   PyGILState_STATE m_state;
 };
 
-class StructuredPythonObject : public StructuredData::Generic {
-public:
-  StructuredPythonObject() : StructuredData::Generic() {}
-
-  StructuredPythonObject(void *obj) : StructuredData::Generic(obj) {
-    assert(PyGILState_Check());
-    Py_XINCREF(GetValue());
-  }
-
-  ~StructuredPythonObject() override {
-    if (Py_IsInitialized()) {
-      if (_Py_IsFinalizing()) {
-        // Leak GetValue() rather than crashing the process.
-        // https://docs.python.org/3/c-api/init.html#c.PyGILState_Ensure
-      } else {
-        PyGILState_STATE state = PyGILState_Ensure();
-        Py_XDECREF(GetValue());
-        PyGILState_Release(state);
-      }
-    }
-    SetValue(nullptr);
-  }
-
-  bool IsValid() const override { return GetValue() && GetValue() != Py_None; }
-
-  void Serialize(llvm::json::OStream &s) const override;
-
-private:
-  StructuredPythonObject(const StructuredPythonObject &) = delete;
-  const StructuredPythonObject &
-  operator=(const StructuredPythonObject &) = delete;
-};
-
 enum class PyObjectType {
   Unknown,
   None,
@@ -784,6 +751,30 @@ class PythonScript {
   }
 };
 
+class StructuredPythonObject : public StructuredData::Generic {
+public:
+  StructuredPythonObject() : StructuredData::Generic() {}
+
+  // Take ownership of the object we received.
+  StructuredPythonObject(PythonObject obj)
+      : StructuredData::Generic(obj.release()) {}
+
+  ~StructuredPythonObject() override {
+    // Hand ownership back to a (temporary) PythonObject instance and let it
+    // take care of releasing it.
+    PythonObject(PyRefType::Owned, static_cast<PyObject *>(GetValue()));
+  }
+
+  bool IsValid() const override { return GetValue() && GetValue() != Py_None; }
+
+  void Serialize(llvm::json::OStream &s) const override;
+
+private:
+  StructuredPythonObject(const StructuredPythonObject &) = delete;
+  const StructuredPythonObject &
+  operator=(const StructuredPythonObject &) = delete;
+};
+
 } // namespace python
 } // namespace lldb_private
 

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h b/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
index 2bb69dc477313..4df2353567374 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
@@ -18,6 +18,7 @@
 // LLDB Python header must be included first
 #include "lldb-python.h"
 
+#include "Plugins/ScriptInterpreter/Python/PythonDataObjects.h"
 #include "lldb/lldb-forward.h"
 #include "lldb/lldb-types.h"
 #include "llvm/Support/Error.h"
@@ -54,17 +55,15 @@ void *LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *data);
 // Although these are scripting-language specific, their definition depends on
 // the public API.
 
-void *LLDBSwigPythonCreateScriptedProcess(const char *python_class_name,
-                                          const char *session_dictionary_name,
-                                          const lldb::TargetSP &target_sp,
-                                          const StructuredDataImpl &args_impl,
-                                          std::string &error_string);
+python::PythonObject LLDBSwigPythonCreateScriptedProcess(
+    const char *python_class_name, const char *session_dictionary_name,
+    const lldb::TargetSP &target_sp, const StructuredDataImpl &args_impl,
+    std::string &error_string);
 
-void *LLDBSwigPythonCreateScriptedThread(const char *python_class_name,
-                                         const char *session_dictionary_name,
-                                         const lldb::ProcessSP &process_sp,
-                                         const StructuredDataImpl &args_impl,
-                                         std::string &error_string);
+python::PythonObject LLDBSwigPythonCreateScriptedThread(
+    const char *python_class_name, const char *session_dictionary_name,
+    const lldb::ProcessSP &process_sp, const StructuredDataImpl &args_impl,
+    std::string &error_string);
 
 llvm::Expected<bool> LLDBSwigPythonBreakpointCallbackFunction(
     const char *python_function_name, const char *session_dictionary_name,
@@ -83,16 +82,17 @@ bool LLDBSwigPythonCallTypeScript(const char *python_function_name,
                                   const lldb::TypeSummaryOptionsSP &options_sp,
                                   std::string &retval);
 
-void *
+python::PythonObject
 LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name,
                                       const char *session_dictionary_name,
                                       const lldb::ValueObjectSP &valobj_sp);
 
-void *LLDBSwigPythonCreateCommandObject(const char *python_class_name,
-                                        const char *session_dictionary_name,
-                                        lldb::DebuggerSP debugger_sp);
+python::PythonObject
+LLDBSwigPythonCreateCommandObject(const char *python_class_name,
+                                  const char *session_dictionary_name,
+                                  lldb::DebuggerSP debugger_sp);
 
-void *LLDBSwigPythonCreateScriptedThreadPlan(
+python::PythonObject LLDBSwigPythonCreateScriptedThreadPlan(
     const char *python_class_name, const char *session_dictionary_name,
     const StructuredDataImpl &args_data, std::string &error_string,
     const lldb::ThreadPlanSP &thread_plan_sp);
@@ -101,7 +101,7 @@ bool LLDBSWIGPythonCallThreadPlan(void *implementor, const char *method_name,
                                   lldb_private::Event *event_sp,
                                   bool &got_error);
 
-void *LLDBSwigPythonCreateScriptedBreakpointResolver(
+python::PythonObject LLDBSwigPythonCreateScriptedBreakpointResolver(
     const char *python_class_name, const char *session_dictionary_name,
     const StructuredDataImpl &args, const lldb::BreakpointSP &bkpt_sp);
 
@@ -109,11 +109,10 @@ unsigned int
 LLDBSwigPythonCallBreakpointResolver(void *implementor, const char *method_name,
                                      lldb_private::SymbolContext *sym_ctx);
 
-void *LLDBSwigPythonCreateScriptedStopHook(lldb::TargetSP target_sp,
-                                           const char *python_class_name,
-                                           const char *session_dictionary_name,
-                                           const StructuredDataImpl &args,
-                                           lldb_private::Status &error);
+python::PythonObject LLDBSwigPythonCreateScriptedStopHook(
+    lldb::TargetSP target_sp, const char *python_class_name,
+    const char *session_dictionary_name, const StructuredDataImpl &args,
+    lldb_private::Status &error);
 
 bool LLDBSwigPythonStopHookCallHandleStop(void *implementor,
                                           lldb::ExecutionContextRefSP exc_ctx,
@@ -150,12 +149,14 @@ bool LLDBSwigPythonCallModuleInit(const char *python_module_name,
                                   const char *session_dictionary_name,
                                   lldb::DebuggerSP debugger);
 
-void *LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
-                                   const char *session_dictionary_name,
-                                   const lldb::ProcessSP &process_sp);
+python::PythonObject
+LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
+                             const char *session_dictionary_name,
+                             const lldb::ProcessSP &process_sp);
 
-void *LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name,
-                                           const char *session_dictionary_name);
+python::PythonObject
+LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name,
+                                     const char *session_dictionary_name);
 
 PyObject *
 LLDBSwigPython_GetRecognizedArguments(PyObject *implementor,

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
index 96725afd279ed..0e01dc52b2513 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -1439,10 +1439,11 @@ ScriptInterpreterPythonImpl::CreateFrameRecognizer(const char *class_name) {
     return StructuredData::GenericSP();
 
   Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
-  void *ret_val = LLDBSWIGPython_CreateFrameRecognizer(
+  PythonObject ret_val = LLDBSWIGPython_CreateFrameRecognizer(
       class_name, m_dictionary_name.c_str());
 
-  return StructuredData::GenericSP(new StructuredPythonObject(ret_val));
+  return StructuredData::GenericSP(
+      new StructuredPythonObject(std::move(ret_val)));
 }
 
 lldb::ValueObjectListSP ScriptInterpreterPythonImpl::GetRecognizedArguments(
@@ -1498,10 +1499,11 @@ ScriptInterpreterPythonImpl::OSPlugin_CreatePluginObject(
     return StructuredData::GenericSP();
 
   Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
-  void *ret_val = LLDBSWIGPythonCreateOSPlugin(
+  PythonObject ret_val = LLDBSWIGPythonCreateOSPlugin(
       class_name, m_dictionary_name.c_str(), process_sp);
 
-  return StructuredData::GenericSP(new StructuredPythonObject(ret_val));
+  return StructuredData::GenericSP(
+      new StructuredPythonObject(std::move(ret_val)));
 }
 
 StructuredData::DictionarySP ScriptInterpreterPythonImpl::OSPlugin_RegisterInfo(
@@ -1749,13 +1751,14 @@ StructuredData::ObjectSP ScriptInterpreterPythonImpl::CreateScriptedThreadPlan(
 
   Locker py_lock(this,
                  Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
-  void *ret_val = LLDBSwigPythonCreateScriptedThreadPlan(
+  PythonObject ret_val = LLDBSwigPythonCreateScriptedThreadPlan(
       class_name, python_interpreter->m_dictionary_name.c_str(), args_data,
       error_str, thread_plan_sp);
   if (!ret_val)
     return {};
 
-  return StructuredData::ObjectSP(new StructuredPythonObject(ret_val));
+  return StructuredData::ObjectSP(
+      new StructuredPythonObject(std::move(ret_val)));
 }
 
 bool ScriptInterpreterPythonImpl::ScriptedThreadPlanExplainsStop(
@@ -1849,11 +1852,12 @@ ScriptInterpreterPythonImpl::CreateScriptedBreakpointResolver(
   Locker py_lock(this,
                  Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
 
-  void *ret_val = LLDBSwigPythonCreateScriptedBreakpointResolver(
+  PythonObject ret_val = LLDBSwigPythonCreateScriptedBreakpointResolver(
       class_name, python_interpreter->m_dictionary_name.c_str(), args_data,
       bkpt_sp);
 
-  return StructuredData::GenericSP(new StructuredPythonObject(ret_val));
+  return StructuredData::GenericSP(
+      new StructuredPythonObject(std::move(ret_val)));
 }
 
 bool ScriptInterpreterPythonImpl::ScriptedBreakpointResolverSearchCallback(
@@ -1920,11 +1924,12 @@ StructuredData::GenericSP ScriptInterpreterPythonImpl::CreateScriptedStopHook(
   Locker py_lock(this,
                  Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
 
-  void *ret_val = LLDBSwigPythonCreateScriptedStopHook(
+  PythonObject ret_val = LLDBSwigPythonCreateScriptedStopHook(
       target_sp, class_name, python_interpreter->m_dictionary_name.c_str(),
       args_data, error);
 
-  return StructuredData::GenericSP(new StructuredPythonObject(ret_val));
+  return StructuredData::GenericSP(
+      new StructuredPythonObject(std::move(ret_val)));
 }
 
 bool ScriptInterpreterPythonImpl::ScriptedStopHookHandleStop(
@@ -2015,10 +2020,11 @@ ScriptInterpreterPythonImpl::CreateSyntheticScriptedProvider(
 
   Locker py_lock(this,
                  Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
-  void *ret_val = LLDBSwigPythonCreateSyntheticProvider(
+  PythonObject ret_val = LLDBSwigPythonCreateSyntheticProvider(
       class_name, python_interpreter->m_dictionary_name.c_str(), valobj);
 
-  return StructuredData::ObjectSP(new StructuredPythonObject(ret_val));
+  return StructuredData::ObjectSP(
+      new StructuredPythonObject(std::move(ret_val)));
 }
 
 StructuredData::GenericSP
@@ -2033,10 +2039,11 @@ ScriptInterpreterPythonImpl::CreateScriptCommandObject(const char *class_name) {
 
   Locker py_lock(this,
                  Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
-  void *ret_val = LLDBSwigPythonCreateCommandObject(
+  PythonObject ret_val = LLDBSwigPythonCreateCommandObject(
       class_name, m_dictionary_name.c_str(), debugger_sp);
 
-  return StructuredData::GenericSP(new StructuredPythonObject(ret_val));
+  return StructuredData::GenericSP(
+      new StructuredPythonObject(std::move(ret_val)));
 }
 
 bool ScriptInterpreterPythonImpl::GenerateTypeScriptFunction(
@@ -2149,7 +2156,8 @@ bool ScriptInterpreterPythonImpl::GetScriptedSummary(
   if (new_callee && old_callee != new_callee) {
     Locker py_lock(this,
                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
-    callee_wrapper_sp = std::make_shared<StructuredPythonObject>(new_callee);
+    callee_wrapper_sp = std::make_shared<StructuredPythonObject>(
+        PythonObject(PyRefType::Borrowed, static_cast<PyObject *>(new_callee)));
   }
 
   return ret_val;
@@ -2802,7 +2810,8 @@ bool ScriptInterpreterPythonImpl::LoadScriptingModule(
             ScriptInterpreter::eScriptReturnTypeOpaqueObject, &module_pyobj,
             exc_options) &&
         module_pyobj)
-      *module_sp = std::make_shared<StructuredPythonObject>(module_pyobj);
+      *module_sp = std::make_shared<StructuredPythonObject>(PythonObject(
+          PyRefType::Owned, static_cast<PyObject *>(module_pyobj)));
   }
 
   return true;

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptedProcessPythonInterface.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptedProcessPythonInterface.cpp
index e3c1931a565ac..da8ff42213552 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptedProcessPythonInterface.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptedProcessPythonInterface.cpp
@@ -43,7 +43,7 @@ StructuredData::GenericSP ScriptedProcessPythonInterface::CreatePluginObject(
   Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
                  Locker::FreeLock);
 
-  void *ret_val = LLDBSwigPythonCreateScriptedProcess(
+  PythonObject ret_val = LLDBSwigPythonCreateScriptedProcess(
       class_name.str().c_str(), m_interpreter.GetDictionaryName(), target_sp,
       args_impl, error_string);
 
@@ -51,7 +51,7 @@ StructuredData::GenericSP ScriptedProcessPythonInterface::CreatePluginObject(
     return {};
 
   m_object_instance_sp =
-      StructuredData::GenericSP(new StructuredPythonObject(ret_val));
+      StructuredData::GenericSP(new StructuredPythonObject(std::move(ret_val)));
 
   return m_object_instance_sp;
 }

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptedThreadPythonInterface.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptedThreadPythonInterface.cpp
index 6a881bfe625c6..fb55d44aca840 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptedThreadPythonInterface.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptedThreadPythonInterface.cpp
@@ -43,7 +43,7 @@ StructuredData::GenericSP ScriptedThreadPythonInterface::CreatePluginObject(
   Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
                  Locker::FreeLock);
 
-  void *ret_val = LLDBSwigPythonCreateScriptedThread(
+  PythonObject ret_val = LLDBSwigPythonCreateScriptedThread(
       class_name.str().c_str(), m_interpreter.GetDictionaryName(), process_sp,
       args_impl, error_string);
 
@@ -51,7 +51,7 @@ StructuredData::GenericSP ScriptedThreadPythonInterface::CreatePluginObject(
     return {};
 
   m_object_instance_sp =
-      StructuredData::GenericSP(new StructuredPythonObject(ret_val));
+      StructuredData::GenericSP(new StructuredPythonObject(std::move(ret_val)));
 
   return m_object_instance_sp;
 }

diff  --git a/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp b/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
index 837f6cbf4dfa7..ba96cc67a68fe 100644
--- a/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
+++ b/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
@@ -80,23 +80,23 @@ bool lldb_private::LLDBSwigPythonCallTypeScript(
   return false;
 }
 
-void *lldb_private::LLDBSwigPythonCreateSyntheticProvider(
+python::PythonObject lldb_private::LLDBSwigPythonCreateSyntheticProvider(
     const char *python_class_name, const char *session_dictionary_name,
     const lldb::ValueObjectSP &valobj_sp) {
-  return nullptr;
+  return python::PythonObject();
 }
 
-void *lldb_private::LLDBSwigPythonCreateCommandObject(
+python::PythonObject lldb_private::LLDBSwigPythonCreateCommandObject(
     const char *python_class_name, const char *session_dictionary_name,
     lldb::DebuggerSP debugger_sp) {
-  return nullptr;
+  return python::PythonObject();
 }
 
-void *lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
+python::PythonObject lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
     const char *python_class_name, const char *session_dictionary_name,
     const StructuredDataImpl &args_data, std::string &error_string,
     const lldb::ThreadPlanSP &thread_plan_sp) {
-  return nullptr;
+  return python::PythonObject();
 }
 
 bool lldb_private::LLDBSWIGPythonCallThreadPlan(void *implementor,
@@ -106,10 +106,11 @@ bool lldb_private::LLDBSWIGPythonCallThreadPlan(void *implementor,
   return false;
 }
 
-void *lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
+python::PythonObject
+lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
     const char *python_class_name, const char *session_dictionary_name,
     const StructuredDataImpl &args, const lldb::BreakpointSP &bkpt_sp) {
-  return nullptr;
+  return python::PythonObject();
 }
 
 unsigned int lldb_private::LLDBSwigPythonCallBreakpointResolver(
@@ -191,30 +192,30 @@ bool lldb_private::LLDBSwigPythonCallModuleInit(
   return false;
 }
 
-void *
+python::PythonObject
 lldb_private::LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
                                            const char *session_dictionary_name,
                                            const lldb::ProcessSP &process_sp) {
-  return nullptr;
+  return python::PythonObject();
 }
 
-void *lldb_private::LLDBSwigPythonCreateScriptedProcess(
+python::PythonObject lldb_private::LLDBSwigPythonCreateScriptedProcess(
     const char *python_class_name, const char *session_dictionary_name,
     const lldb::TargetSP &target_sp, const StructuredDataImpl &args_impl,
     std::string &error_string) {
-  return nullptr;
+  return python::PythonObject();
 }
 
-void *lldb_private::LLDBSwigPythonCreateScriptedThread(
+python::PythonObject lldb_private::LLDBSwigPythonCreateScriptedThread(
     const char *python_class_name, const char *session_dictionary_name,
     const lldb::ProcessSP &process_sp, const StructuredDataImpl &args_impl,
     std::string &error_string) {
-  return nullptr;
+  return python::PythonObject();
 }
 
-void *lldb_private::LLDBSWIGPython_CreateFrameRecognizer(
+python::PythonObject lldb_private::LLDBSWIGPython_CreateFrameRecognizer(
     const char *python_class_name, const char *session_dictionary_name) {
-  return nullptr;
+  return python::PythonObject();
 }
 
 PyObject *lldb_private::LLDBSwigPython_GetRecognizedArguments(
@@ -257,11 +258,11 @@ void *lldb_private::LLDBSWIGPython_GetDynamicSetting(
   return nullptr;
 }
 
-void *lldb_private::LLDBSwigPythonCreateScriptedStopHook(
+python::PythonObject lldb_private::LLDBSwigPythonCreateScriptedStopHook(
     lldb::TargetSP target_sp, const char *python_class_name,
     const char *session_dictionary_name, const StructuredDataImpl &args_impl,
     Status &error) {
-  return nullptr;
+  return python::PythonObject();
 }
 
 bool lldb_private::LLDBSwigPythonStopHookCallHandleStop(


        


More information about the lldb-commits mailing list