[Lldb-commits] [lldb] e9349ef - Fix `script -lpython` to handle control flow in one-line commands.

Dave Lee via lldb-commits lldb-commits at lists.llvm.org
Wed Jun 15 22:23:06 PDT 2022


Author: Dave Lee
Date: 2022-06-15T22:20:48-07:00
New Revision: e9349ef9e6d83aa8900b5e534f1d15fb4d44a33c

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

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

The fix is to append a newline to the 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
```

Notes:

>From an interactive python repl, the output is not printed immediately. The
user is required to enter a blank line following the first.

```
>>> if True: print(1)
...
1
```

However, `python -c 'if True: print(1)'` works without needing a newline.

Reviewed By: JDevlieghere

Differential Revision: https://reviews.llvm.org/D127586

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/lldb/source/Interpreter/embedded_interpreter.py b/lldb/source/Interpreter/embedded_interpreter.py
index 523bc051a0ce2..fd2cc06bc2860 100644
--- a/lldb/source/Interpreter/embedded_interpreter.py
+++ b/lldb/source/Interpreter/embedded_interpreter.py
@@ -116,6 +116,10 @@ def run_one_line(local_dict, input_string):
         input_string = strip_and_check_exit(input_string)
         repl = code.InteractiveConsole(local_dict)
         if input_string:
+            # A newline is appended to support one-line statements containing
+            # control flow. For example "if True: print(1)" silently does
+            # nothing, but works with a newline: "if True: print(1)\n".
+            input_string += "\n"
             repl.runsource(input_string)
         elif g_run_one_line_str:
             repl.runsource(g_run_one_line_str)

diff  --git a/lldb/test/Shell/ScriptInterpreter/Python/python.test b/lldb/test/Shell/ScriptInterpreter/Python/python.test
index f8f9abfc3ab64..333afd914df00 100644
--- a/lldb/test/Shell/ScriptInterpreter/Python/python.test
+++ b/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


        


More information about the lldb-commits mailing list