Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python 2.7 signal value errors

Please, who can explain why I get this error :

ValueError: invalid signal value

when trying to execute this test code, under Python 2.7.6 as well as under Python 3.3

import signal, os

def handler(signum, frame):    
    print('Signal handler called with signal', signum)

signal.signal(signal.CTRL_C_EVENT, handler)
like image 962
user3572262 Avatar asked Dec 25 '22 10:12

user3572262


1 Answers

You cannot register a signal handler for CTRL_C_EVENT, because it is not supported.

Unfortunately, the Python docs on CTRL_C_EVENT don't really state that explicitly: When the docs say "the signal can only be used with os.kill", they mean it literally, as in "really, all you can do with this signal is use it as in os.kill(pid, signal.CTRL_C_EVENT), so don't try registering a handler for it".

For some background, see Python issue 9524.

Should the behavior be better documented? Hell yes.

like image 82
Petri Avatar answered Jan 02 '23 01:01

Petri