Description
code.interact()
is provided as a method to embed an interactive shell in arbitrary python code. It was used in pdb
for interact
command.
However, when the user needs to exit, they probably will use a very familiar command exit()
(or quit()
), then they'll realize that the whole process is terminated. In most of the cases, that's not the expected bahavior. The user probably just wants to exit the interpreter and go back to the original code.
Ctrl+D(Ctrl+Z then enter) works as expected because there's logic to handle that specifically.
Simply catching SystemExit
won't work because builtins.exit()
and builtins.quit()
(if provided) will also close sys.stdin
(weird behavior to me) and it's not trivial to reopen stdin
.
To provide a more reasonable and unified behavior, we can replace the exit
and quit
in builtins
in the interactive shell, and switch back after. This will also make pdb
happier.