[lldb] Reimplment PyRun_SimpleString using the Python stable C API (#151777)
Reimplment `PyRun_SimpleString` using the Python stable C API and the `RunString` helper. Part of https://github.com/llvm/llvm-project/issues/151617
This commit is contained in:
parent
33abf05af4
commit
4d1ae58d9e
@ -1492,6 +1492,22 @@ PyObject *RunString(const char *str, int start, PyObject *globals,
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int RunSimpleString(const char *str) {
|
||||
PyObject *main_module = PyImport_AddModule("__main__");
|
||||
if (!main_module)
|
||||
return -1;
|
||||
|
||||
PyObject *globals = PyModule_GetDict(main_module);
|
||||
if (!globals)
|
||||
return -1;
|
||||
|
||||
PyObject *result = RunString(str, Py_file_input, globals, globals);
|
||||
if (!result)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
} // namespace python
|
||||
} // namespace lldb_private
|
||||
|
||||
|
@ -782,6 +782,7 @@ private:
|
||||
|
||||
PyObject *RunString(const char *str, int start, PyObject *globals,
|
||||
PyObject *locals);
|
||||
int RunSimpleString(const char *str);
|
||||
|
||||
} // namespace python
|
||||
} // namespace lldb_private
|
||||
|
@ -420,13 +420,13 @@ ScriptInterpreterPythonImpl::ScriptInterpreterPythonImpl(Debugger &debugger)
|
||||
run_string.Printf("%s = dict()", m_dictionary_name.c_str());
|
||||
|
||||
Locker locker(this, Locker::AcquireLock, Locker::FreeAcquiredLock);
|
||||
PyRun_SimpleString(run_string.GetData());
|
||||
RunSimpleString(run_string.GetData());
|
||||
|
||||
run_string.Clear();
|
||||
run_string.Printf(
|
||||
"run_one_line (%s, 'import copy, keyword, os, re, sys, uuid, lldb')",
|
||||
m_dictionary_name.c_str());
|
||||
PyRun_SimpleString(run_string.GetData());
|
||||
RunSimpleString(run_string.GetData());
|
||||
|
||||
// Reloading modules requires a different syntax in Python 2 and Python 3.
|
||||
// This provides a consistent syntax no matter what version of Python.
|
||||
@ -434,7 +434,7 @@ ScriptInterpreterPythonImpl::ScriptInterpreterPythonImpl(Debugger &debugger)
|
||||
run_string.Printf(
|
||||
"run_one_line (%s, 'from importlib import reload as reload_module')",
|
||||
m_dictionary_name.c_str());
|
||||
PyRun_SimpleString(run_string.GetData());
|
||||
RunSimpleString(run_string.GetData());
|
||||
|
||||
// WARNING: temporary code that loads Cocoa formatters - this should be done
|
||||
// on a per-platform basis rather than loading the whole set and letting the
|
||||
@ -444,20 +444,20 @@ ScriptInterpreterPythonImpl::ScriptInterpreterPythonImpl(Debugger &debugger)
|
||||
run_string.Printf(
|
||||
"run_one_line (%s, 'import lldb.formatters, lldb.formatters.cpp')",
|
||||
m_dictionary_name.c_str());
|
||||
PyRun_SimpleString(run_string.GetData());
|
||||
RunSimpleString(run_string.GetData());
|
||||
run_string.Clear();
|
||||
|
||||
run_string.Printf("run_one_line (%s, 'import lldb.embedded_interpreter; from "
|
||||
"lldb.embedded_interpreter import run_python_interpreter; "
|
||||
"from lldb.embedded_interpreter import run_one_line')",
|
||||
m_dictionary_name.c_str());
|
||||
PyRun_SimpleString(run_string.GetData());
|
||||
RunSimpleString(run_string.GetData());
|
||||
run_string.Clear();
|
||||
|
||||
run_string.Printf("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64
|
||||
"')",
|
||||
m_dictionary_name.c_str(), m_debugger.GetID());
|
||||
PyRun_SimpleString(run_string.GetData());
|
||||
RunSimpleString(run_string.GetData());
|
||||
}
|
||||
|
||||
ScriptInterpreterPythonImpl::~ScriptInterpreterPythonImpl() {
|
||||
@ -572,8 +572,8 @@ void ScriptInterpreterPythonImpl::LeaveSession() {
|
||||
log->PutCString("ScriptInterpreterPythonImpl::LeaveSession()");
|
||||
|
||||
// Unset the LLDB global variables.
|
||||
PyRun_SimpleString("lldb.debugger = None; lldb.target = None; lldb.process "
|
||||
"= None; lldb.thread = None; lldb.frame = None");
|
||||
RunSimpleString("lldb.debugger = None; lldb.target = None; lldb.process "
|
||||
"= None; lldb.thread = None; lldb.frame = None");
|
||||
|
||||
// checking that we have a valid thread state - since we use our own
|
||||
// threading and locking in some (rare) cases during cleanup Python may end
|
||||
@ -674,7 +674,7 @@ bool ScriptInterpreterPythonImpl::EnterSession(uint16_t on_entry_flags,
|
||||
run_string.PutCString("')");
|
||||
}
|
||||
|
||||
PyRun_SimpleString(run_string.GetData());
|
||||
RunSimpleString(run_string.GetData());
|
||||
run_string.Clear();
|
||||
|
||||
PythonDictionary &sys_module_dict = GetSysModuleDictionary();
|
||||
@ -816,9 +816,9 @@ bool ScriptInterpreterPythonImpl::ExecuteOneLine(
|
||||
|
||||
if (!command.empty()) {
|
||||
// We want to call run_one_line, passing in the dictionary and the command
|
||||
// string. We cannot do this through PyRun_SimpleString here because the
|
||||
// string. We cannot do this through RunSimpleString here because the
|
||||
// command string may contain escaped characters, and putting it inside
|
||||
// another string to pass to PyRun_SimpleString messes up the escaping. So
|
||||
// another string to pass to RunSimpleString messes up the escaping. So
|
||||
// we use the following more complicated method to pass the command string
|
||||
// directly down to Python.
|
||||
llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
|
||||
@ -3057,7 +3057,7 @@ void ScriptInterpreterPythonImpl::Initialize() {
|
||||
// Update the path python uses to search for modules to include the current
|
||||
// directory.
|
||||
|
||||
PyRun_SimpleString("import sys");
|
||||
RunSimpleString("import sys");
|
||||
AddToSysPath(AddLocation::End, ".");
|
||||
|
||||
// Don't denormalize paths when calling file_spec.GetPath(). On platforms
|
||||
@ -3069,10 +3069,10 @@ void ScriptInterpreterPythonImpl::Initialize() {
|
||||
if (FileSpec file_spec = HostInfo::GetShlibDir())
|
||||
AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false));
|
||||
|
||||
PyRun_SimpleString("sys.dont_write_bytecode = 1; import "
|
||||
"lldb.embedded_interpreter; from "
|
||||
"lldb.embedded_interpreter import run_python_interpreter; "
|
||||
"from lldb.embedded_interpreter import run_one_line");
|
||||
RunSimpleString("sys.dont_write_bytecode = 1; import "
|
||||
"lldb.embedded_interpreter; from "
|
||||
"lldb.embedded_interpreter import run_python_interpreter; "
|
||||
"from lldb.embedded_interpreter import run_one_line");
|
||||
|
||||
#if LLDB_USE_PYTHON_SET_INTERRUPT
|
||||
// Python will not just overwrite its internal SIGINT handler but also the
|
||||
@ -3084,13 +3084,13 @@ void ScriptInterpreterPythonImpl::Initialize() {
|
||||
// normal Python REPL signal handler which raises a KeyboardInterrupt.
|
||||
// Also make sure to not pollute the user's REPL with the signal module nor
|
||||
// our utility function.
|
||||
PyRun_SimpleString("def lldb_setup_sigint_handler():\n"
|
||||
" import signal;\n"
|
||||
" def signal_handler(sig, frame):\n"
|
||||
" raise KeyboardInterrupt()\n"
|
||||
" signal.signal(signal.SIGINT, signal_handler);\n"
|
||||
"lldb_setup_sigint_handler();\n"
|
||||
"del lldb_setup_sigint_handler\n");
|
||||
RunSimpleString("def lldb_setup_sigint_handler():\n"
|
||||
" import signal;\n"
|
||||
" def signal_handler(sig, frame):\n"
|
||||
" raise KeyboardInterrupt()\n"
|
||||
" signal.signal(signal.SIGINT, signal_handler);\n"
|
||||
"lldb_setup_sigint_handler();\n"
|
||||
"del lldb_setup_sigint_handler\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -3106,7 +3106,7 @@ void ScriptInterpreterPythonImpl::AddToSysPath(AddLocation location,
|
||||
statement.append(path);
|
||||
statement.append("\")");
|
||||
}
|
||||
PyRun_SimpleString(statement.c_str());
|
||||
RunSimpleString(statement.c_str());
|
||||
}
|
||||
|
||||
// We are intentionally NOT calling Py_Finalize here (this would be the logical
|
||||
|
@ -475,7 +475,7 @@ public:
|
||||
StreamString run_string;
|
||||
run_string.Printf("run_python_interpreter (%s)",
|
||||
m_python->GetDictionaryName());
|
||||
PyRun_SimpleString(run_string.GetData());
|
||||
python::RunSimpleString(run_string.GetData());
|
||||
}
|
||||
}
|
||||
SetIsDone(true);
|
||||
|
@ -22,7 +22,7 @@ void PythonTestSuite::SetUp() {
|
||||
// test suite.
|
||||
Py_InitializeEx(0);
|
||||
m_gil_state = PyGILState_Ensure();
|
||||
PyRun_SimpleString("import sys");
|
||||
python::RunSimpleString("import sys");
|
||||
}
|
||||
|
||||
void PythonTestSuite::TearDown() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user