@@ -370,7 +370,7 @@ void ScriptInterpreterPython::Terminate() {}
370
370
371
371
ScriptInterpreterPythonImpl::Locker::Locker (
372
372
ScriptInterpreterPythonImpl *py_interpreter, uint16_t on_entry,
373
- uint16_t on_leave, FILE * in, FILE * out, FILE * err)
373
+ uint16_t on_leave, FileSP in, FileSP out, FileSP err)
374
374
: ScriptInterpreterLocker(),
375
375
m_teardown_session((on_leave & TearDownSession) == TearDownSession),
376
376
m_python_interpreter(py_interpreter) {
@@ -400,8 +400,8 @@ bool ScriptInterpreterPythonImpl::Locker::DoAcquireLock() {
400
400
}
401
401
402
402
bool ScriptInterpreterPythonImpl::Locker::DoInitSession (uint16_t on_entry_flags,
403
- FILE * in, FILE * out,
404
- FILE * err) {
403
+ FileSP in, FileSP out,
404
+ FileSP err) {
405
405
if (!m_python_interpreter)
406
406
return false ;
407
407
return m_python_interpreter->EnterSession (on_entry_flags, in, out, err);
@@ -636,28 +636,31 @@ void ScriptInterpreterPythonImpl::LeaveSession() {
636
636
m_session_is_active = false ;
637
637
}
638
638
639
- bool ScriptInterpreterPythonImpl::SetStdHandle (File &file, const char *py_name,
640
- PythonFile &save_file,
639
+ bool ScriptInterpreterPythonImpl::SetStdHandle (FileSP file_sp,
640
+ const char *py_name,
641
+ PythonObject &save_file,
641
642
const char *mode) {
642
- if (file.IsValid ()) {
643
- // Flush the file before giving it to python to avoid interleaved output.
644
- file.Flush ();
643
+ if (!file_sp || !*file_sp) {
644
+ save_file.Reset ();
645
+ return false ;
646
+ }
647
+ File &file = *file_sp;
645
648
646
- PythonDictionary &sys_module_dict = GetSysModuleDictionary ();
649
+ // Flush the file before giving it to python to avoid interleaved output.
650
+ file.Flush ();
647
651
648
- save_file = sys_module_dict.GetItemForKey (PythonString (py_name))
649
- .AsType <PythonFile>();
652
+ PythonDictionary &sys_module_dict = GetSysModuleDictionary ();
650
653
651
- PythonFile new_file (file, mode);
652
- sys_module_dict.SetItemForKey (PythonString (py_name), new_file);
653
- return true ;
654
- } else
655
- save_file.Reset ();
656
- return false ;
654
+ save_file = sys_module_dict.GetItemForKey (PythonString (py_name));
655
+
656
+ PythonFile new_file (file, mode);
657
+ sys_module_dict.SetItemForKey (PythonString (py_name), new_file);
658
+ return true ;
657
659
}
658
660
659
661
bool ScriptInterpreterPythonImpl::EnterSession (uint16_t on_entry_flags,
660
- FILE *in, FILE *out, FILE *err) {
662
+ FileSP in_sp, FileSP out_sp,
663
+ FileSP err_sp) {
661
664
// If we have already entered the session, without having officially 'left'
662
665
// it, then there is no need to 'enter' it again.
663
666
Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT));
@@ -706,33 +709,29 @@ bool ScriptInterpreterPythonImpl::EnterSession(uint16_t on_entry_flags,
706
709
707
710
PythonDictionary &sys_module_dict = GetSysModuleDictionary ();
708
711
if (sys_module_dict.IsValid ()) {
709
- NativeFile in_file (in, false );
710
- NativeFile out_file (out, false );
711
- NativeFile err_file (err, false );
712
-
713
- lldb::FileSP in_sp;
714
- lldb::StreamFileSP out_sp;
715
- lldb::StreamFileSP err_sp;
716
- if (!in_file.IsValid () || !out_file.IsValid () || !err_file.IsValid ())
717
- m_debugger.AdoptTopIOHandlerFilesIfInvalid (in_sp, out_sp, err_sp);
712
+ lldb::FileSP top_in_sp;
713
+ lldb::StreamFileSP top_out_sp, top_err_sp;
714
+ if (!in_sp || !out_sp || !err_sp || !*in_sp || !*out_sp || !*err_sp)
715
+ m_debugger.AdoptTopIOHandlerFilesIfInvalid (top_in_sp, top_out_sp,
716
+ top_err_sp);
718
717
719
718
if (on_entry_flags & Locker::NoSTDIN) {
720
719
m_saved_stdin.Reset ();
721
720
} else {
722
- if (!SetStdHandle (in_file , " stdin" , m_saved_stdin, " r" )) {
723
- if (in_sp )
724
- SetStdHandle (*in_sp , " stdin" , m_saved_stdin, " r" );
721
+ if (!SetStdHandle (in_sp , " stdin" , m_saved_stdin, " r" )) {
722
+ if (top_in_sp )
723
+ SetStdHandle (top_in_sp , " stdin" , m_saved_stdin, " r" );
725
724
}
726
725
}
727
726
728
- if (!SetStdHandle (out_file , " stdout" , m_saved_stdout, " w" )) {
729
- if (out_sp )
730
- SetStdHandle (out_sp-> GetFile (), " stdout" , m_saved_stdout, " w" );
727
+ if (!SetStdHandle (out_sp , " stdout" , m_saved_stdout, " w" )) {
728
+ if (top_out_sp )
729
+ SetStdHandle (top_out_sp-> GetFileSP (), " stdout" , m_saved_stdout, " w" );
731
730
}
732
731
733
- if (!SetStdHandle (err_file , " stderr" , m_saved_stderr, " w" )) {
734
- if (err_sp )
735
- SetStdHandle (err_sp-> GetFile (), " stderr" , m_saved_stderr, " w" );
732
+ if (!SetStdHandle (err_sp , " stderr" , m_saved_stderr, " w" )) {
733
+ if (top_err_sp )
734
+ SetStdHandle (top_err_sp-> GetFileSP (), " stderr" , m_saved_stderr, " w" );
736
735
}
737
736
}
738
737
@@ -909,9 +908,6 @@ bool ScriptInterpreterPythonImpl::ExecuteOneLine(
909
908
error_file_sp = output_file_sp = std::make_shared<StreamFile>(std::move (nullout.get ()));
910
909
}
911
910
912
- FILE *in_file = input_file_sp->GetStream ();
913
- FILE *out_file = output_file_sp->GetFile ().GetStream ();
914
- FILE *err_file = error_file_sp->GetFile ().GetStream ();
915
911
bool success = false ;
916
912
{
917
913
// WARNING! It's imperative that this RAII scope be as tight as
@@ -927,8 +923,8 @@ bool ScriptInterpreterPythonImpl::ExecuteOneLine(
927
923
Locker::AcquireLock | Locker::InitSession |
928
924
(options.GetSetLLDBGlobals () ? Locker::InitGlobals : 0 ) |
929
925
((result && result->GetInteractive ()) ? 0 : Locker::NoSTDIN),
930
- Locker::FreeAcquiredLock | Locker::TearDownSession, in_file, out_file ,
931
- err_file );
926
+ Locker::FreeAcquiredLock | Locker::TearDownSession, input_file_sp ,
927
+ output_file_sp-> GetFileSP (), error_file_sp-> GetFileSP () );
932
928
933
929
// Find the correct script interpreter dictionary in the main module.
934
930
PythonDictionary &session_dict = GetSessionDictionary ();
@@ -955,9 +951,8 @@ bool ScriptInterpreterPythonImpl::ExecuteOneLine(
955
951
}
956
952
957
953
// Flush our output and error file handles
958
- ::fflush (out_file);
959
- if (out_file != err_file)
960
- ::fflush (err_file);
954
+ output_file_sp->Flush ();
955
+ error_file_sp->Flush ();
961
956
}
962
957
963
958
if (join_read_thread) {
0 commit comments