-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpy3support.py
More file actions
51 lines (42 loc) · 1.77 KB
/
py3support.py
File metadata and controls
51 lines (42 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from __future__ import print_function
import sys
from inspect import isgenerator
from inspect import isgeneratorfunction
from logging import getLogger
from aspectlib import ExpectedGenerator
from aspectlib import Proceed
from aspectlib import Return
from aspectlib import UnacceptableAdvice
from aspectlib import mimic
from aspectlib.utils import logf
logger = getLogger(__name__)
logdebug = logf(logger.debug)
def decorate_advising_generator_py3(advising_function, cutpoint_function, bind):
assert isgeneratorfunction(cutpoint_function)
def advising_generator_wrapper_py3(*args, **kwargs):
gen = cutpoint_function(*args, **kwargs)
while True:
if bind:
advisor = advising_function(cutpoint_function, *args, **kwargs)
else:
advisor = advising_function(*args, **kwargs)
if not isgenerator(advisor):
raise ExpectedGenerator("advising_function %s did not return a generator." % advising_function)
advice = next(advisor)
logdebug('Got advice %r from %s', advice, advising_function)
result = None
if advice is Proceed or advice is None or isinstance(advice, Proceed):
try:
result = next(gen)
advice = advisor.send(result)
except StopIteration as e:
return e.value
except BaseException:
advice = advisor.throw(*sys.exc_info())
if advice is Return:
yield result
elif isinstance(advice, Return):
yield advice.value
else:
raise UnacceptableAdvice("Unknown advice %s" % advice)
return mimic(advising_generator_wrapper_py3, cutpoint_function)