From 0a3271ca8b2273a5c1d3b367b99f0be3ade949a3 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 5 Dec 2016 18:02:15 +0100 Subject: [PATCH 1/3] Do not record errors for terminals --- CHANGES | 1 + raven/base.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 73e190e96..0135267fa 100644 --- a/CHANGES +++ b/CHANGES @@ -9,6 +9,7 @@ Version 6.0.0 * Refactored transports to support multiple URLs. This might affect you if you have custom subclasses of those. The main change is that the URL parameter moved from the constructor into the `send` method. +* Do not record from the sys except hook if stdin is a terminal. Version 5.32.0 -------------- diff --git a/raven/base.py b/raven/base.py index 2f147cffe..7c0291939 100644 --- a/raven/base.py +++ b/raven/base.py @@ -242,14 +242,22 @@ def set_dsn(self, dsn=None, transport=None): self.logger.debug("Configuring Raven for host: {0}".format(self.remote)) - def install_sys_hook(self): + def install_sys_hook(self, skip_for_tty=True): global __excepthook__ if __excepthook__ is None: __excepthook__ = sys.excepthook def handle_exception(*exc_info): - self.captureException(exc_info=exc_info, level='fatal') + if not skip_for_tty: + capture = True + else: + try: + capture = not sys.stdout.isatty() + except Exception: + capture = True + if capture: + self.captureException(exc_info=exc_info, level='fatal') __excepthook__(*exc_info) handle_exception.raven_client = self sys.excepthook = handle_exception From 1a99705938e94ad41d7e0d2ae5bb9faba9b3bc7c Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 5 Dec 2016 20:50:18 +0100 Subject: [PATCH 2/3] Check stdin and not stdout for tty status --- raven/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/raven/base.py b/raven/base.py index 7c0291939..b80dd9794 100644 --- a/raven/base.py +++ b/raven/base.py @@ -253,7 +253,7 @@ def handle_exception(*exc_info): capture = True else: try: - capture = not sys.stdout.isatty() + capture = not sys.stdin.isatty() except Exception: capture = True if capture: From fd01cfb78923b9abc166f4cdbcf259f432ac8f11 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 5 Dec 2016 20:50:29 +0100 Subject: [PATCH 3/3] Added test for sys hook behavior --- tests/base/tests.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/base/tests.py b/tests/base/tests.py index 1ef4402fa..f2e54858b 100644 --- a/tests/base/tests.py +++ b/tests/base/tests.py @@ -585,3 +585,26 @@ def test_no_sys_argv(self): Client() finally: sys.argv = argv + + def test_sys_hook(self): + client = TempStoreClient(install_sys_hook=True) + try: + 1 / 0 + except Exception: + sys.excepthook(*sys.exc_info()) + + event = client.events.pop(0) + exc = event['exception']['values'][-1] + assert exc['type'] == 'ZeroDivisionError' + assert not client.events + + sys.stdin.isatty = lambda: True + try: + try: + 1 / 0 + except Exception: + sys.excepthook(*sys.exc_info()) + + assert not client.events + finally: + del sys.stdin.isatty