aboutsummaryrefslogtreecommitdiff
path: root/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
diff options
context:
space:
mode:
authorPavel Labath <pavel@labath.sk>2022-01-19 11:59:19 +0100
committerPavel Labath <pavel@labath.sk>2022-01-19 12:49:46 +0100
commit6ff4af8e182333740a58176a3e9cbc84f6828216 (patch)
tree07940d4f8a814627aed9a2aab2b39bd6e7364b1a /lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
parent6eb8fc924485facd911b49370f53ab93728b2935 (diff)
downloadllvm-6ff4af8e182333740a58176a3e9cbc84f6828216.zip
llvm-6ff4af8e182333740a58176a3e9cbc84f6828216.tar.gz
llvm-6ff4af8e182333740a58176a3e9cbc84f6828216.tar.bz2
[lldb] Fix D114722 for python<=3.6
_Py_IsFinalizing was called _Py_Finalizing back then (and it was a variable instead of a function).
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();