Repository navigation
Expand file tree
/
Copy pathtest_conts_topo.py
More file actions
230 lines (211 loc) · 7.24 KB
/
Copy pathtest_conts_topo.py
File metadata and controls
230 lines (211 loc) · 7.24 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# -*- coding: utf-8 -*-
"""Test exotic program topologies.
For pictures, see ``doc/callcc_topology.pdf`` in the source distribution.
"""
from ...syntax import macros, test # noqa: F401
from ...test.fixtures import session, testset
from inspect import stack
from ...syntax import macros, continuations, call_cc # noqa: F401, F811
def me():
"""Return the caller's function name."""
callstack = stack()
framerecord = callstack[1] # ignore me() itself, get caller's record
# Python 3.5+ have named fields here.
# named tuple FrameInfo(frame, filename, lineno, function, code_context, index)
# https://docs.python.org/3/library/inspect.html#the-interpreter-stack
# But on 3.4:
# When the following functions return “frame records,” each record is a
# tuple of six items: the frame object, the filename, the line number of
# the current line, the function name, a list of lines of context from the
# source code, and the index of the current line within that list.
# https://docs.python.org/3.4/library/inspect.html#the-interpreter-stack
return framerecord[3] # function name
# return framerecord.function # Python 3.5+
def runtests():
with testset("basic case: one continuation"):
with continuations:
out = []
def g(cc):
out.append(me())
def f():
out.append(me())
call_cc[g()]
out.append(me())
f()
test[out == ['f', 'g', 'f_cont']]
with testset("sequence of continuations"):
with continuations:
out = []
def g(cc): # noqa: F811, the previous one is no longer used.
out.append(me())
def h(cc):
out.append(me())
def f():
out.append(me())
call_cc[g()]
out.append(me())
call_cc[h()]
out.append(me())
f()
test[out == ['f', 'g', 'f_cont1', 'h', 'f_cont2']] # gensym -> cont1, ...
with testset("nested continuations, case 1"): # left in the picture
with continuations:
out = []
def h(cc): # noqa: F811, the previous one is no longer used.
out.append(me())
def g(cc): # noqa: F811, the previous one is no longer used.
out.append(me())
call_cc[h()]
out.append(me())
def f():
out.append(me())
call_cc[g()]
out.append(me())
f()
test[out == ['f', 'g', 'h', 'g_cont', 'f_cont3']]
with testset("nested continuations, case 2a, tail-call in f1"): # right in the picture
with continuations:
out = []
def w(cc):
out.append(me())
def v():
out.append(me())
call_cc[w()]
out.append(me())
def f1(cc):
out.append(me())
return v()
# To be eligible to act as a continuation, f2 must accept
# one positional arg, because the implicit "return None" in v()
# will send one (then passed along by f1).
def f2(dummy):
out.append(me())
f1(cc=f2)
test[out == ['f1', 'v', 'w', 'v_cont', 'f2']]
with testset("nested continuations, case 2b, call_cc in f1"):
with continuations:
out = []
def w(cc): # noqa: F811, the previous one is no longer used.
out.append(me())
def v(cc): # noqa: F811, the previous one is no longer used.
out.append(me())
call_cc[w()]
out.append(me())
def f1(cc):
out.append(me())
call_cc[v()]
out.append(me())
def f2(dummy):
out.append(me())
f1(cc=f2)
test[out == ['f1', 'v', 'w', 'v_cont1', 'f1_cont', 'f2']]
# preparation for confetti, create a saved chain
with continuations:
out = []
k = None
def h(cc): # noqa: F811, the previous one is no longer used.
nonlocal k
k = cc
out.append(me())
def g(cc): # noqa: F811, the previous one is no longer used.
out.append(me())
call_cc[h()]
out.append(me()) # g_cont1
def f():
out.append(me())
call_cc[g()]
out.append(me()) # f_cont4
f()
with testset("confetti 1a - call_cc'ing into a saved continuation"):
with continuations:
out = []
def v(): # noqa: F811, the previous one is no longer used.
out.append(me())
call_cc[k()]
out.append(me())
v()
test[out == ['v', 'g_cont1', 'f_cont4', 'v_cont2']]
with testset("confetti 1b - tail-calling a saved continuation"):
with continuations:
out = []
def f2(dummy):
out.append(me())
def f1():
out.append(me())
return k(cc=f2)
f1()
test[out == ['f1', 'g_cont1', 'f_cont4', 'f2']]
# more preparation for confetti
with continuations:
out = []
k2 = None
def t(cc):
nonlocal k2
k2 = cc
out.append(me())
def s(cc):
out.append(me())
call_cc[t()]
out.append(me()) # s_cont
def r():
out.append(me())
call_cc[s()]
out.append(me()) # r_cont
r()
out = []
k3 = None
def qq(cc):
nonlocal k3
k3 = cc
out.append(me())
def q(cc):
out.append(me())
call_cc[qq()]
out.append(me()) # q_cont
def p():
out.append(me())
call_cc[q()]
out.append(me()) # p_cont
return k2()
p()
with testset("confetti 2a"): # second picture from bottom
with continuations:
out = []
def f2(dummy):
out.append(me())
def f1():
out.append(me())
return k3(cc=f2)
f1()
test[out == ['f1', 'q_cont', 'p_cont', 's_cont', 'r_cont', 'f2']]
# more preparation for confetti
with continuations:
out = []
k4 = None
def z(cc):
nonlocal k4
k4 = cc
out.append(me())
def y(cc):
out.append(me())
call_cc[z()]
out.append(me()) # y_cont
return k2()
def x():
out.append(me())
call_cc[y()]
out.append(me()) # x_cont
x()
with testset("confetti 2b"): # bottommost picture
with continuations:
out = []
def f2(dummy):
out.append(me())
def f1():
out.append(me())
return k4(cc=f2)
f1()
test[out == ['f1', 'y_cont', 's_cont', 'r_cont', 'x_cont', 'f2']]
if __name__ == '__main__': # pragma: no cover
with session(__file__):
runtests()