break_control¶
Break control utilities for handling SIGINT signals.
This module provides BreakControl, a context manager that registers a global SIGINT handler and tracks whether a break has been received. Multiple BreakControl instances can be active simultaneously; the first to open installs the handler, and the last to close removes it.
Example
import os
import signal
from py_misc_utils import break_control
with break_control.BreakControl() as bc:
os.kill(os.getpid(), signal.SIGINT) # Simulate Ctrl-C.
# The break is recorded rather than interrupting the program.
assert bc.hit()
assert bc.frame() is not None
BreakControl
¶
Context manager for handling break (SIGINT) signals.
Registers itself with a global SIGINT handler on open() and unregisters on close(). Tracks whether a break signal has been received and the frame at which it occurred. Multiple instances can be active at once.
Source code in py_misc_utils/break_control.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
open()
¶
Register this BreakControl with the global SIGINT handler.
If this is the first handler, installs the global signal handler.
Returns:
| Type | Description |
|---|---|
|
self, for use as a context manager. |
Source code in py_misc_utils/break_control.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
close()
¶
Unregister this BreakControl from the global SIGINT handler.
If this is the last handler, removes the global signal handler.
Source code in py_misc_utils/break_control.py
71 72 73 74 75 76 77 78 79 | |
trigger(frame)
¶
Mark this BreakControl as hit and record the frame.
Called by the global signal handler when SIGINT is received.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
The stack frame at the time of the signal. |
required |
Source code in py_misc_utils/break_control.py
102 103 104 105 106 107 108 109 110 111 | |
hit()
¶
Check whether a break signal has been received.
Returns:
| Type | Description |
|---|---|
|
True if a break has been received, False otherwise. |
Source code in py_misc_utils/break_control.py
113 114 115 116 117 118 119 | |
frame()
¶
Return the stack frame where the break was received.
Returns:
| Type | Description |
|---|---|
|
The frame object, or None if no break has been received. |
Source code in py_misc_utils/break_control.py
121 122 123 124 125 126 127 | |
create()
¶
Create a BreakControl wrapped with fin_wrap for automatic cleanup.
Creates a BreakControl, opens it, and registers its close() as a finalizer via fin_wrap_np so it is automatically closed when the returned wrapper is garbage collected.
Returns:
| Type | Description |
|---|---|
|
A fin_wrap wrapper that will call bc.close() on cleanup. |
Source code in py_misc_utils/break_control.py
130 131 132 133 134 135 136 137 138 139 140 141 142 | |