Skip to content

Commit d8e5b87

Browse files
committed
move sync_perform to the main module, instead of just testing.
1 parent fede858 commit d8e5b87

3 files changed

Lines changed: 41 additions & 34 deletions

File tree

effect/__init__.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262

6363
import sys
6464

65+
import six
66+
6567
from functools import wraps
6668

6769
from .continuation import trampoline
@@ -283,3 +285,39 @@ def parallel(effects):
283285
the same order as the input to this function.
284286
"""
285287
return Effect(ParallelEffects(list(effects)))
288+
289+
290+
class NotSynchronousError(Exception):
291+
"""Raised when performing an effect wasn't synchronous."""
292+
293+
294+
def sync_perform(effect, dispatcher=default_dispatcher):
295+
"""
296+
Perform an effect, and return the value that its last callback or error
297+
handler returns. If the final callback raises an exception, the exception
298+
will be raised.
299+
300+
This requires that the effect (and all effects returned from any of its
301+
callbacks) to be synchronous -- in other words, the effect performers
302+
must pass the result to the box before returning.
303+
304+
If this is not the case, NotSynchronousError will be raised.
305+
"""
306+
successes = []
307+
errors = []
308+
309+
def success(x):
310+
successes.append(x)
311+
312+
def error(x):
313+
errors.append(x)
314+
315+
effect = effect.on(success=success, error=error)
316+
perform(effect, dispatcher=dispatcher)
317+
if successes:
318+
return successes[0]
319+
elif errors:
320+
six.reraise(*errors[0])
321+
else:
322+
raise NotSynchronousError("Performing %r was not synchronous!"
323+
% (effect,))

effect/test_effect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
raises, MatchesPredicateWithParams)
66

77
from . import (Effect, NoEffectHandlerError, synchronous_performer, perform,
8-
default_dispatcher)
9-
from .testing import StubIntent, sync_perform
8+
default_dispatcher, sync_perform)
9+
from .testing import StubIntent
1010

1111

1212
class SelfContainedIntent(object):

effect/testing.py

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import sys
88

9-
from . import Effect, synchronous_performer, perform, default_dispatcher, guard
9+
from . import Effect, synchronous_performer, guard
1010

1111
import six
1212

@@ -84,34 +84,3 @@ def resolve_stub(effect):
8484
StubIntent.
8585
"""
8686
return resolve_effect(effect, effect.intent.result)
87-
88-
89-
def sync_perform(effect, dispatcher=default_dispatcher):
90-
"""
91-
Perform an effect, and return the value that its last callback or error
92-
handler returns. If the final callback raises an exception, the exception
93-
will be raised.
94-
95-
This requires that the effect (and all effects returned from any of its
96-
callbacks) to be synchronous -- in other words, the effect performers
97-
must pass the result to the box before returning.
98-
99-
If this is not the case, an AssertionError will be raised.
100-
"""
101-
successes = []
102-
errors = []
103-
104-
def SUCC(x):
105-
successes.append(x)
106-
107-
def ERR(x):
108-
errors.append(x)
109-
110-
effect = effect.on(success=SUCC, error=ERR)
111-
perform(effect, dispatcher=dispatcher)
112-
if successes:
113-
return successes[0]
114-
elif errors:
115-
six.reraise(*errors[0])
116-
else:
117-
raise AssertionError("Performing %r was not synchronous!" % (effect,))

0 commit comments

Comments
 (0)