Fixed the python interpreter so that it correctly inherits the top IOHandler's files instead of always using stdin/out/err.

Removed lldb_private::File::Duplicate() and the copy constructor and the assignment operator that used to duplicate the file handles and made them private so no one uses them. Previously the lldb_private::File::Duplicate() function duplicated files that used file descriptors, (int) but not file streams (FILE *), so the lldb_private::File::Duplicate() function only worked some of the time. No one else excep thee ScriptInterpreterPython was using these functions, so that aren't needed nor desired. Previously every time you would drop into the python interpreter we would duplicate files, and now we avoid this file churn.

<rdar://problem/24877720>

llvm-svn: 263161
This commit is contained in:
Greg Clayton 2016-03-10 20:49:32 +00:00
parent ad04914a53
commit a31baf081b
4 changed files with 41 additions and 96 deletions

View File

@ -74,8 +74,6 @@ public:
{
}
File (const File &rhs);
//------------------------------------------------------------------
/// Constructor with path.
///
@ -138,9 +136,6 @@ public:
//------------------------------------------------------------------
~File() override;
File &
operator= (const File &rhs);
bool
IsValid() const override
{
@ -226,9 +221,6 @@ public:
void
Clear ();
Error
Duplicate (const File &rhs);
int
GetDescriptor() const;
@ -557,6 +549,9 @@ protected:
LazyBool m_is_interactive;
LazyBool m_is_real_terminal;
LazyBool m_supports_colors;
private:
DISALLOW_COPY_AND_ASSIGN(File);
};
} // namespace lldb_private

View File

@ -109,27 +109,6 @@ File::File (const FileSpec& filespec,
}
}
File::File (const File &rhs) :
IOObject(eFDTypeFile, false),
m_descriptor (kInvalidDescriptor),
m_stream (kInvalidStream),
m_options (0),
m_own_stream (false),
m_is_interactive (eLazyBoolCalculate),
m_is_real_terminal (eLazyBoolCalculate)
{
Duplicate (rhs);
}
File &
File::operator = (const File &rhs)
{
if (this != &rhs)
Duplicate (rhs);
return *this;
}
File::~File()
{
Close ();
@ -215,7 +194,6 @@ File::GetStream ()
return m_stream;
}
void
File::SetStream (FILE *fh, bool transfer_ownership)
{
@ -225,35 +203,6 @@ File::SetStream (FILE *fh, bool transfer_ownership)
m_own_stream = transfer_ownership;
}
Error
File::Duplicate (const File &rhs)
{
Error error;
if (IsValid ())
Close();
if (rhs.DescriptorIsValid())
{
#ifdef _WIN32
m_descriptor = ::_dup(rhs.GetDescriptor());
#else
m_descriptor = dup(rhs.GetDescriptor());
#endif
if (!DescriptorIsValid())
error.SetErrorToErrno();
else
{
m_options = rhs.m_options;
m_should_close_fd = true;
}
}
else
{
error.SetErrorString ("invalid file to duplicate");
}
return error;
}
Error
File::Open (const char *path, uint32_t options, uint32_t permissions)
{

View File

@ -546,6 +546,27 @@ ScriptInterpreterPython::LeaveSession ()
m_session_is_active = false;
}
bool
ScriptInterpreterPython::SetStdHandle(File &file, const char *py_name, PythonFile &save_file, const char *mode)
{
if (file.IsValid())
{
// Flush the file before giving it to python to avoid interleaved output.
file.Flush();
PythonDictionary &sys_module_dict = GetSysModuleDictionary();
save_file = sys_module_dict.GetItemForKey(PythonString(py_name)).AsType<PythonFile>();
PythonFile new_file(file, mode);
sys_module_dict.SetItemForKey(PythonString(py_name), new_file);
return true;
}
else
save_file.Reset();
return false;
}
bool
ScriptInterpreterPython::EnterSession (uint16_t on_entry_flags,
FILE *in,
@ -604,54 +625,31 @@ ScriptInterpreterPython::EnterSession (uint16_t on_entry_flags,
if (!in_file.IsValid() || !out_file.IsValid() || !err_file.IsValid())
m_interpreter.GetDebugger().AdoptTopIOHandlerFilesIfInvalid (in_sp, out_sp, err_sp);
if (on_entry_flags & Locker::NoSTDIN)
{
m_saved_stdin.Reset();
if ((on_entry_flags & Locker::NoSTDIN) == 0)
{
// STDIN is enabled
if (!in_file.IsValid() && in_sp)
in_file = in_sp->GetFile();
if (in_file.IsValid())
{
// Flush the file before giving it to python to avoid interleaved output.
in_file.Flush();
m_saved_stdin = sys_module_dict.GetItemForKey(PythonString("stdin")).AsType<PythonFile>();
// This call can deadlock your process if the file is locked
PythonFile new_file(in_file, "r");
sys_module_dict.SetItemForKey (PythonString("stdin"), new_file);
}
}
if (!out_file.IsValid() && out_sp)
out_file = out_sp->GetFile();
if (out_file.IsValid())
{
// Flush the file before giving it to python to avoid interleaved output.
out_file.Flush();
m_saved_stdout = sys_module_dict.GetItemForKey(PythonString("stdout")).AsType<PythonFile>();
PythonFile new_file(out_file, "w");
sys_module_dict.SetItemForKey (PythonString("stdout"), new_file);
}
else
m_saved_stdout.Reset();
if (!err_file.IsValid() && err_sp)
err_file = err_sp->GetFile();
if (err_file.IsValid())
{
// Flush the file before giving it to python to avoid interleaved output.
err_file.Flush();
m_saved_stderr = sys_module_dict.GetItemForKey(PythonString("stderr")).AsType<PythonFile>();
PythonFile new_file(err_file, "w");
sys_module_dict.SetItemForKey (PythonString("stderr"), new_file);
if (!SetStdHandle(in_file, "stdin", m_saved_stdin, "r"))
{
if (in_sp)
SetStdHandle(in_sp->GetFile(), "stdin", m_saved_stdin, "r");
}
}
if (!SetStdHandle(out_file, "stdout", m_saved_stdout, "w"))
{
if (out_sp)
SetStdHandle(out_sp->GetFile(), "stdout", m_saved_stdout, "w");
}
if (!SetStdHandle(err_file, "stderr", m_saved_stderr, "w"))
{
if (err_sp)
SetStdHandle(err_sp->GetFile(), "stderr", m_saved_stderr, "w");
}
else
m_saved_stderr.Reset();
}
if (PyErr_Occurred())

View File

@ -581,6 +581,9 @@ protected:
bool
GetEmbeddedInterpreterModuleObjects ();
bool
SetStdHandle(File &file, const char *py_name, PythonFile &save_file, const char *mode);
PythonFile m_saved_stdin;
PythonFile m_saved_stdout;
PythonFile m_saved_stderr;