[Lldb-commits] [PATCH] D127586: [lldb][Interpreter] Add newline terminal to one-line script calls

Dave Lee via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Sat Jun 11 20:58:51 PDT 2022


kastiglione created this revision.
kastiglione added reviewers: mib, JDevlieghere, jingham.
Herald added a project: All.
kastiglione requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Fix `script -lpython` to handle control flow in one-line commands.

The fix is to append a newline to one-line of source being evaluated.

Without this patch, the following commands **print no output, no errors**.

  (lldb) script if "foo" in lldb.frame.name: print(lldb.thread)
  (lldb) script for f in lldb.thread: print(f.name)

The issue is with `code.InteractiveConsole.runsource()`. A trailing newline is
needed for these expressions to be evaluated. I don't know why this is, the
docs don't mention anything.

>From a python repl, the following samples show that a terminal newline allows
statements containing flow control to fully execute.

  >>> import code
  >>> repl = code.InteractiveConsole()
  >>> repl.runsource("if True: print(1)");
  True
  >>> repl.runsource("if True: print(1)\n");
  1
  False


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127586

Files:
  lldb/source/Interpreter/embedded_interpreter.py
  lldb/test/Shell/ScriptInterpreter/Python/python.test


Index: lldb/test/Shell/ScriptInterpreter/Python/python.test
===================================================================
--- lldb/test/Shell/ScriptInterpreter/Python/python.test
+++ lldb/test/Shell/ScriptInterpreter/Python/python.test
@@ -5,6 +5,7 @@
 # RUN: %lldb -o 'script -lpython -- print("{}".format(1000+100+10+1))' 2>&1 | FileCheck %s
 # RUN: %lldb -o 'script --language python -- print("{}".format(1000+100+10+1))' 2>&1 | FileCheck %s
 # RUN: %lldb -o 'script --language=python -- print("{}".format(1000+100+10+1))' 2>&1 | FileCheck %s
+# RUN: %lldb -o 'script -lpython -- if True: print("{}".format(1000+100+10+1))' 2>&1 | FileCheck %s
 # CHECK: 1111
 
 # RUN: %lldb -o 'script --language invalid -- print("{}".format(1000+100+10+1))' 2>&1 | FileCheck %s --check-prefix INVALID
Index: lldb/source/Interpreter/embedded_interpreter.py
===================================================================
--- lldb/source/Interpreter/embedded_interpreter.py
+++ lldb/source/Interpreter/embedded_interpreter.py
@@ -129,6 +129,7 @@
     try:
         repl = code.InteractiveConsole(local_dict)
         if input_string:
+            input_string += "\n"
             repl.runsource(input_string)
         elif g_run_one_line_str:
             repl.runsource(g_run_one_line_str)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127586.436191.patch
Type: text/x-patch
Size: 1288 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20220612/e91db0db/attachment.bin>


More information about the lldb-commits mailing list