5
from pypy.interpreter.pycompiler import CPythonCompiler as CompilerClass
7
def codetest(self, source, functionname, args):
8
"""Compile and run the given code string, and then call its function
9
named by 'functionname' with arguments 'args'."""
10
from pypy.interpreter import baseobjspace
11
from pypy.interpreter import pyframe, gateway, module
14
source = str(py.code.Source(source).strip()) + '\n'
17
w_code = space.builtin.call('compile',
18
w(source), w('<string>'), w('exec'), w(0), w(0))
20
tempmodule = module.Module(space, w("__temp__"))
21
w_glob = tempmodule.w_dict
22
space.setitem(w_glob, w("__builtins__"), space.builtin)
24
code = space.unwrap(w_code)
25
code.exec_code(space, w_glob, w_glob)
27
wrappedargs = [w(a) for a in args]
28
wrappedfunc = space.getitem(w_glob, w(functionname))
30
w_output = space.call_function(wrappedfunc, *wrappedargs)
31
except baseobjspace.OperationError, e:
32
#e.print_detailed_traceback(space)
33
return '<<<%s>>>' % e.errorstr(space)
35
return space.unwrap(w_output)
37
def setup_method(self, arg):
38
ec = self.space.getexecutioncontext()
39
self.saved_compiler = ec.compiler
40
ec.compiler = self.CompilerClass(self.space)
42
def teardown_method(self, arg):
43
ec = self.space.getexecutioncontext()
44
ec.compiler = self.saved_compiler
46
def test_exception_trivial(self):
47
x = self.codetest('''\
57
def test_exception(self):
67
def test_finally(self):
77
assert self.codetest(code, 'f', [0]) == -12
78
assert self.codetest(code, 'f', [1]) == 1
80
## def test_raise(self):
81
## x = self.codetest('''
85
## self.assertEquals(x, '<<<TypeError: exceptions must be instances or subclasses of Exception or strings (deprecated), not int>>>')
87
def test_except2(self):
102
def test_except3(self):
108
except ZeroDivisionError, e:
109
z = "infinite result"
112
assert self.codetest(code, 'f', [2]) == 0
113
assert self.codetest(code, 'f', [0]) == "infinite result"
114
ess = "TypeError: unsupported operand type"
115
res = self.codetest(code, 'f', ['x'])
116
assert res.find(ess) >= 0
117
# the following (original) test was a bit too strict...:
118
# self.assertEquals(self.codetest(code, 'f', ['x']), "<<<TypeError: unsupported operand type(s) for //: 'int' and 'str'>>>")
120
def test_break(self):
132
assert self.codetest(code, 'f', [4]) == 1+2+3
133
assert self.codetest(code, 'f', [9]) == 1+2+3+4
135
def test_continue(self):
148
assert self.codetest(code, 'f', [4]) == 1+2+3+400
149
assert self.codetest(code, 'f', [9]) == (
152
def test_import(self):
153
# Regression test for a bug in PyFrame.IMPORT_NAME: when an
154
# import statement was executed in a function without a locals dict, a
155
# plain unwrapped None could be passed into space.call_function causing
156
# assertion errors later on.
157
real_call_function = self.space.call_function
158
def safe_call_function(w_obj, *arg_w):
160
assert arg is not None
161
return real_call_function(w_obj, *arg_w)
162
self.space.call_function = safe_call_function
167
self.codetest(code, 'f', [])
169
def test_extended_arg(self):
170
longexpr = 'x = x or ' + '-x' * 2500
184
x -= 1 # EXTENDED_ARG is for the JUMP_ABSOLUTE at the end of the loop
186
''' % ((longexpr,)*10)
187
assert self.codetest(code, 'f', [3]) == 0
189
def test_call_star_starstar(self):
218
assert self.codetest(code, 'f38', [117]) == [234]*19
220
def test_star_arg(self):
227
assert self.codetest(code, 'g', [12, ()]) == ()
228
assert self.codetest(code, 'g', [12, (3,4)]) == (3,4)
229
assert self.codetest(code, 'g', [12, []]) == ()
230
assert self.codetest(code, 'g', [12, [3,4]]) == (3,4)
231
assert self.codetest(code, 'g', [12, {}]) == ()
232
assert self.codetest(code, 'g', [12, {3:1}]) == (3,)
235
class TestPyPyInterpreter(TestInterpreter):
236
"""Runs the previous test with the pypy parser"""
237
from pypy.interpreter.pycompiler import PythonAstCompiler as CompilerClass
239
def test_extended_arg(self):
240
py.test.skip("expression too large for the recursive parser")
243
class AppTestInterpreter:
244
def test_trivial(self):
248
def test_trivial_call(self):
252
def test_trivial_call2(self):
253
def f(): return 1 + 1
256
def test_print(self):
262
def write(self, *args):
263
self.args.extend(args)
268
assert out.args == ['10','\n']
272
def test_identity(self):