I want to prevent user to go back to shell_prompt by pressing CTRL + Z from my python command line interpreter script.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You could write a signal handler for SIGTSTP, which is triggered by Ctrl + Z. Here is an example:
Even if you trap Ctrl+Z (which depends on your terminal settings - see stty(1)) then there are other ways the user can return to the command-line. The only 'real' way of preventing a return to the shell is to remove the shell process by using exec. So, in the user's startup file (
.profile|.bash_profile|.cshrc
) do:Get out of that!
Roughly speaking the Ctrl+Z from a Unix/Linux terminal in cooked or canonical modes will cause the terminal driver to generate a "suspend" signal to the foreground application.
So you have two different overall approaches. Change the terminal settings or ignore the signal.
If you put the terminal into "raw" mode then you disable that signal generation. It's also possible to use terminal settings (
import tty
and read the info about tcsetattr, but also read the man pages for ``stty` and terminfo(5) for more details).ZelluX has already described the simplest signal handling approach.
The following does the trick on my Linux box:
Here is a complete example:
Installing my own signal handler as suggested by @ZelluX does not work here: pressing Ctrl+Z while in
raw_input()
gives a spuriousEOFError
: