You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
code.interact() will exit the whole process when given exit() or quit() #102895
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.
You want to change the behavior of a library that's been like this since 1998. It's not clear to me that making exit() and quit() continue the program would be indeed what the user expects. It's perfectly valid to expect to be able to issue any command and have it behave like it was issued from the code itself. So it's subjective, I guess.
Even if we agree to introduce this behavior, code.interact() specifically says in its docstring that "This is a backwards compatible interface to the InteractiveConsole class."
Therefore, any additional functionality would have to live in the class as a non-default option.
I understood this will break the backward compatibility. I considered this as a bug mainly because - when you type exit in code.interact(), it will display the normal exit message (its repr) - Use exit() or Ctrl-D (i.e. EOF) to exit. However, the two methods now have different behavior, where Ctrl-D exits the code shell only and exit() terminates the process.
I hit this issue when I was trying to test pdb.interact and realized that it's difficult to exit from the code shell with the script, but now that I think about it, I can simply raise a SystemExit.
But, now that we are on that topic, the actual issue evolves - when the users are using pdb.interact, then they use exit() in the interactive shell, I think most of the times they did not expect to exit the process completely. However, the current behavior of code.interact made it almost impossible to recover from an exit() in the interactive shell, which means an exit() in pdb.interact will just break the whole process being debugged. (and of course in theory that's not code.interact's consideration, just to point it out)
I think we can provide user an option to enable this feature? Keep all the default behavior as before, but add an optional replace_exit(or some better name) argument so the users can have an interactive shell that can do exit().
Now this feature will be hidden behind a new option for InteractiveConsole - block_exit. By default, block_exit will be False and everything would behave as before. If the user enabled the option, the interactive shell would block the exit() function from exiting the process, and only exit the interactive shell. The option is available on both the initializer of InteractiveConsole and code.interact().
If this is something we can do, I'll work on the documetation for the explanation.
Activity
iritkatriel commentedon Mar 27, 2023
We would only consider this a bug if behaviour contradicts the documentation. Is this the case?
ambv commentedon Mar 27, 2023
You want to change the behavior of a library that's been like this since 1998. It's not clear to me that making
exit()andquit()continue the program would be indeed what the user expects. It's perfectly valid to expect to be able to issue any command and have it behave like it was issued from the code itself. So it's subjective, I guess.Even if we agree to introduce this behavior,
code.interact()specifically says in its docstring that "This is a backwards compatible interface to theInteractiveConsoleclass."Therefore, any additional functionality would have to live in the class as a non-default option.
gaogaotiantian commentedon Mar 27, 2023
I understood this will break the backward compatibility. I considered this as a bug mainly because - when you type
exitincode.interact(), it will display the normal exit message (itsrepr) -Use exit() or Ctrl-D (i.e. EOF) to exit. However, the two methods now have different behavior, where Ctrl-D exits the code shell only andexit()terminates the process.I hit this issue when I was trying to test
pdb.interactand realized that it's difficult to exit from the code shell with the script, but now that I think about it, I can simply raise aSystemExit.But, now that we are on that topic, the actual issue evolves - when the users are using
pdb.interact, then they useexit()in the interactive shell, I think most of the times they did not expect to exit the process completely. However, the current behavior ofcode.interactmade it almost impossible to recover from anexit()in the interactive shell, which means anexit()inpdb.interactwill just break the whole process being debugged. (and of course in theory that's notcode.interact's consideration, just to point it out)gaogaotiantian commentedon Mar 28, 2023
I think we can provide user an option to enable this feature? Keep all the default behavior as before, but add an optional
replace_exit(or some better name) argument so the users can have an interactive shell that can doexit().gaogaotiantian commentedon Mar 29, 2023
I made a modification to the implementation.
Now this feature will be hidden behind a new option for
InteractiveConsole-block_exit. By default,block_exitwill beFalseand everything would behave as before. If the user enabled the option, the interactive shell would block theexit()function from exiting the process, and only exit the interactive shell. The option is available on both the initializer ofInteractiveConsoleandcode.interact().If this is something we can do, I'll work on the documetation for the explanation.
GH-102895 Add an option local_exit in code.interact to block exit() f…
pythonGH-102895 Add an option local_exit in code.interact to block ex…
pythonGH-102895 Add an option local_exit in code.interact to block ex…