When entering the context, set up signal handler for signal . When leaving the context, restore the original signal handler. can be either a str (the name of a signal.SIGXXX attribute; it will not crash if the attribute name does not exist, as some names are pl
(sig, handler)
| 166 | |
| 167 | @contextlib.contextmanager |
| 168 | def signal_handler(sig, handler): |
| 169 | """ |
| 170 | When entering the context, set up signal handler <handler> for signal <sig>. |
| 171 | When leaving the context, restore the original signal handler. |
| 172 | |
| 173 | <sig> can be either a str (the name of a signal.SIGXXX attribute; it |
| 174 | will not crash if the attribute name does not exist, as some names are platform |
| 175 | specific) or an int (a signal number). |
| 176 | |
| 177 | <handler> is any handler value accepted by signal.signal(sig, handler). |
| 178 | """ |
| 179 | if isinstance(sig, str): |
| 180 | sig = getattr(signal, sig, None) |
| 181 | if sig is not None: |
| 182 | orig_handler = signal.signal(sig, handler) |
| 183 | try: |
| 184 | yield |
| 185 | finally: |
| 186 | if sig is not None: |
| 187 | signal.signal(sig, orig_handler) |
| 188 | |
| 189 | |
| 190 | def raising_signal_handler(exc_cls): |
no outgoing calls
no test coverage detected