Skip to content

Commit

Permalink
Use warnings instead
Browse files Browse the repository at this point in the history
  • Loading branch information
kitchoi committed May 22, 2020
1 parent 102be64 commit 420eab0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
26 changes: 14 additions & 12 deletions traits/observation/exception_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
# This module provides the push_exception_handler and pop_exception_handler
# for the observers.

import logging
import warnings
import sys


_logger = logging.getLogger("traits")
import traceback


class ObserverExceptionHandler:
Expand All @@ -25,27 +23,31 @@ class ObserverExceptionHandler:
----------
handler : callable(event) or None
A callable to handle an event, in the context of
an exception. If None, the exceptions will be logged.
an exception. If None, the exceptions will result in a warning.
reraise_exceptions : boolean
Whether to reraise the exception.
"""

def __init__(self, handler, reraise_exceptions):
self.handler = handler if handler is not None else self._log_exception
self.handler = handler if handler is not None else self._warn
self.reraise_exceptions = reraise_exceptions

def _log_exception(self, event):
""" A handler that logs the exception with the given event.
def _warn(self, event):
""" A handler that warns about the exception with the given event.
Parameters
----------
event : object
An event object emitted by the notification.
"""
_logger.exception(
_, _, tb = sys.exc_info()
texts = traceback.format_tb(tb)
warnings.warn(
"Exception occurred in traits notification handler "
"for event object: %r",
event,
"for event object: {!r}\n{}".format(
event, "\n".join(texts)
),
RuntimeWarning,
)


Expand All @@ -70,7 +72,7 @@ def push_exception_handler(
----------
handler : callable(event) or None
A callable to handle an event, in the context of
an exception. If None, the exceptions will be logged.
an exception. If None, exceptions will result in warnings.
reraise_exceptions : boolean
Whether to reraise the exception.
"""
Expand Down
10 changes: 4 additions & 6 deletions traits/observation/tests/test_exception_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@ class TestExceptionHandling(unittest.TestCase):
def test_default_logging(self):
stack = ObserverExceptionHandlerStack()

with self.assertLogs("traits", level="ERROR") as log_context:
with self.assertWarns(RuntimeWarning) as warn_context:
try:
raise ZeroDivisionError()
except Exception:
stack.handle_exception("Event")

content, = log_context.output
self.assertIn(
"Exception occurred in traits notification handler for "
"event object: {!r}".format("Event"),
content,
str(warn_context.warning),
)

def test_push_exception_handler(self):
Expand All @@ -45,16 +44,15 @@ def test_push_exception_handler(self):

stack.push_exception_handler(reraise_exceptions=True)

with self.assertLogs("traits", level="ERROR") as log_context, \
with self.assertWarns(RuntimeWarning) as warn_context, \
self.assertRaises(ZeroDivisionError):

try:
raise ZeroDivisionError()
except Exception:
stack.handle_exception("Event")

content, = log_context.output
self.assertIn("ZeroDivisionError", content)
self.assertIn("ZeroDivisionError", str(warn_context.warning))

def test_push_exception_handler_collect_events(self):

Expand Down
7 changes: 3 additions & 4 deletions traits/observation/tests/test_trait_event_notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,16 @@ def misbehaving_handler(event):
notifier = create_notifier(handler=misbehaving_handler)

# when
with self.assertLogs("traits", level="ERROR") as log_exception:
with self.assertWarns(RuntimeWarning) as warn_context:
notifier(a=1, b=2)

# then
content, = log_exception.output
self.assertIn(
"Exception occurred in traits notification handler",
content,
str(warn_context.warning),
)
# The tracback should be included
self.assertIn("ZeroDivisionError", content)
self.assertIn("ZeroDivisionError", str(warn_context.warning))


class TestTraitEventNotifierEqual(unittest.TestCase):
Expand Down

0 comments on commit 420eab0

Please sign in to comment.