Skip to content

Commit 4d1ae58

Browse files
authored
[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 #151617
1 parent 33abf05 commit 4d1ae58

File tree

5 files changed

+43
-26
lines changed

5 files changed

+43
-26
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,22 @@ PyObject *RunString(const char *str, int start, PyObject *globals,
14921492

14931493
return result;
14941494
}
1495+
1496+
int RunSimpleString(const char *str) {
1497+
PyObject *main_module = PyImport_AddModule("__main__");
1498+
if (!main_module)
1499+
return -1;
1500+
1501+
PyObject *globals = PyModule_GetDict(main_module);
1502+
if (!globals)
1503+
return -1;
1504+
1505+
PyObject *result = RunString(str, Py_file_input, globals, globals);
1506+
if (!result)
1507+
return -1;
1508+
1509+
return 0;
1510+
}
14951511
} // namespace python
14961512
} // namespace lldb_private
14971513

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ class StructuredPythonObject : public StructuredData::Generic {
782782

783783
PyObject *RunString(const char *str, int start, PyObject *globals,
784784
PyObject *locals);
785+
int RunSimpleString(const char *str);
785786

786787
} // namespace python
787788
} // namespace lldb_private

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -420,21 +420,21 @@ ScriptInterpreterPythonImpl::ScriptInterpreterPythonImpl(Debugger &debugger)
420420
run_string.Printf("%s = dict()", m_dictionary_name.c_str());
421421

422422
Locker locker(this, Locker::AcquireLock, Locker::FreeAcquiredLock);
423-
PyRun_SimpleString(run_string.GetData());
423+
RunSimpleString(run_string.GetData());
424424

425425
run_string.Clear();
426426
run_string.Printf(
427427
"run_one_line (%s, 'import copy, keyword, os, re, sys, uuid, lldb')",
428428
m_dictionary_name.c_str());
429-
PyRun_SimpleString(run_string.GetData());
429+
RunSimpleString(run_string.GetData());
430430

431431
// Reloading modules requires a different syntax in Python 2 and Python 3.
432432
// This provides a consistent syntax no matter what version of Python.
433433
run_string.Clear();
434434
run_string.Printf(
435435
"run_one_line (%s, 'from importlib import reload as reload_module')",
436436
m_dictionary_name.c_str());
437-
PyRun_SimpleString(run_string.GetData());
437+
RunSimpleString(run_string.GetData());
438438

439439
// WARNING: temporary code that loads Cocoa formatters - this should be done
440440
// on a per-platform basis rather than loading the whole set and letting the
@@ -444,20 +444,20 @@ ScriptInterpreterPythonImpl::ScriptInterpreterPythonImpl(Debugger &debugger)
444444
run_string.Printf(
445445
"run_one_line (%s, 'import lldb.formatters, lldb.formatters.cpp')",
446446
m_dictionary_name.c_str());
447-
PyRun_SimpleString(run_string.GetData());
447+
RunSimpleString(run_string.GetData());
448448
run_string.Clear();
449449

450450
run_string.Printf("run_one_line (%s, 'import lldb.embedded_interpreter; from "
451451
"lldb.embedded_interpreter import run_python_interpreter; "
452452
"from lldb.embedded_interpreter import run_one_line')",
453453
m_dictionary_name.c_str());
454-
PyRun_SimpleString(run_string.GetData());
454+
RunSimpleString(run_string.GetData());
455455
run_string.Clear();
456456

457457
run_string.Printf("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64
458458
"')",
459459
m_dictionary_name.c_str(), m_debugger.GetID());
460-
PyRun_SimpleString(run_string.GetData());
460+
RunSimpleString(run_string.GetData());
461461
}
462462

463463
ScriptInterpreterPythonImpl::~ScriptInterpreterPythonImpl() {
@@ -572,8 +572,8 @@ void ScriptInterpreterPythonImpl::LeaveSession() {
572572
log->PutCString("ScriptInterpreterPythonImpl::LeaveSession()");
573573

574574
// Unset the LLDB global variables.
575-
PyRun_SimpleString("lldb.debugger = None; lldb.target = None; lldb.process "
576-
"= None; lldb.thread = None; lldb.frame = None");
575+
RunSimpleString("lldb.debugger = None; lldb.target = None; lldb.process "
576+
"= None; lldb.thread = None; lldb.frame = None");
577577

578578
// checking that we have a valid thread state - since we use our own
579579
// threading and locking in some (rare) cases during cleanup Python may end
@@ -674,7 +674,7 @@ bool ScriptInterpreterPythonImpl::EnterSession(uint16_t on_entry_flags,
674674
run_string.PutCString("')");
675675
}
676676

677-
PyRun_SimpleString(run_string.GetData());
677+
RunSimpleString(run_string.GetData());
678678
run_string.Clear();
679679

680680
PythonDictionary &sys_module_dict = GetSysModuleDictionary();
@@ -816,9 +816,9 @@ bool ScriptInterpreterPythonImpl::ExecuteOneLine(
816816

817817
if (!command.empty()) {
818818
// We want to call run_one_line, passing in the dictionary and the command
819-
// string. We cannot do this through PyRun_SimpleString here because the
819+
// string. We cannot do this through RunSimpleString here because the
820820
// command string may contain escaped characters, and putting it inside
821-
// another string to pass to PyRun_SimpleString messes up the escaping. So
821+
// another string to pass to RunSimpleString messes up the escaping. So
822822
// we use the following more complicated method to pass the command string
823823
// directly down to Python.
824824
llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
@@ -3057,7 +3057,7 @@ void ScriptInterpreterPythonImpl::Initialize() {
30573057
// Update the path python uses to search for modules to include the current
30583058
// directory.
30593059

3060-
PyRun_SimpleString("import sys");
3060+
RunSimpleString("import sys");
30613061
AddToSysPath(AddLocation::End, ".");
30623062

30633063
// Don't denormalize paths when calling file_spec.GetPath(). On platforms
@@ -3069,10 +3069,10 @@ void ScriptInterpreterPythonImpl::Initialize() {
30693069
if (FileSpec file_spec = HostInfo::GetShlibDir())
30703070
AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false));
30713071

3072-
PyRun_SimpleString("sys.dont_write_bytecode = 1; import "
3073-
"lldb.embedded_interpreter; from "
3074-
"lldb.embedded_interpreter import run_python_interpreter; "
3075-
"from lldb.embedded_interpreter import run_one_line");
3072+
RunSimpleString("sys.dont_write_bytecode = 1; import "
3073+
"lldb.embedded_interpreter; from "
3074+
"lldb.embedded_interpreter import run_python_interpreter; "
3075+
"from lldb.embedded_interpreter import run_one_line");
30763076

30773077
#if LLDB_USE_PYTHON_SET_INTERRUPT
30783078
// Python will not just overwrite its internal SIGINT handler but also the
@@ -3084,13 +3084,13 @@ void ScriptInterpreterPythonImpl::Initialize() {
30843084
// normal Python REPL signal handler which raises a KeyboardInterrupt.
30853085
// Also make sure to not pollute the user's REPL with the signal module nor
30863086
// our utility function.
3087-
PyRun_SimpleString("def lldb_setup_sigint_handler():\n"
3088-
" import signal;\n"
3089-
" def signal_handler(sig, frame):\n"
3090-
" raise KeyboardInterrupt()\n"
3091-
" signal.signal(signal.SIGINT, signal_handler);\n"
3092-
"lldb_setup_sigint_handler();\n"
3093-
"del lldb_setup_sigint_handler\n");
3087+
RunSimpleString("def lldb_setup_sigint_handler():\n"
3088+
" import signal;\n"
3089+
" def signal_handler(sig, frame):\n"
3090+
" raise KeyboardInterrupt()\n"
3091+
" signal.signal(signal.SIGINT, signal_handler);\n"
3092+
"lldb_setup_sigint_handler();\n"
3093+
"del lldb_setup_sigint_handler\n");
30943094
#endif
30953095
}
30963096

@@ -3106,7 +3106,7 @@ void ScriptInterpreterPythonImpl::AddToSysPath(AddLocation ___location,
31063106
statement.append(path);
31073107
statement.append("\")");
31083108
}
3109-
PyRun_SimpleString(statement.c_str());
3109+
RunSimpleString(statement.c_str());
31103110
}
31113111

31123112
// We are intentionally NOT calling Py_Finalize here (this would be the logical

lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ class IOHandlerPythonInterpreter : public IOHandler {
475475
StreamString run_string;
476476
run_string.Printf("run_python_interpreter (%s)",
477477
m_python->GetDictionaryName());
478-
PyRun_SimpleString(run_string.GetData());
478+
python::RunSimpleString(run_string.GetData());
479479
}
480480
}
481481
SetIsDone(true);

lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void PythonTestSuite::SetUp() {
2222
// test suite.
2323
Py_InitializeEx(0);
2424
m_gil_state = PyGILState_Ensure();
25-
PyRun_SimpleString("import sys");
25+
python::RunSimpleString("import sys");
2626
}
2727

2828
void PythonTestSuite::TearDown() {

0 commit comments

Comments
 (0)