aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2022-01-27 22:17:16 +0000
committerDimitry Andric <dim@FreeBSD.org>2022-05-14 11:44:34 +0000
commit04eeddc0aa8e0a417a16eaf9d7d095207f4a8623 (patch)
tree2a5d3b2fe5c852e91531d128d9177754572d5338 /contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
parent0eae32dcef82f6f06de6419a0d623d7def0cc8f6 (diff)
parent6f8fc217eaa12bf657be1c6468ed9938d10168b3 (diff)
Diffstat (limited to 'contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp')
-rw-r--r--contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp30
1 files changed, 29 insertions, 1 deletions
diff --git a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
index 7c71c9329e57..68f4e90d70f6 100644
--- a/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
+++ b/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
@@ -69,6 +69,30 @@ Expected<std::string> python::As<std::string>(Expected<PythonObject> &&obj) {
return std::string(utf8.get());
}
+static bool python_is_finalizing() {
+#if PY_MAJOR_VERSION == 2
+ return false;
+#elif PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 7
+ return _Py_Finalizing != nullptr;
+#else
+ return _Py_IsFinalizing();
+#endif
+}
+
+void PythonObject::Reset() {
+ if (m_py_obj && Py_IsInitialized()) {
+ if (python_is_finalizing()) {
+ // Leak m_py_obj rather than crashing the process.
+ // https://docs.python.org/3/c-api/init.html#c.PyGILState_Ensure
+ } else {
+ PyGILState_STATE state = PyGILState_Ensure();
+ Py_DECREF(m_py_obj);
+ PyGILState_Release(state);
+ }
+ }
+ m_py_obj = nullptr;
+}
+
Expected<long long> PythonObject::AsLongLong() const {
if (!m_py_obj)
return nullDeref();
@@ -257,6 +281,9 @@ PythonObject PythonObject::GetAttributeValue(llvm::StringRef attr) const {
}
StructuredData::ObjectSP PythonObject::CreateStructuredObject() const {
+#if PY_MAJOR_VERSION >= 3
+ assert(PyGILState_Check());
+#endif
switch (GetObjectType()) {
case PyObjectType::Dictionary:
return PythonDictionary(PyRefType::Borrowed, m_py_obj)
@@ -279,7 +306,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)));
}
}