Search code examples
pythonexceptionsignalsinterruptsignal-handling

Python - Handle CTRL+D with 'import signal'


I can currently handle CTRL+C via:

def hand_inter(signum, frame):
    print 'hey, nice job.'

signal.signal(signal.SIGINT, hand_inter)

However I am required to also handle CTRL+D yet cannot find the appropriate "signal.CTRL+D" call for signum.


Solution

  • Ctrl+D is not a signal, it's end of file.

    If you have an interactive program, you will be most probably reading STDIN and Ctrl+D is way how user says that the input is over. Outside this context it does not have any special meaning.

    The code that executes after that is typically the code after "readline" or similar call. This is equivalent to reading any other file and detecting it has ended and there is no more data to read--the corresponding call will give you indication of that.

    For example, this could be a simple interactive program:

    import sys
    
    while True:
        line = sys.stdin.readline()    # readline will return "" on EOF
        if line:
            do_something_with(line)    # * if user just pressed Enter line will
                                       #   be "\n", i.e. still True
        else:                          # * user pressed C-D, i.e. stdin has been
            sys.exit(0)                #   closed readline call must have returned ""
    

    On the other hand, Ctrl+C is different, it's way how user tells their terminal to terminate the running process. It can come at any moment, regardless if the process ever asks for input or even cares about outside world.

    Because the process has no way of expecting this you need signal to set up so-called traps, which is mechanism provided by the OS to enable processes say, "if you ever want to terminate me, please just execute this instead..." (which could be anything including nothing, i.e. just ignoring the signal). Exceptions are special signals like SIGKILL, which can't be caught by the process.