virtual ~ScriptInterpreter ();
+ struct ExecuteScriptOptions
+ {
+ public:
+ ExecuteScriptOptions () :
+ m_enable_io(true),
+ m_set_lldb_globals(true),
+ m_maskout_errors(true)
+ {
+ }
+
+ bool
+ GetEnableIO () const
+ {
+ return m_enable_io;
+ }
+
+ bool
+ GetSetLLDBGlobals () const
+ {
+ return m_set_lldb_globals;
+ }
+
+ bool
+ GetMaskoutErrors () const
+ {
+ return m_maskout_errors;
+ }
+
+ ExecuteScriptOptions&
+ SetEnableIO (bool enable)
+ {
+ m_enable_io = enable;
+ return *this;
+ }
+
+ ExecuteScriptOptions&
+ SetSetLLDBGlobals (bool set)
+ {
+ m_set_lldb_globals = set;
+ return *this;
+ }
+
+ ExecuteScriptOptions&
+ SetMaskoutErrors (bool maskout)
+ {
+ m_maskout_errors = maskout;
+ return *this;
+ }
+
+ private:
+ bool m_enable_io;
+ bool m_set_lldb_globals;
+ bool m_maskout_errors;
+ };
+
virtual bool
ExecuteOneLine (const char *command,
CommandReturnObject *result,
- bool enable_io,
- bool set_lldb_globals = true) = 0;
+ const ExecuteScriptOptions &options = ExecuteScriptOptions()) = 0;
virtual void
ExecuteInterpreterLoop () = 0;
ExecuteOneLineWithReturn (const char *in_string,
ScriptReturnType return_type,
void *ret_value,
- bool enable_io,
- bool set_lldb_globals = true)
+ const ExecuteScriptOptions &options = ExecuteScriptOptions())
{
return true;
}
virtual bool
ExecuteMultipleLines (const char *in_string,
- bool enable_io,
- bool set_lldb_globals = true)
+ const ExecuteScriptOptions &options = ExecuteScriptOptions())
{
return true;
}
}
bool
-ScriptInterpreterPython::ExecuteOneLine (const char *command, CommandReturnObject *result, bool enable_io, bool set_lldb_globals)
+ScriptInterpreterPython::ExecuteOneLine (const char *command, CommandReturnObject *result, const ExecuteScriptOptions &options)
{
if (!m_valid_session)
return false;
// method to pass the command string directly down to Python.
Locker locker(this,
- ScriptInterpreterPython::Locker::AcquireLock | (set_lldb_globals ? ScriptInterpreterPython::Locker::InitSession : 0),
- ScriptInterpreterPython::Locker::FreeAcquiredLock | (set_lldb_globals ? ScriptInterpreterPython::Locker::TearDownSession : 0));
+ ScriptInterpreterPython::Locker::AcquireLock | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitSession : 0),
+ ScriptInterpreterPython::Locker::FreeAcquiredLock | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::TearDownSession : 0));
bool success = false;
{
PyObject *pvalue = NULL;
{ // scope for PythonInputReaderManager
- PythonInputReaderManager py_input(enable_io ? this : NULL);
+ PythonInputReaderManager py_input(options.GetEnableIO() ? this : NULL);
pvalue = PyObject_CallObject (pfunc, pargs);
}
Py_DECREF (pargs);
Py_DECREF (pvalue);
success = true;
}
- else if (PyErr_Occurred ())
+ else if (options.GetMaskoutErrors() && PyErr_Occurred ())
{
PyErr_Print();
PyErr_Clear();
ScriptInterpreterPython::ExecuteOneLineWithReturn (const char *in_string,
ScriptInterpreter::ScriptReturnType return_type,
void *ret_value,
- bool enable_io,
- bool set_lldb_globals)
+ const ExecuteScriptOptions &options)
{
Locker locker(this,
- ScriptInterpreterPython::Locker::AcquireLock | (set_lldb_globals ? ScriptInterpreterPython::Locker::InitSession : 0),
- ScriptInterpreterPython::Locker::FreeAcquiredLock | (set_lldb_globals ? ScriptInterpreterPython::Locker::TearDownSession : 0));
+ ScriptInterpreterPython::Locker::AcquireLock | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitSession : 0),
+ ScriptInterpreterPython::Locker::FreeAcquiredLock | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::TearDownSession : 0));
PyObject *py_return = NULL;
PyObject *mainmod = PyImport_AddModule ("__main__");
if (in_string != NULL)
{
{ // scope for PythonInputReaderManager
- PythonInputReaderManager py_input(enable_io ? this : NULL);
+ PythonInputReaderManager py_input(options.GetEnableIO() ? this : NULL);
py_return = PyRun_String (in_string, Py_eval_input, globals, locals);
if (py_return == NULL)
{
py_error = PyErr_Occurred();
if (py_error != NULL)
{
- if (PyErr_GivenExceptionMatches (py_error, PyExc_SyntaxError))
- PyErr_Print ();
- PyErr_Clear();
ret_success = false;
+ if (options.GetMaskoutErrors())
+ {
+ if (PyErr_GivenExceptionMatches (py_error, PyExc_SyntaxError))
+ PyErr_Print ();
+ PyErr_Clear();
+ }
}
return ret_success;
}
bool
-ScriptInterpreterPython::ExecuteMultipleLines (const char *in_string, bool enable_io, bool set_lldb_globals)
+ScriptInterpreterPython::ExecuteMultipleLines (const char *in_string, const ExecuteScriptOptions &options)
{
Locker locker(this,
- ScriptInterpreterPython::Locker::AcquireLock | (set_lldb_globals ? ScriptInterpreterPython::Locker::InitSession : 0),
- ScriptInterpreterPython::Locker::FreeAcquiredLock | (set_lldb_globals ? ScriptInterpreterPython::Locker::TearDownSession : 0));
+ ScriptInterpreterPython::Locker::AcquireLock | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitSession : 0),
+ ScriptInterpreterPython::Locker::FreeAcquiredLock | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::TearDownSession : 0));
bool success = false;
PyObject *py_return = NULL;
if (compiled_code)
{
{ // scope for PythonInputReaderManager
- PythonInputReaderManager py_input(enable_io ? this : NULL);
+ PythonInputReaderManager py_input(options.GetEnableIO() ? this : NULL);
py_return = PyEval_EvalCode (compiled_code, globals, locals);
}
if (py_return != NULL)
py_error = PyErr_Occurred ();
if (py_error != NULL)
{
- if (PyErr_GivenExceptionMatches (py_error, PyExc_SyntaxError))
- PyErr_Print ();
- PyErr_Clear();
success = false;
+ if (options.GetMaskoutErrors())
+ {
+ if (PyErr_GivenExceptionMatches (py_error, PyExc_SyntaxError))
+ PyErr_Print ();
+ PyErr_Clear();
+ }
}
return success;
// Convert StringList to one long, newline delimited, const char *.
std::string function_def_string(function_def.CopyList());
- return ExecuteMultipleLines (function_def_string.c_str(), false);
+ return ExecuteMultipleLines (function_def_string.c_str(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false));
}
bool
command_stream.Printf("if not (sys.path.__contains__('%s')):\n sys.path.append('%s');\n\n",
directory,
directory);
- bool syspath_retval = ExecuteMultipleLines(command_stream.GetData(), false, false);
+ bool syspath_retval = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false));
if (!syspath_retval)
{
error.SetErrorString("Python sys.path handling failed");
bool was_imported = (ExecuteOneLineWithReturn(command_stream.GetData(),
ScriptInterpreterPython::eScriptReturnTypeInt,
&refcount,
- false,
- false) && refcount > 0);
+ ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)) && refcount > 0);
if (was_imported == true && can_reload == false)
{
error.SetErrorString("module already imported");
// now actually do the import
command_stream.Clear();
command_stream.Printf("import %s",basename.c_str());
- bool import_retval = ExecuteOneLine(command_stream.GetData(), NULL, false, false);
- if (!import_retval)
+ bool import_retval = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false).SetMaskoutErrors(false));
+ PyObject* py_error = PyErr_Occurred(); // per Python docs: "you do not need to Py_DECREF()" the return of this function
+
+ if (py_error || !import_retval) // check for failure of the import
{
- error.SetErrorString("Python import statement failed");
+ if (py_error) // if we have a Python error..
+ {
+ if (PyErr_GivenExceptionMatches (py_error, PyExc_ImportError)) // and it is an ImportError
+ {
+ PyObject *type,*value,*traceback;
+ PyErr_Fetch (&type,&value,&traceback);
+
+ if (value && value != Py_None)
+ error.SetErrorString(PyString_AsString(PyObject_Str(value)));
+ else
+ error.SetErrorString("ImportError raised by imported module");
+
+ Py_XDECREF(type);
+ Py_XDECREF(value);
+ Py_XDECREF(traceback);
+ }
+ else // any other error
+ {
+ error.SetErrorString("Python raised an error while importing module");
+ }
+ }
+ else // we failed but have no error to explain why
+ {
+ error.SetErrorString("unknown error while importing module");
+ }
+
+ // anyway, clear the error indicator and return false
+ PyErr_Clear();
return false;
}
+ // if we are here, everything worked
// call __lldb_init_module(debugger,dict)
if (!g_swig_call_module_init (basename,
m_dictionary_name.c_str(),
if (ExecuteOneLineWithReturn (command.c_str(),
ScriptInterpreter::eScriptReturnTypeCharStrOrNone,
- &result_ptr, false))
+ &result_ptr, ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false) /*.SetSetLLDBGlobals(false)*/))
{
if (result_ptr)
dest.assign(result_ptr);