[all-commits] [llvm/llvm-project] e9349e: Fix `script -lpython` to handle control flow in on...

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

  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: e9349ef9e6d83aa8900b5e534f1d15fb4d44a33c
  Author: Dave Lee <davelee.com at gmail.com>
  Date:   2022-06-15 (Wed, 15 Jun 2022)

  Changed paths:
    M lldb/source/Interpreter/embedded_interpreter.py
    M lldb/test/Shell/ScriptInterpreter/Python/python.test

  Log Message:
  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)")
>>> repl.runsource("if True: print(1)\n")


>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)

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

Reviewed By: JDevlieghere

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

More information about the All-commits mailing list