Skip to content

Commit

Permalink
Merge pull request #23 from gregoil/feature/add-breakpoint-signal
Browse files Browse the repository at this point in the history
Added breakpoint signal
  • Loading branch information
osherdp committed May 1, 2019
2 parents 9575734 + bf8ae8a commit 07f7c39
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
7 changes: 6 additions & 1 deletion ipdbugger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
import types
import inspect
import traceback
from bdb import BdbQuit

import colorama
from termcolor import colored
from future.utils import raise_
from IPython.terminal.debugger import TerminalPdb

from .signals import register_break_signal

# Enable color printing on screen.
colorama.init()

Expand Down Expand Up @@ -287,7 +290,8 @@ def get_last_lineno(node):
return max_lineno


def debug(victim=None, ignore_exceptions=(), catch_exception=None, depth=0):
def debug(victim=None, ignore_exceptions=(BdbQuit,),
catch_exception=None, depth=0):
"""A decorator function to catch exceptions and enter debug mode.
Args:
Expand All @@ -313,6 +317,7 @@ def wrapper(real_victim):

return wrapper

register_break_signal()
if inspect.isfunction(victim):
if hasattr(victim, '_ipdebug_wrapped'):
# Don't wrap the function more than once
Expand Down
31 changes: 31 additions & 0 deletions ipdbugger/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Signals usage in Rotest."""
# pylint: disable=global-statement,no-member
import sys
import signal


BREAKPOINT_SIGNAL_REGISTERED = False


class BreakPointException(Exception):
"""An exception to raise on the break signal."""


def raise_exception_handler(_signum, _frame):
"""Raise a break-point exception."""
raise BreakPointException()


def register_break_signal():
"""Register raising an exception on break signal if needed."""
global BREAKPOINT_SIGNAL_REGISTERED
if BREAKPOINT_SIGNAL_REGISTERED:
return

BREAKPOINT_SIGNAL_REGISTERED = True

if sys.platform == "win32":
signal.signal(signal.SIGBREAK, raise_exception_handler)

else:
signal.signal(signal.SIGQUIT, raise_exception_handler)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Setup file for handling packaging and distribution."""
from setuptools import setup

__version__ = "2.4.1"
__version__ = "2.5.0"

setup(
name="ipdbugger",
Expand Down
15 changes: 15 additions & 0 deletions tests/test_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,21 @@ def func_upper():
assert name_saver.func_name == "func_lowest"


def test_depth_and_catch_attribute():
"""Test wrapping a function one more call level."""
def func_lower():
raise utils.TestError()

def func_upper():
try:
func_lower()
except (utils.TestError, ValueError):
pass

func_upper = debug(func_upper, depth=1)
func_upper()


def test_ignore_all_exceptions():
"""Test ignoring all exceptions."""
def func():
Expand Down

0 comments on commit 07f7c39

Please sign in to comment.