Skip to content

Commit 92d51cf

Browse files
committed
[lldb] Don't rely on ScriptInterpreterPythonImpl::Initialize in the unit tests (llvm#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 llvm#70453 also manifested itself in the unit tests. (cherry picked from commit 5c96e71)
1 parent 184444b commit 92d51cf

File tree

1 file changed

+4
-20
lines changed

1 file changed

+4
-20
lines changed

lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,26 @@
99
#include "gtest/gtest.h"
1010

1111
#include "Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h"
12-
#include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h"
13-
#include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h"
1412
#include "Plugins/ScriptInterpreter/Python/lldb-python.h"
1513

16-
#include "lldb/Host/FileSystem.h"
17-
#include "lldb/Host/HostInfo.h"
18-
1914
#include "PythonTestSuite.h"
2015

21-
using namespace lldb_private;
22-
class TestScriptInterpreterPython : public ScriptInterpreterPythonImpl {
23-
public:
24-
using ScriptInterpreterPythonImpl::Initialize;
25-
};
26-
2716
void PythonTestSuite::SetUp() {
28-
FileSystem::Initialize();
29-
HostInfoBase::Initialize();
30-
// ScriptInterpreterPython::Initialize() depends on HostInfo being
31-
// initializedso it can compute the python directory etc.
32-
TestScriptInterpreterPython::Initialize();
33-
3417
// Although we don't care about concurrency for the purposes of running
3518
// this test suite, Python requires the GIL to be locked even for
3619
// deallocating memory, which can happen when you call Py_DECREF or
3720
// Py_INCREF. So acquire the GIL for the entire duration of this
3821
// test suite.
22+
Py_InitializeEx(0);
3923
m_gil_state = PyGILState_Ensure();
24+
PyRun_SimpleString("import sys");
4025
}
4126

4227
void PythonTestSuite::TearDown() {
4328
PyGILState_Release(m_gil_state);
4429

45-
TestScriptInterpreterPython::Terminate();
46-
HostInfoBase::Terminate();
47-
FileSystem::Terminate();
30+
// We could call Py_FinalizeEx here, but initializing and finalizing Python is
31+
// pretty slow, so just keep Python initialized across tests.
4832
}
4933

5034
// The following functions are the Pythonic implementations of the required

0 commit comments

Comments
 (0)