[Lldb-commits] [lldb] 3b33b41 - [lldb] Remove lexical block and fix formatting LoadScriptingModule (NFC)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Tue Oct 13 23:51:06 PDT 2020


Author: Jonas Devlieghere
Date: 2020-10-13T23:50:57-07:00
New Revision: 3b33b41604784f903c7c5c38665d75da93dbf805

URL: https://github.com/llvm/llvm-project/commit/3b33b41604784f903c7c5c38665d75da93dbf805
DIFF: https://github.com/llvm/llvm-project/commit/3b33b41604784f903c7c5c38665d75da93dbf805.diff

LOG: [lldb] Remove lexical block and fix formatting LoadScriptingModule (NFC)

Added: 
    

Modified: 
    lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
index 17ba2db38b4d..8d4a69831a29 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -2734,6 +2734,8 @@ uint64_t replace_all(std::string &str, const std::string &oldStr,
 bool ScriptInterpreterPythonImpl::LoadScriptingModule(
     const char *pathname, bool init_session, lldb_private::Status &error,
     StructuredData::ObjectSP *module_sp) {
+  namespace fs = llvm::sys::fs;
+
   if (!pathname || !pathname[0]) {
     error.SetErrorString("invalid pathname");
     return false;
@@ -2741,142 +2743,137 @@ bool ScriptInterpreterPythonImpl::LoadScriptingModule(
 
   lldb::DebuggerSP debugger_sp = m_debugger.shared_from_this();
 
-  {
-    FileSpec target_file(pathname);
-    FileSystem::Instance().Resolve(target_file);
-    FileSystem::Instance().Collect(target_file);
-    std::string basename(target_file.GetFilename().GetCString());
-
-    StreamString command_stream;
-
-    // Before executing Python code, lock the GIL.
-    Locker py_lock(this,
-                   Locker::AcquireLock |
-                       (init_session ? Locker::InitSession : 0) |
-                       Locker::NoSTDIN,
-                   Locker::FreeAcquiredLock |
-                       (init_session ? Locker::TearDownSession : 0));
-    namespace fs = llvm::sys::fs;
-    fs::file_status st;
-    std::error_code ec = status(target_file.GetPath(), st);
-
-    if (ec || st.type() == fs::file_type::status_error ||
-        st.type() == fs::file_type::type_unknown ||
-        st.type() == fs::file_type::file_not_found) {
-      // if not a valid file of any sort, check if it might be a filename still
-      // dot can't be used but / and \ can, and if either is found, reject
-      if (strchr(pathname, '\\') || strchr(pathname, '/')) {
-        error.SetErrorString("invalid pathname");
-        return false;
-      }
-      basename = pathname; // not a filename, probably a package of some sort,
-                           // let it go through
-    } else if (is_directory(st) || is_regular_file(st)) {
-      if (target_file.GetDirectory().IsEmpty()) {
-        error.SetErrorString("invalid directory name");
-        return false;
-      }
+  FileSpec target_file(pathname);
+  FileSystem::Instance().Resolve(target_file);
+  FileSystem::Instance().Collect(target_file);
+  std::string basename(target_file.GetFilename().GetCString());
 
-      std::string directory = target_file.GetDirectory().GetCString();
-      replace_all(directory, "\\", "\\\\");
-      replace_all(directory, "'", "\\'");
-
-      // now make sure that Python has "directory" in the search path
-      StreamString command_stream;
-      command_stream.Printf("if not (sys.path.__contains__('%s')):\n    "
-                            "sys.path.insert(1,'%s');\n\n",
-                            directory.c_str(), directory.c_str());
-      bool syspath_retval =
-          ExecuteMultipleLines(command_stream.GetData(),
-                               ScriptInterpreter::ExecuteScriptOptions()
-                                   .SetEnableIO(false)
-                                   .SetSetLLDBGlobals(false))
-              .Success();
-      if (!syspath_retval) {
-        error.SetErrorString("Python sys.path handling failed");
-        return false;
-      }
+  StreamString command_stream;
 
-    } else {
-      error.SetErrorString("no known way to import this module specification");
+  // Before executing Python code, lock the GIL.
+  Locker py_lock(this,
+                 Locker::AcquireLock |
+                     (init_session ? Locker::InitSession : 0) | Locker::NoSTDIN,
+                 Locker::FreeAcquiredLock |
+                     (init_session ? Locker::TearDownSession : 0));
+  fs::file_status st;
+  std::error_code ec = status(target_file.GetPath(), st);
+
+  if (ec || st.type() == fs::file_type::status_error ||
+      st.type() == fs::file_type::type_unknown ||
+      st.type() == fs::file_type::file_not_found) {
+    // if not a valid file of any sort, check if it might be a filename still
+    // dot can't be used but / and \ can, and if either is found, reject
+    if (strchr(pathname, '\\') || strchr(pathname, '/')) {
+      error.SetErrorString("invalid pathname");
       return false;
     }
-
-    // Strip .py or .pyc extension
-    llvm::StringRef extension = target_file.GetFileNameExtension().GetCString();
-    if (!extension.empty()) {
-      if (extension == ".py")
-        basename.resize(basename.length() - 3);
-      else if (extension == ".pyc")
-        basename.resize(basename.length() - 4);
+    basename = pathname; // not a filename, probably a package of some sort,
+                         // let it go through
+  } else if (is_directory(st) || is_regular_file(st)) {
+    if (target_file.GetDirectory().IsEmpty()) {
+      error.SetErrorString("invalid directory name");
+      return false;
     }
 
-    // check if the module is already import-ed
-    command_stream.Clear();
-    command_stream.Printf("sys.modules.__contains__('%s')", basename.c_str());
-    bool does_contain = false;
-    // this call will succeed if the module was ever imported in any Debugger
-    // in the lifetime of the process in which this LLDB framework is living
-    bool was_imported_globally =
-        (ExecuteOneLineWithReturn(
-             command_stream.GetData(),
-             ScriptInterpreterPythonImpl::eScriptReturnTypeBool, &does_contain,
-             ScriptInterpreter::ExecuteScriptOptions()
-                 .SetEnableIO(false)
-                 .SetSetLLDBGlobals(false)) &&
-         does_contain);
-    // this call will fail if the module was not imported in this Debugger
-    // before
-    command_stream.Clear();
-    command_stream.Printf("sys.getrefcount(%s)", basename.c_str());
-    bool was_imported_locally = GetSessionDictionary()
-                                    .GetItemForKey(PythonString(basename))
-                                    .IsAllocated();
-
-    bool was_imported = (was_imported_globally || was_imported_locally);
-
-    // now actually do the import
-    command_stream.Clear();
+    std::string directory = target_file.GetDirectory().GetCString();
+    replace_all(directory, "\\", "\\\\");
+    replace_all(directory, "'", "\\'");
 
-    if (was_imported) {
-      if (!was_imported_locally)
-        command_stream.Printf("import %s ; reload_module(%s)", basename.c_str(),
-                              basename.c_str());
-      else
-        command_stream.Printf("reload_module(%s)", basename.c_str());
-    } else
-      command_stream.Printf("import %s", basename.c_str());
-
-    error = ExecuteMultipleLines(command_stream.GetData(),
-                                 ScriptInterpreter::ExecuteScriptOptions()
-                                     .SetEnableIO(false)
-                                     .SetSetLLDBGlobals(false));
-    if (error.Fail())
-      return false;
-
-    // if we are here, everything worked
-    // call __lldb_init_module(debugger,dict)
-    if (!LLDBSwigPythonCallModuleInit(basename.c_str(),
-                                      m_dictionary_name.c_str(), debugger_sp)) {
-      error.SetErrorString("calling __lldb_init_module failed");
+    // now make sure that Python has "directory" in the search path
+    StreamString command_stream;
+    command_stream.Printf("if not (sys.path.__contains__('%s')):\n    "
+                          "sys.path.insert(1,'%s');\n\n",
+                          directory.c_str(), directory.c_str());
+    bool syspath_retval =
+        ExecuteMultipleLines(command_stream.GetData(),
+                             ScriptInterpreter::ExecuteScriptOptions()
+                                 .SetEnableIO(false)
+                                 .SetSetLLDBGlobals(false))
+            .Success();
+    if (!syspath_retval) {
+      error.SetErrorString("Python sys.path handling failed");
       return false;
     }
 
-    if (module_sp) {
-      // everything went just great, now set the module object
-      command_stream.Clear();
-      command_stream.Printf("%s", basename.c_str());
-      void *module_pyobj = nullptr;
-      if (ExecuteOneLineWithReturn(
-              command_stream.GetData(),
-              ScriptInterpreter::eScriptReturnTypeOpaqueObject,
-              &module_pyobj) &&
-          module_pyobj)
-        *module_sp = std::make_shared<StructuredPythonObject>(module_pyobj);
-    }
+  } else {
+    error.SetErrorString("no known way to import this module specification");
+    return false;
+  }
 
-    return true;
+  // Strip .py or .pyc extension
+  llvm::StringRef extension = target_file.GetFileNameExtension().GetCString();
+  if (!extension.empty()) {
+    if (extension == ".py")
+      basename.resize(basename.length() - 3);
+    else if (extension == ".pyc")
+      basename.resize(basename.length() - 4);
+  }
+
+  // check if the module is already import-ed
+  command_stream.Clear();
+  command_stream.Printf("sys.modules.__contains__('%s')", basename.c_str());
+  bool does_contain = false;
+  // this call will succeed if the module was ever imported in any Debugger
+  // in the lifetime of the process in which this LLDB framework is living
+  bool was_imported_globally =
+      (ExecuteOneLineWithReturn(
+           command_stream.GetData(),
+           ScriptInterpreterPythonImpl::eScriptReturnTypeBool, &does_contain,
+           ScriptInterpreter::ExecuteScriptOptions()
+               .SetEnableIO(false)
+               .SetSetLLDBGlobals(false)) &&
+       does_contain);
+  // this call will fail if the module was not imported in this Debugger
+  // before
+  command_stream.Clear();
+  command_stream.Printf("sys.getrefcount(%s)", basename.c_str());
+  bool was_imported_locally = GetSessionDictionary()
+                                  .GetItemForKey(PythonString(basename))
+                                  .IsAllocated();
+
+  bool was_imported = (was_imported_globally || was_imported_locally);
+
+  // now actually do the import
+  command_stream.Clear();
+
+  if (was_imported) {
+    if (!was_imported_locally)
+      command_stream.Printf("import %s ; reload_module(%s)", basename.c_str(),
+                            basename.c_str());
+    else
+      command_stream.Printf("reload_module(%s)", basename.c_str());
+  } else
+    command_stream.Printf("import %s", basename.c_str());
+
+  error = ExecuteMultipleLines(command_stream.GetData(),
+                               ScriptInterpreter::ExecuteScriptOptions()
+                                   .SetEnableIO(false)
+                                   .SetSetLLDBGlobals(false));
+  if (error.Fail())
+    return false;
+
+  // if we are here, everything worked
+  // call __lldb_init_module(debugger,dict)
+  if (!LLDBSwigPythonCallModuleInit(basename.c_str(), m_dictionary_name.c_str(),
+                                    debugger_sp)) {
+    error.SetErrorString("calling __lldb_init_module failed");
+    return false;
   }
+
+  if (module_sp) {
+    // everything went just great, now set the module object
+    command_stream.Clear();
+    command_stream.Printf("%s", basename.c_str());
+    void *module_pyobj = nullptr;
+    if (ExecuteOneLineWithReturn(
+            command_stream.GetData(),
+            ScriptInterpreter::eScriptReturnTypeOpaqueObject, &module_pyobj) &&
+        module_pyobj)
+      *module_sp = std::make_shared<StructuredPythonObject>(module_pyobj);
+  }
+
+  return true;
 }
 
 bool ScriptInterpreterPythonImpl::IsReservedWord(const char *word) {


        


More information about the lldb-commits mailing list