1
2
3 import traceback
4 import sys
5
6 if False:
7 print "Using Blaise's implementation"
11 self.value = value
12 raise self
13
16 def function_internal(*args, **kwargs):
17 f = func(*args, **kwargs)
18 m = f.send
19 args = (None, )
20 while True:
21 try:
22 yield_val = m(*args)
23 except StopIteration:
24 raise returnValue(None)
25 else:
26 try:
27 m = f.send
28 if yield_val is None:
29
30 args = yield
31 args = (args, )
32 else:
33
34 m = f.send
35 r = yield_val
36 val = None
37 while True:
38 try:
39 r.send(val)
40 val = yield
41 except returnValue, r:
42 args = (r.value, )
43 break
44 except:
45 m = f.throw
46 args = list(sys.exc_info())
47
48 try:
49 args[2] = args[2].tb_next.tb_next
50 except:
51 pass
52
53 return function_internal
54
58
59 @function
61 yield
62 try:
63 out = yield f
64 print name, "returned:", out
65 except:
66 print name, "had exception:"
67 tb = list(sys.exc_info())
68
69 traceback.print_exception(*tb)
70
71 - def run(f, name = "<unnamed>"):
72 try:
73 r = start(print_result(f, name))
74 while True:
75 r.send(None)
76 except returnValue, r:
77 pass
78
79 else:
80
81 from twisted.internet import defer
82
83 function = defer.inlineCallbacks
84 returnValue = defer.returnValue
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 @function
103
104 out = yield f
105 print name, "returned:", out
106
107
108
109
110
111
112 - def run(f, name = "<unnamed>"):
113 try:
114 r = print_result(f, name)
115 except returnValue, r:
116 pass
117
118 @function
119 -def f(a, b):
122
123 @function
124 -def g2():
125 raise Exception("Booh!")
126 yield
127
128 @function
129 -def g(a, b):
132
133 @function
134 -def h(a, b):
137
138 @function
139 -def i(a, b, c):
140 out = yield (f(a, b))
141 out2 = f(out, c)
142 out = yield out2
143 returnValue(out)
144
146 def demo(s):
147 run(eval(s), s)
148
149 demo("f(1,2)")
150 print
151 sys.stdout.flush()
152 demo("g(1,2)")
153 print
154 sys.stdout.flush()
155 demo("h(1,2)")
156 print
157 sys.stdout.flush()
158 demo("i(1,2,3)")
159 sys.stdout.flush()
160
161 if __name__ == "__main__":
162 main()
163
164
165
166