/
execution.py
463 lines (386 loc) · 13.2 KB
/
execution.py
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import os
from nose.tools import istest, nottest, assert_equal, assert_not_equal, assert_in
import tempman
from .. import testing
from ..testing import program_path
# TODO: add tests for with statements (requires class definition support)
@nottest
class ExecutionTests(object):
@istest
def empty_program_runs_without_output(self):
result = self._run_program(path=program_path("valid/empty.py"), program="empty")
assert_equal(b"", result.output)
assert_equal(b"", result.stderr_output)
@istest
def print_program_prints_to_stdout(self):
result = self._run_program(path=program_path("valid/print.py"), program="print")
assert_equal(b"42\n", result.output)
assert_equal(b"", result.stderr_output)
@istest
def print_def_program_prints_to_stdout(self):
result = self._run_program(path=program_path("valid/print_def.py"), program="print_def")
assert_equal(b"42\n", result.output)
assert_equal(b"", result.stderr_output)
@istest
def function_calls(self):
program = """
#:: x: int, y: str -> none
def f(x, y):
print(x)
print(y)
f(42, y="blah")
"""
self._test_program_string(program, b"42\nblah\n")
@istest
def fib_program_prints_result_to_stdout(self):
result = self._run_program(path=program_path("valid/fib.py"), program="fib")
assert_equal(b"55\n", result.output)
assert_equal(b"", result.stderr_output)
@istest
def can_read_attributes_of_builtins(self):
result = self._run_program(path=program_path("valid/attribute_read.py"), program="attribute_read")
assert_equal(b"1\n", result.output)
assert_equal(b"", result.stderr_output)
@istest
def can_import_value_from_local_package(self):
result = self._run_program(path=program_path("valid/import_value_from_local_package"), program="main")
assert_equal(b"Hello\n", result.output)
assert_equal(b"", result.stderr_output)
@istest
def can_import_local_package(self):
result = self._run_program(path=program_path("valid/import_local_package"), program="main")
assert_equal(b"Hello\n", result.output)
assert_equal(b"", result.stderr_output)
@istest
def can_import_local_module(self):
result = self._run_program(path=program_path("valid/import_local_module"), program="main")
assert_equal(b"Hello\n", result.output)
assert_equal(b"", result.stderr_output)
@istest
def can_import_module_in_package(self):
result = self._run_program(path=program_path("valid/import_module_in_package"), program="main")
assert_equal(b"Hello\n", result.output)
assert_equal(b"", result.stderr_output)
@istest
def test_output_of_bool(self):
result = self._run_program(path=program_path("valid/bool.py"), program="bool")
expected_output = b"""False
True
False
True
False
True
False
True
"""
assert_equal(expected_output, result.output)
assert_equal(b"", result.stderr_output)
@istest
def test_arithmetic(self):
result = self._run_program(path=program_path("valid/arithmetic.py"), program="arithmetic")
assert_equal(b"19\n1.25\n1\n", result.output)
assert_equal(b"", result.stderr_output)
@istest
def test_call_int_magic_method_directly(self):
self._test_expression("(4).__add__(5)", b"9")
@istest
def test_add_int(self):
self._test_expression("4 + 5", b"9")
@istest
def test_sub_int(self):
self._test_expression("4 - 5", b"-1")
@istest
def test_mul_int(self):
self._test_expression("4 * 5", b"20")
@istest
def test_truediv_int(self):
self._test_expression("1 / 2", b"0.5")
self._test_expression("(0 - 1) / 2", b"-0.5")
@istest
def test_floordiv_int(self):
self._test_expression("1 // 2", b"0")
self._test_expression("(0 - 1) // 2", b"-1")
@istest
def test_mod_int(self):
self._test_expression("5 % 3", b"2")
self._test_expression("(0 - 5) % 3", b"1")
self._test_expression("5 % (0 - 3)", b"-1")
self._test_expression("(0 - 5) % (0 - 3)", b"-2")
@istest
def test_divmod_int(self):
self._test_expression("divmod(5, 3)", b"(1, 2)")
self._test_expression("divmod(-5, 3)", b"(-2, 1)")
self._test_expression("divmod(5, -3)", b"(-2, -1)")
self._test_expression("divmod(-5, -3)", b"(1, -2)")
@istest
def test_pow_int(self):
self._test_expression("2 ** 3", b"8")
self._test_expression("2 ** -3", b"0.125")
@istest
def test_lshift_int(self):
self._test_expression("5 << 3", b"40")
self._test_expression("-5 << 3", b"-40")
@istest
def test_rshift_int(self):
self._test_expression("41 >> 3", b"5")
self._test_expression("-41 >> 3", b"-6")
@istest
def test_bitwise_and_int(self):
self._test_expression("25 & 51", b"17")
self._test_expression("-25 & 51", b"35")
@istest
def test_bitwise_or_int(self):
self._test_expression("25 | 51", b"59")
self._test_expression("-25 | 51", b"-9")
@istest
def test_bitwise_xor_int(self):
self._test_expression("25 ^ 51", b"42")
self._test_expression("-25 ^ 51", b"-44")
@istest
def test_neg_int(self):
self._test_expression("-(1 + 2)", b"-3")
@istest
def test_pos_int(self):
self._test_expression("+(1 + 2)", b"3")
self._test_expression("+(0 - 3)", b"-3")
@istest
def test_abs_int(self):
self._test_expression("abs(10)", b"10")
self._test_expression("abs(-12)", b"12")
@istest
def test_invert_int(self):
self._test_expression("~10", b"-11")
self._test_expression("~-10", b"9")
@istest
def test_getitem_list(self):
self._test_expression("[42, 53, 75][1]", b"53")
@istest
def test_settitem_list(self):
self._test_program_string("x = [1]\nx[0] = 2\nprint(x[0])", b"2\n")
@istest
def test_while(self):
self._test_program_string("x = 4\nwhile x: x = x - 1\nprint(x)", b"0\n")
@istest
def test_while_else(self):
program = """
#:: int -> none
def countdown(length):
index = length
while index:
print(index)
index = index - 1
else:
print("blastoff")
countdown(0)
countdown(1)
countdown(2)
"""
self._test_program_string(program, b"blastoff\n1\nblastoff\n2\n1\nblastoff\n")
@istest
def test_while_else_break(self):
program = """
#:: int -> none
def countdown(length):
index = length
while index:
print(index)
break
else:
print("blastoff")
countdown(0)
countdown(1)
countdown(2)
"""
self._test_program_string(program, b"blastoff\n1\n2\n")
@istest
def test_while_else_continue(self):
program = """
#:: int -> none
def countdown(length):
index = length
while index:
index = index - 1
continue
print(index)
else:
print("blastoff")
countdown(0)
countdown(1)
countdown(2)
"""
self._test_program_string(program, b"blastoff\nblastoff\nblastoff\n")
@istest
def test_for(self):
self._test_program_string("x = 0\nfor y in [1, 2, 3]:\n x = x + y\nprint(x)", b"6\n")
@istest
def test_for_else(self):
program = """
#:: int -> none
def countup(length):
for index in range(0, length):
print(index)
else:
print("blastoff")
countup(0)
countup(1)
countup(2)
"""
self._test_program_string(program, b"blastoff\n0\nblastoff\n0\n1\nblastoff\n")
@istest
def test_for_else_break(self):
program = """
#:: int -> none
def countup(length):
for index in range(0, length):
print(index)
break
else:
print("blastoff")
countup(0)
countup(1)
countup(2)
"""
self._test_program_string(program, b"blastoff\n0\n0\n")
@istest
def test_for_else_continue(self):
program = """
#:: int -> none
def countup(length):
for index in range(0, length):
continue
print(index)
else:
print("blastoff")
countup(0)
countup(1)
countup(2)
"""
self._test_program_string(program, b"blastoff\nblastoff\nblastoff\n")
@istest
def test_break_for(self):
self._test_program_string("y = 0\nfor x in [2, 3]:\n if y:\n break\n y = -x\nprint(y)", b"-2\n")
@istest
def test_continue_for(self):
self._test_program_string("y = 1\nfor x in [0, 3]:\n if x:\n pass\n else:\n continue\n y = y * x\nprint(y)", b"3\n")
@istest
def test_unhandled_exception(self):
result = self._run_program_string("raise Exception('Argh!')", allow_error=True)
assert_not_equal(0, result.return_code)
assert_equal(b"", result.output)
assert_in(b"Exception: Argh!", result.stderr_output)
@istest
def test_try_except_finally_with_no_exception(self):
program = """
try:
print("try")
except:
print("except")
finally:
print("finally")
print("done")
"""
result = self._run_program_string(program)
assert_equal(b"try\nfinally\ndone\n", result.output)
assert_equal(b"", result.stderr_output)
@istest
def test_try_except_finally_with_exception(self):
program = """
try:
print("try-before")
raise Exception("error")
print("try-after")
except:
print("except")
finally:
print("finally")
print("done")
"""
result = self._run_program_string(program)
assert_equal(b"try-before\nexcept\nfinally\ndone\n", result.output)
assert_equal(b"", result.stderr_output)
@istest
def test_try_named_except_finally_with_exception(self):
program = """
try:
print("try-before")
raise Exception("error")
print("try-after")
except Exception as error:
print(str(error))
finally:
print("finally")
print("done")
"""
result = self._run_program_string(program)
assert_equal(b"try-before\nerror\nfinally\ndone\n", result.output)
assert_equal(b"", result.stderr_output)
@istest
def test_try_except_with_exception_handles_subexception(self):
program = """
try:
print("try-before")
raise AssertionError("error")
print("try-after")
except Exception as error:
print(str(error))
"""
result = self._run_program_string(program)
assert_equal(b"try-before\nerror\n", result.output)
assert_equal(b"", result.stderr_output)
@istest
def test_try_except_with_exception_ignores_superexception(self):
program = """
try:
print("try-before")
raise Exception("error")
print("try-after")
except AssertionError as error:
print(str(error))
"""
result = self._run_program_string(program, allow_error=True)
assert_equal(b"try-before\n", result.output)
assert_in(b"Exception: error", result.stderr_output)
@istest
def test_first_matching_exception_handler_runs_first(self):
program = """
try:
raise AssertionError("error")
except AssertionError as error:
print("handling AssertionError")
except Exception as error:
print("handling Exception")
"""
result = self._run_program_string(program, allow_error=True)
assert_equal(b"handling AssertionError\n", result.output)
assert_equal(b"", result.stderr_output)
@istest
def test_assert_true_shows_no_output(self):
result = self._run_program_string("assert True, 'Argh!'")
assert_equal(b"", result.output)
assert_equal(b"", result.stderr_output)
@istest
def test_assert_false_with_message(self):
result = self._run_program_string("assert False, 'Argh!'", allow_error=True)
assert_not_equal(0, result.return_code)
assert_equal(b"", result.output)
assert_in(b"AssertionError: Argh!", result.stderr_output)
@istest
def test_assert_false_without_message(self):
result = self._run_program_string("assert False", allow_error=True)
assert_not_equal(0, result.return_code)
assert_equal(b"", result.output)
assert_in(b"AssertionError", result.stderr_output)
def _test_program_string(self, program, expected_output):
result = self._run_program_string(program)
assert_equal(expected_output, result.output)
assert_equal(b"", result.stderr_output)
def _run_program_string(self, program, allow_error=False):
with tempman.create_temp_dir() as temp_dir:
with open(os.path.join(temp_dir.path, "main.py"), "w") as main_file:
main_file.write("#!/usr/bin/env python\n")
main_file.write(program)
return self._run_program(path=temp_dir.path, program="main", allow_error=allow_error)
def _test_expression(self, expression, expected_output):
self._test_program_string("print({})\n".format(expression), expected_output + b"\n")
def _run_program(self, path, program, allow_error=False):
return testing.compile_and_run(self.platform, path, program, allow_error=allow_error)