update ScriptInterpreterPython to use File, not FILE*

Summary:
ScriptInterpreterPython needs to save and restore sys.stdout and
friends when LLDB runs a python script.

It currently does this using FILE*, which is not optimal.  If
whatever was in sys.stdout can not be represented as a FILE*, then
it will not be restored correctly when the script is finished.

It also means that if the debugger's own output stream is not
representable as a file, ScriptInterpreterPython will not be able
to redirect python's  output correctly.

This patch updates ScriptInterpreterPython to represent files with
lldb_private::File, and to represent whatever the user had in
sys.stdout as simply a PythonObject.

This will make lldb interoperate better with other scripts or programs
that need to manipulate sys.stdout.

Reviewers: JDevlieghere, jasonmolenda, labath

Reviewed By: labath

Subscribers: lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D68962

llvm-svn: 374964
This commit is contained in:
Lawrence D'Anna
2019-10-16 01:58:15 +00:00
parent 9d10b9d99b
commit b07823f3e2
4 changed files with 58 additions and 66 deletions

View File

@@ -294,17 +294,19 @@ public:
TearDownSession = 0x0004
};
Locker(ScriptInterpreterPythonImpl *py_interpreter = nullptr,
Locker(ScriptInterpreterPythonImpl *py_interpreter,
uint16_t on_entry = AcquireLock | InitSession,
uint16_t on_leave = FreeLock | TearDownSession, FILE *in = nullptr,
FILE *out = nullptr, FILE *err = nullptr);
uint16_t on_leave = FreeLock | TearDownSession,
lldb::FileSP in = nullptr, lldb::FileSP out = nullptr,
lldb::FileSP err = nullptr);
~Locker() override;
private:
bool DoAcquireLock();
bool DoInitSession(uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err);
bool DoInitSession(uint16_t on_entry_flags, lldb::FileSP in,
lldb::FileSP out, lldb::FileSP err);
bool DoFreeLock();
@@ -312,7 +314,6 @@ public:
bool m_teardown_session;
ScriptInterpreterPythonImpl *m_python_interpreter;
// FILE* m_tmp_fh;
PyGILState_STATE m_GILState;
};
@@ -341,7 +342,8 @@ public:
static void AddToSysPath(AddLocation location, std::string path);
bool EnterSession(uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err);
bool EnterSession(uint16_t on_entry_flags, lldb::FileSP in, lldb::FileSP out,
lldb::FileSP err);
void LeaveSession();
@@ -369,12 +371,12 @@ public:
bool GetEmbeddedInterpreterModuleObjects();
bool SetStdHandle(File &file, const char *py_name, PythonFile &save_file,
const char *mode);
bool SetStdHandle(lldb::FileSP file, const char *py_name,
PythonObject &save_file, const char *mode);
PythonFile m_saved_stdin;
PythonFile m_saved_stdout;
PythonFile m_saved_stderr;
PythonObject m_saved_stdin;
PythonObject m_saved_stdout;
PythonObject m_saved_stderr;
PythonObject m_main_module;
PythonDictionary m_session_dict;
PythonDictionary m_sys_module_dict;