[Lldb-commits] [PATCH] D89352: [lldb] Unconditionally strip the `.py(c)` extension as we always import Python modules

Jonas Devlieghere via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Tue Oct 13 16:59:08 PDT 2020


JDevlieghere created this revision.
JDevlieghere added reviewers: kastiglione, labath.
JDevlieghere requested review of this revision.

Currently we only strip the Python extension when the file exists on disk because we assumed that if it didn't exist it was a module. However, with the change from D89334 <https://reviews.llvm.org/D89334> this is no longer the case as we want to be able to import a relative path to a `.py` as a module. Since we always import a scripting module as a "python module" we should always strip the extension if present.

This is necessary to write a test with a `.py` extension as suggested by Dave:

In D89334#2328372 <https://reviews.llvm.org/D89334#2328372>, @kastiglione wrote:

> Would you mind adding a couple tests for imports via a path to a python file, ex `command script import command.py`, maybe even a test that checks nested directories, ex: `command script import path/to/command.py`?


Repository:
  rLLDB LLDB

https://reviews.llvm.org/D89352

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


Index: lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -2797,19 +2797,20 @@
         return false;
       }
 
-      // strip .py or .pyc extension
-      ConstString extension = target_file.GetFileNameExtension();
-      if (extension) {
-        if (llvm::StringRef(extension.GetCString()) == ".py")
-          basename.resize(basename.length() - 3);
-        else if (llvm::StringRef(extension.GetCString()) == ".pyc")
-          basename.resize(basename.length() - 4);
-      }
     } else {
       error.SetErrorString("no known way to import this module specification");
       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);
+    }
+
     // check if the module is already import-ed
     command_stream.Clear();
     command_stream.Printf("sys.modules.__contains__('%s')", basename.c_str());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89352.297995.patch
Type: text/x-patch
Size: 1339 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20201013/960caf70/attachment.bin>


More information about the lldb-commits mailing list