This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Don't overwrite quit and exit builtins in the interactive python interpreter
ClosedPublic

Authored by JDevlieghere on Jun 15 2022, 12:23 PM.

Details

Summary

The interactive interpreter is overwriting the exit and quit builtins with an instance of LLDBQuitter in order to make exit and quit behave like exit() and quit(). It does that by overwriting the __repr__ function to call itself. Despite being a neat trick, it has the unintentional side effect that printing these builtins now quits the interpreter:

(lldb) script
Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
>>> print(exit)
(lldb)

You might consider the above example slightly convoluted, but a more realistic situation is calling locals():

(lldb) script
Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
>>> locals()
(lldb)

This patch keeps the existing behavior but without overwriting the builtins. Instead, it looks for quit and exit in the input and raises an appropriate exception. The previous implementation also used globals to differentiate between exit getting called from the interactive interpreter and from scripts. This patch makes use of a custom exception in order to do the same.

rdar://84095490

Diff Detail

Event Timeline

JDevlieghere created this revision.Jun 15 2022, 12:23 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2022, 12:23 PM
JDevlieghere requested review of this revision.Jun 15 2022, 12:23 PM
kastiglione added inline comments.
lldb/source/Interpreter/embedded_interpreter.py
49–52

could do:

if line in ('exit', 'quit'):
lldb/test/Shell/ScriptInterpreter/Python/exit.test
2–6

is there a reason to support both with and without ()?

mib added inline comments.Jun 15 2022, 1:33 PM
lldb/source/Interpreter/embedded_interpreter.py
49–52

+1

JDevlieghere marked 3 inline comments as done.Jun 15 2022, 1:55 PM
JDevlieghere added inline comments.
lldb/test/Shell/ScriptInterpreter/Python/exit.test
2–6

Yes, the latter is the function call, which works out of the box. This patch is mostly concerned with the former, which allows you to exit the script interpreter the same way you're allowed to exit the lldb prompt (i.e. with quit).

JDevlieghere marked an inline comment as done.
kastiglione accepted this revision.Jun 15 2022, 2:40 PM

Before this, I didn't realize that quit/exit worked without being a function call. I think it's unfortunate we have to preserve that, but oh well.

This revision is now accepted and ready to land.Jun 15 2022, 2:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2022, 2:53 PM