Skip to content

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
class 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.
  """

  def __init__(self):
    """Initialize a BreakControl with no hit and no frame."""
    self._hit = False
    self._frame = None

  def open(self):
    """Register this BreakControl with the global SIGINT handler.

    If this is the first handler, installs the global signal handler.

    Returns:
      self, for use as a context manager.
    """
    with _LOCK:
      if not _HANDLERS:
        sgn.signal(signal.SIGINT, _handler)
      _HANDLERS.add(self)

    return self

  def close(self):
    """Unregister this BreakControl from the global SIGINT handler.

    If this is the last handler, removes the global signal handler.
    """
    with _LOCK:
      _HANDLERS.remove(self)
      if not _HANDLERS:
        sgn.unsignal(signal.SIGINT, _handler)

  def __enter__(self):
    """Enter the context manager by opening this BreakControl.

    Returns:
      self.
    """
    return self.open()

  def __exit__(self, *exc):
    """Exit the context manager by closing this BreakControl.

    Args:
      *exc: Exception info (unused).

    Returns:
      False, to not suppress exceptions.
    """
    self.close()

    return False

  def trigger(self, frame):
    """Mark this BreakControl as hit and record the frame.

    Called by the global signal handler when SIGINT is received.

    Args:
      frame: The stack frame at the time of the signal.
    """
    self._hit = True
    self._frame = frame

  def hit(self):
    """Check whether a break signal has been received.

    Returns:
      True if a break has been received, False otherwise.
    """
    return self._hit

  def frame(self):
    """Return the stack frame where the break was received.

    Returns:
      The frame object, or None if no break has been received.
    """
    return self._frame

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
def open(self):
  """Register this BreakControl with the global SIGINT handler.

  If this is the first handler, installs the global signal handler.

  Returns:
    self, for use as a context manager.
  """
  with _LOCK:
    if not _HANDLERS:
      sgn.signal(signal.SIGINT, _handler)
    _HANDLERS.add(self)

  return self

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
def close(self):
  """Unregister this BreakControl from the global SIGINT handler.

  If this is the last handler, removes the global signal handler.
  """
  with _LOCK:
    _HANDLERS.remove(self)
    if not _HANDLERS:
      sgn.unsignal(signal.SIGINT, _handler)

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
def trigger(self, frame):
  """Mark this BreakControl as hit and record the frame.

  Called by the global signal handler when SIGINT is received.

  Args:
    frame: The stack frame at the time of the signal.
  """
  self._hit = True
  self._frame = frame

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
def hit(self):
  """Check whether a break signal has been received.

  Returns:
    True if a break has been received, False otherwise.
  """
  return self._hit

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
def frame(self):
  """Return the stack frame where the break was received.

  Returns:
    The frame object, or None if no break has been received.
  """
  return self._frame

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
def 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:
    A fin_wrap wrapper that will call bc.close() on cleanup.
  """
  bc = BreakControl()

  return fw.fin_wrap_np(bc.open(), bc.close)