aboutsummaryrefslogtreecommitdiff
path: root/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp')
-rw-r--r--lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
index 13dabb2..32020f9 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
@@ -69,6 +69,28 @@ Expected<std::string> python::As<std::string>(Expected<PythonObject> &&obj) {
return std::string(utf8.get());
}
+static bool python_is_finalizing() {
+#if 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();