This is an archive of the discontinued LLVM Phabricator instance.

[lldb][Interpreter] Add newline terminal to one-line script calls
ClosedPublic

Authored by kastiglione on Jun 11 2022, 8:58 PM.

Details

Summary

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.

Diff Detail

Event Timeline

kastiglione created this revision.Jun 11 2022, 8:58 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 11 2022, 8:58 PM
kastiglione requested review of this revision.Jun 11 2022, 8:58 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 11 2022, 8:58 PM
kastiglione edited the summary of this revision. (Show Details)Jun 11 2022, 9:07 PM
This revision is now accepted and ready to land.Jun 15 2022, 5:07 PM
mib added a comment.Jun 15 2022, 10:47 PM

A bit late to this ... thanks for your fix!