diff options
author | Pavel Labath <pavel@labath.sk> | 2022-01-17 11:29:35 +0100 |
---|---|---|
committer | Pavel Labath <pavel@labath.sk> | 2022-01-18 10:28:58 +0100 |
commit | c154f397eeb86ea1a5b8fa46405104ace962cec3 (patch) | |
tree | df2a6b975c72e1933494dab0a909e1253035e6e8 /lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h | |
parent | cc0d208805c3d1dac3ec5a44f971b1b5c8ab3a2a (diff) | |
download | llvm-c154f397eeb86ea1a5b8fa46405104ace962cec3.zip llvm-c154f397eeb86ea1a5b8fa46405104ace962cec3.tar.gz llvm-c154f397eeb86ea1a5b8fa46405104ace962cec3.tar.bz2 |
[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
Diffstat (limited to 'lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h')
-rw-r--r-- | lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h | 57 |
1 files changed, 24 insertions, 33 deletions
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h index 9d2cdca..7dd8a74 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h +++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h @@ -83,39 +83,6 @@ protected: 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 @@ public: } }; +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 |