From e0c70f1b2c4f05ca1e3333015faac4c87d814403 Mon Sep 17 00:00:00 2001 From: Enrico Granata Date: Fri, 31 May 2013 01:03:09 +0000 Subject: [PATCH] command script import now does reloads - for real If you invoke command script import foo and it detects that foo has already been imported, it will - invoke reload(foo) to reload the module in Python - re-invoke foo.__lldb_init_module This second step is necessary to ensure that LLDB does not keep cached copies of any formatter, command, ... that the module is providing Usual caveats with Python imports persist. Among these: - if you have objects lurking around, reloading the module won't magically update them to reflect changes - if module A imports module B, reloading A won't reload B These are Python-specific issues independent of LLDB that would require more extensive design work The --allow-reload (-r) option is maintained for compatibility with existing scripts, but is clearly documented as redundant - reloading is always enabled whether you use it or not llvm-svn: 182977 --- lldb/source/Commands/CommandObjectCommands.cpp | 4 ++-- lldb/source/Core/Module.cpp | 2 +- lldb/source/Interpreter/ScriptInterpreterPython.cpp | 5 ++++- .../functionalities/command_script/import/TestImport.py | 8 +++----- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lldb/source/Commands/CommandObjectCommands.cpp b/lldb/source/Commands/CommandObjectCommands.cpp index 278fdc0c8764..5964eacce554 100644 --- a/lldb/source/Commands/CommandObjectCommands.cpp +++ b/lldb/source/Commands/CommandObjectCommands.cpp @@ -1355,7 +1355,7 @@ protected: void OptionParsingStarting () { - m_allow_reload = false; + m_allow_reload = true; } const OptionDefinition* @@ -1426,7 +1426,7 @@ protected: OptionDefinition CommandObjectCommandsScriptImport::CommandOptions::g_option_table[] = { - { LLDB_OPT_SET_1, false, "allow-reload", 'r', no_argument, NULL, 0, eArgTypeNone, "Allow the script to be loaded even if it was already loaded before (for Python, the __lldb_init_module function will be called again, but the module will not be reloaded from disk)."}, + { LLDB_OPT_SET_1, false, "allow-reload", 'r', no_argument, NULL, 0, eArgTypeNone, "Allow the script to be loaded even if it was already loaded before. This argument exists for backwards compatibility, but reloading is always allowed, whether you specify it or not."}, { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } }; diff --git a/lldb/source/Core/Module.cpp b/lldb/source/Core/Module.cpp index 0967ef7100d3..d8420ab8af1f 100644 --- a/lldb/source/Core/Module.cpp +++ b/lldb/source/Core/Module.cpp @@ -1287,7 +1287,7 @@ Module::LoadScriptingResourceInTarget (Target *target, Error& error, Stream* fee } StreamString scripting_stream; scripting_fspec.Dump(&scripting_stream); - const bool can_reload = false; + const bool can_reload = true; const bool init_lldb_globals = false; bool did_load = script_interpreter->LoadScriptingModule(scripting_stream.GetData(), can_reload, init_lldb_globals, error); if (!did_load) diff --git a/lldb/source/Interpreter/ScriptInterpreterPython.cpp b/lldb/source/Interpreter/ScriptInterpreterPython.cpp index b54ef1ca901e..da80e0ce0e8a 100644 --- a/lldb/source/Interpreter/ScriptInterpreterPython.cpp +++ b/lldb/source/Interpreter/ScriptInterpreterPython.cpp @@ -2672,7 +2672,10 @@ ScriptInterpreterPython::LoadScriptingModule (const char* pathname, // now actually do the import command_stream.Clear(); - command_stream.Printf("import %s",basename.c_str()); + if (was_imported) + command_stream.Printf("reload(%s)",basename.c_str()); + else + command_stream.Printf("import %s",basename.c_str()); 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 diff --git a/lldb/test/functionalities/command_script/import/TestImport.py b/lldb/test/functionalities/command_script/import/TestImport.py index 2356fcee850a..2f0121bd73d0 100644 --- a/lldb/test/functionalities/command_script/import/TestImport.py +++ b/lldb/test/functionalities/command_script/import/TestImport.py @@ -45,17 +45,15 @@ class ImportTestCase(TestBase): error=True, startstr='error: module importing failed') self.expect("command script import ./nosuchfolder/", error=True, startstr='error: module importing failed') - self.expect("command script import ./foo/foo.py", - error=True, startstr='error: module importing failed') + self.expect("command script import ./foo/foo.py", error=False) self.runCmd("command script import --allow-reload ./thepackage") self.expect("TPcommandA",substrs=["hello world A"]) self.expect("TPcommandB",substrs=["hello world B"]) self.runCmd("script import dummymodule") - self.expect("command script import ./dummymodule.py", - error=True, startstr='error: module importing failed') - self.runCmd("command script import --allow-reload ./dummymodule.py") + self.expect("command script import ./dummymodule.py", error=False) + self.expect("command script import --allow-reload ./dummymodule.py", error=False) self.runCmd("command script add -f foo.foo_function foocmd") self.runCmd("command script add -f foobar.foo_function foobarcmd")