7 from custom_result
import CustomResult
9 # A wrapper so we don't have to trap all exceptions when running statement.Execute
10 class ExecutionError(Exception):
11 def __init__(self
, type, value
, traceback
):
14 self
.traceback
= traceback
17 return "ExecutionError: " + str(self
.cause
)
19 class WarningResult(object):
20 def __init__(self
, message
):
21 self
.message
= message
24 def __init__(self
, text
, worksheet
, parent
= None):
26 self
.__worksheet
= worksheet
27 self
.result_scope
= None
30 # May raise SyntaxError
31 self
.__compiled
, self
.__mutated
= rewrite
.rewrite_and_compile(self
.__text
)
33 self
.set_parent(parent
)
35 def set_parent(self
, parent
):
36 self
.__parent
= parent
38 def get_result_scope(self
):
39 return self
.result_scope
41 def do_output(self
, *args
):
47 elif isinstance(args
[0], CustomResult
):
48 self
.results
.append(args
[0])
50 self
.results
.append(repr(args
[0]))
51 self
.result_scope
['_'] = args
[0]
53 self
.results
.append(repr(args
))
54 self
.result_scope
['_'] = args
56 def do_print(self
, *args
):
57 self
.results
.append(" ".join(map(str, args
)))
60 root_scope
= self
.__worksheet
.global_scope
62 scope
= copy
.copy(self
.__parent
.result_scope
)
64 scope
= copy
.copy(root_scope
)
67 self
.result_scope
= scope
69 for mutation
in self
.__mutated
:
70 if isinstance(mutation
, tuple):
71 variable
, method
= mutation
76 scope
[variable
] = copy
.copy(scope
[variable
])
78 self
.results
.append(WarningResult("Variable '%s' apparently modified, but can't copy it" % variable
))
80 root_scope
['__reinteract_statement'] = self
82 exec self
.__compiled
in scope
, scope
85 self
.result_scope
= None
86 type, value
, traceback
= sys
.exc_info()
87 raise ExecutionError(type, value
, traceback
)
89 root_scope
['__reinteract_statement'] = None
91 if __name__
=='__main__':
92 def expect(actual
,expected
):
93 if actual
!= expected
:
94 raise AssertionError("Got: '%s'; Expected: '%s'" % (actual
, expected
))
96 def expect_result(text
, result
):
99 expect(s
.results
[0], result
)
101 # A bare expression should give the repr of the expression
102 expect_result("'a'", repr('a'))
103 expect_result("1,2", repr((1,2)))
105 # Print, on the other hand, gives the string form of the expression
106 expect_result("print 'a'", 'a')
108 # Test that we copy a variable before mutating it (when we can detect
110 s1
= Statement("b = [0]")
112 s2
= Statement("b[0] = 1", parent
=s1
)
114 s3
= Statement("b[0]", parent
= s2
)
116 expect(s3
.results
[0], "1")
118 s2a
= Statement("b[0]", parent
=s1
)
120 expect(s2a
.results
[0], "0")