From 9326a732a4e3d27a1bba7aa71d58516a6aff2148 Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Mon, 6 Jan 2014 14:15:10 -0500 Subject: [PATCH] MAINT: Make exception handling tests compatible between Python 2 and 3 Python 3 removes the `.message` attribute, so use `str` instead. Also, the divide by zero message has changed slightly between versions, so just check for the exception type, instead of also checking the message. --- tests/test_exception_handling.py | 21 +++++---------------- zipline/utils/test_utils.py | 3 +++ 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/tests/test_exception_handling.py b/tests/test_exception_handling.py index 6d24908f..0002fd8a 100644 --- a/tests/test_exception_handling.py +++ b/tests/test_exception_handling.py @@ -57,14 +57,9 @@ class ExceptionTestCase(TestCase): **self.zipline_test_config ) - with self.assertRaises(ZeroDivisionError) as ctx: + with self.assertRaises(ZeroDivisionError): output, _ = drain_zipline(self, zipline) - self.assertEqual( - ctx.exception.message, - 'integer division or modulo by zero' - ) - def test_tranform_exception(self): exc_tnfm = StatefulTransform(ExceptionTransform) self.zipline_test_config['transforms'] = [exc_tnfm] @@ -76,7 +71,7 @@ class ExceptionTestCase(TestCase): with self.assertRaises(AssertionError) as ctx: output, _ = drain_zipline(self, zipline) - self.assertEqual(ctx.exception.message, + self.assertEqual(str(ctx.exception), 'An assertion message') def test_exception_in_handle_data(self): @@ -96,7 +91,7 @@ class ExceptionTestCase(TestCase): with self.assertRaises(Exception) as ctx: output, _ = drain_zipline(self, zipline) - self.assertEqual(ctx.exception.message, + self.assertEqual(str(ctx.exception), 'Algo exception in handle_data') def test_zerodivision_exception_in_handle_data(self): @@ -113,12 +108,9 @@ class ExceptionTestCase(TestCase): **self.zipline_test_config ) - with self.assertRaises(ZeroDivisionError) as ctx: + with self.assertRaises(ZeroDivisionError): output, _ = drain_zipline(self, zipline) - self.assertEqual(ctx.exception.message, - 'integer division or modulo by zero') - def test_set_portfolio(self): """ Are we protected against overwriting an algo's portfolio? @@ -136,8 +128,5 @@ class ExceptionTestCase(TestCase): **self.zipline_test_config ) - with self.assertRaises(AttributeError) as ctx: + with self.assertRaises(AttributeError): output, _ = drain_zipline(self, zipline) - - self.assertEqual(ctx.exception.message, - "can't set attribute") diff --git a/zipline/utils/test_utils.py b/zipline/utils/test_utils.py index 0dec5cec..9f348c3b 100644 --- a/zipline/utils/test_utils.py +++ b/zipline/utils/test_utils.py @@ -95,6 +95,9 @@ class ExceptionSource(object): def next(self): 5 / 0 + def __next__(self): + 5 / 0 + class ExceptionTransform(object):