Skip to content

Commit eb66c21

Browse files
committed
get rid of exc_info-using code
1 parent d125faa commit eb66c21

File tree

2 files changed

+16
-35
lines changed

2 files changed

+16
-35
lines changed

txeffect/__init__.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
does.
1414
"""
1515

16-
from __future__ import absolute_import
17-
1816
from functools import partial
19-
import sys
2017

2118
from twisted.internet.defer import Deferred
2219
from twisted.python.failure import Failure
@@ -27,15 +24,15 @@
2724
ParallelEffects,
2825
perform as base_perform,
2926
TypeDispatcher)
30-
from effect.async import perform_parallel_async
27+
from effect.parallel_async import perform_parallel_async
3128
from effect._utils import wraps # whoah there
3229

3330

3431
def deferred_to_box(d, box):
3532
"""
3633
Make a Deferred pass its success or fail events on to the given box.
3734
"""
38-
d.addCallbacks(box.succeed, lambda f: box.fail((f.type, f.value, f.tb)))
35+
d.addCallbacks(box.succeed, lambda f: box.fail(f.value))
3936

4037

4138
def make_twisted_dispatcher(reactor):
@@ -71,8 +68,8 @@ def deferred_wrapper(*args, **kwargs):
7168
pass_args = args[:-1]
7269
try:
7370
result = f(*pass_args, **kwargs)
74-
except:
75-
box.fail(sys.exc_info())
71+
except Exception as e:
72+
box.fail(e)
7673
else:
7774
if isinstance(result, Deferred):
7875
deferred_to_box(result, box)
@@ -97,11 +94,6 @@ def perform(dispatcher, effect):
9794
d = Deferred()
9895
eff = effect.on(
9996
success=d.callback,
100-
error=lambda e: d.errback(exc_info_to_failure(e)))
97+
error=lambda e: d.errback(e))
10198
base_perform(dispatcher, eff)
10299
return d
103-
104-
105-
def exc_info_to_failure(exc_info):
106-
"""Convert an exc_info tuple to a :class:`Failure`."""
107-
return Failure(exc_info[1], exc_info[0], exc_info[2])

txeffect/test_txeffect.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55

66
from testtools import TestCase
7-
from testtools.matchers import MatchesListwise, Equals, MatchesException
7+
from testtools.matchers import MatchesListwise, Equals
88

99
from twisted.trial.unittest import SynchronousTestCase
1010
from twisted.internet.defer import Deferred
@@ -16,9 +16,9 @@
1616
base_dispatcher, parallel,
1717
ComposedDispatcher,
1818
)
19+
from effect._test_utils import MatchesException
1920
from . import (
2021
deferred_performer,
21-
exc_info_to_failure,
2222
make_twisted_dispatcher,
2323
perform)
2424

@@ -95,14 +95,22 @@ def test_perform_failure(self):
9595
effect.twisted.perform fails the Deferred it returns if the ultimate
9696
result of the Effect is an exception.
9797
"""
98+
try:
99+
raise ValueError("oh dear")
100+
except Exception as e:
101+
exc = e
102+
98103
boxes = []
99104
e = Effect(boxes.append)
100105
d = perform(func_dispatcher, e)
101106
self.assertNoResult(d)
102-
boxes[0].fail((ValueError, ValueError("oh dear"), None))
107+
boxes[0].fail(exc)
103108
f = self.failureResultOf(d)
104109
self.assertEqual(f.type, ValueError)
105110
self.assertEqual(str(f.value), 'oh dear')
111+
self.assertRegex(
112+
f.getTraceback().splitlines()[-3],
113+
'^\s+File ".*?test_txeffect.py", line \d+, in test_perform_failure$')
106114

107115

108116
class DeferredPerformerTests(TestCase):
@@ -219,22 +227,3 @@ def p(dispatcher, intent, extra):
219227
dispatcher = lambda _: partial(p, extra='extra val')
220228
result = self.successResultOf(perform(dispatcher, Effect('foo')))
221229
self.assertEqual(result, 'extra val')
222-
223-
224-
class ExcInfoToFailureTests(TestCase):
225-
"""Tests for :func:`exc_info_to_failure`."""
226-
227-
def test_exc_info_to_failure(self):
228-
"""
229-
:func:`exc_info_to_failure` converts an exc_info tuple to a
230-
:obj:`Failure`.
231-
"""
232-
try:
233-
raise RuntimeError("foo")
234-
except:
235-
exc_info = sys.exc_info()
236-
237-
failure = exc_info_to_failure(exc_info)
238-
self.assertIs(failure.type, RuntimeError)
239-
self.assertEqual(str(failure.value), "foo")
240-
self.assertIs(failure.tb, exc_info[2])

0 commit comments

Comments
 (0)