Skip to content

Commit 3686e5b

Browse files
authored
[lldb] Eliminate (_)Py_IsFinalizing (NFC) (#152226)
Looking at the implementation of `pylifecycle.c` in cpython, finalizing and initialized are set at the same time. Therefore we can eliminate the call to `Py_IsFinalizing` and only check `Py_IsInitialized`, which is part of the stable API. I converted the check to an assert and confirmed that during my test suite runs, we never got into the if block. Because we check before taking the lock, there is an opportunity for a race, but that exact same race exists with the original code.
1 parent 056608a commit 3686e5b

File tree

1 file changed

+3
-16
lines changed

1 file changed

+3
-16
lines changed

lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,11 @@ Expected<std::string> python::As<std::string>(Expected<PythonObject> &&obj) {
7171
return std::string(utf8.get());
7272
}
7373

74-
static bool python_is_finalizing() {
75-
#if PY_VERSION_HEX >= 0x030d0000
76-
return Py_IsFinalizing();
77-
#else
78-
return _Py_IsFinalizing();
79-
#endif
80-
}
81-
8274
void PythonObject::Reset() {
8375
if (m_py_obj && Py_IsInitialized()) {
84-
if (python_is_finalizing()) {
85-
// Leak m_py_obj rather than crashing the process.
86-
// https://docs.python.org/3/c-api/init.html#c.PyGILState_Ensure
87-
} else {
88-
PyGILState_STATE state = PyGILState_Ensure();
89-
Py_DECREF(m_py_obj);
90-
PyGILState_Release(state);
91-
}
76+
PyGILState_STATE state = PyGILState_Ensure();
77+
Py_DECREF(m_py_obj);
78+
PyGILState_Release(state);
9279
}
9380
m_py_obj = nullptr;
9481
}

0 commit comments

Comments
 (0)