[lldb] Don't overwrite quit and exit builtins in the Python interpreter
authorJonas Devlieghere <jonas@devlieghere.com>
Wed, 15 Jun 2022 21:47:41 +0000 (14:47 -0700)
committerJonas Devlieghere <jonas@devlieghere.com>
Wed, 15 Jun 2022 21:53:40 +0000 (14:53 -0700)
commit6cde6ac03c2c1851b1156dd334b87b38fff79f70
treebd2d9d25e3422c8dcc87888ddd29912143c74f42
parentf4ad2039307df26fb8b217ee61391f00a85ea620
[lldb] Don't overwrite quit and exit builtins in the Python interpreter

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. If they're
present, we exit the interpreter with the help of an exception.

The previous implementation also used globals to differentiate between
exit getting called from the interactive interpreter or from inside a
script. This patch achieves the same by using a different exception in
for the interpreter case.

rdar://84095490

Differential revision: https://reviews.llvm.org/D127895
lldb/source/Interpreter/embedded_interpreter.py
lldb/test/Shell/ScriptInterpreter/Python/exit.test [new file with mode: 0644]