Skip to content

Commit ef4bb4f

Browse files
author
Ayla Khan
authored
Merge pull request #219 from SCIInstitute/py_help_fix
Trap help() function to prevent interpreter hanging.
2 parents b973fd6 + 01e617b commit ef4bb4f

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/Core/Python/PythonInterpreter.cc

+16-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <boost/python.hpp>
3737
#include <boost/regex.hpp>
3838
#include <boost/thread/condition_variable.hpp>
39+
#include <boost/algorithm/string.hpp>
3940

4041
#include <sstream>
4142

@@ -409,8 +410,21 @@ void PythonInterpreter::run_string( const std::string& command )
409410
CORE_THROW_LOGICERROR( "The python interpreter hasn't been initialized!" );
410411
}
411412
}
412-
413-
if ( !this->is_eventhandler_thread() )
413+
414+
std::string trimmed_command = command;
415+
boost::algorithm::trim( trimmed_command );
416+
if ( trimmed_command == "help()" )
417+
{
418+
// trap help(), since it hangs interpreter and therefore the entire app
419+
CORE_LOG_DEBUG( "Python help() trapped" );
420+
PyErr_Clear();
421+
PyRun_SimpleString("print(\"help() function ignored in interpreter\")\n");
422+
this->private_->command_buffer_.clear();
423+
this->prompt_signal_( this->private_->prompt1_ );
424+
return;
425+
}
426+
427+
if ( ! this->is_eventhandler_thread() )
414428
{
415429
{
416430
PythonInterpreterPrivate::lock_type lock( this->private_->get_mutex() );
@@ -615,7 +629,6 @@ void PythonInterpreter::interrupt()
615629
{
616630
this->error_signal_( "\nKeyboardInterrupt\n" );
617631
this->private_->command_buffer_.clear();
618-
this->prompt_signal_( this->private_->prompt1_ );
619632
}
620633
}
621634
}

0 commit comments

Comments
 (0)