diff options
author | Jonas Devlieghere <jonas@devlieghere.com> | 2024-02-17 20:55:33 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-17 20:55:33 -0800 |
commit | 5c96e71d0d49dd55711ccdb57a22d033fe7a8fae (patch) | |
tree | 9b1bca4f8d5a4299b71aed616fa8cae744cf3782 | |
parent | 4206d06130834cb79b641221261c4b5d7a174320 (diff) |
[lldb] Don't rely on ScriptInterpreterPythonImpl::Initialize in the unit tests (#82096)
The unit tests only test the Python objects and don't actually use
anything from the LLDB module. That means that all the additional
complexity in ScriptInterpreterPythonImpl::Initialize is overkill.
By doing the initialization by hand, we avoid the annoying
ModuleNotFoundError.
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'lldb'
The error is the result of us stubbing out the SWIG (specifically
`PyInit__lldb`) because we cannot link against libLLDB from the unit
tests.
The downside of doing the initialization manually is that we do lose a
bit of test coverage. For example, issue #70453 also manifested itself
in the unit tests.
-rw-r--r-- | lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp | 26 |
1 files changed, 5 insertions, 21 deletions
diff --git a/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp b/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp index 5f0cc4c23db7..23162436d42c 100644 --- a/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp +++ b/lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp @@ -9,43 +9,27 @@ #include "gtest/gtest.h" #include "Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h" -#include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h" -#include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h" #include "Plugins/ScriptInterpreter/Python/lldb-python.h" -#include "lldb/Host/FileSystem.h" -#include "lldb/Host/HostInfo.h" - #include "PythonTestSuite.h" #include <optional> -using namespace lldb_private; -class TestScriptInterpreterPython : public ScriptInterpreterPythonImpl { -public: - using ScriptInterpreterPythonImpl::Initialize; -}; - void PythonTestSuite::SetUp() { - FileSystem::Initialize(); - HostInfoBase::Initialize(); - // ScriptInterpreterPython::Initialize() depends on HostInfo being - // initializedso it can compute the python directory etc. - TestScriptInterpreterPython::Initialize(); - // Although we don't care about concurrency for the purposes of running // this test suite, Python requires the GIL to be locked even for // deallocating memory, which can happen when you call Py_DECREF or // Py_INCREF. So acquire the GIL for the entire duration of this // test suite. + Py_InitializeEx(0); m_gil_state = PyGILState_Ensure(); + PyRun_SimpleString("import sys"); } void PythonTestSuite::TearDown() { PyGILState_Release(m_gil_state); - TestScriptInterpreterPython::Terminate(); - HostInfoBase::Terminate(); - FileSystem::Terminate(); + // We could call Py_FinalizeEx here, but initializing and finalizing Python is + // pretty slow, so just keep Python initialized across tests. } // The following functions are the Pythonic implementations of the required @@ -219,7 +203,7 @@ bool lldb_private::python::SWIGBridge::LLDBSwigPythonCallCommandObject( } bool lldb_private::python::SWIGBridge::LLDBSwigPythonCallParsedCommandObject( - PyObject *implementor, lldb::DebuggerSP debugger, + PyObject *implementor, lldb::DebuggerSP debugger, StructuredDataImpl &args_impl, lldb_private::CommandReturnObject &cmd_retobj, lldb::ExecutionContextRefSP exe_ctx_ref_sp) { |