Skip to content

Commit 0150deb

Browse files
committed
add pypy exec tests
1 parent 7ae9669 commit 0150deb

File tree

3 files changed

+356
-33
lines changed

3 files changed

+356
-33
lines changed
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
# The basis for this file before inclusion and extension here is
41+
# Copyright (c) 2017, The PyPy Project
42+
#
43+
# The MIT License
44+
# Permission is hereby granted, free of charge, to any person
45+
# obtaining a copy of this software and associated documentation
46+
# files (the "Software"), to deal in the Software without
47+
# restriction, including without limitation the rights to use,
48+
# copy, modify, merge, publish, distribute, sublicense, and/or
49+
# sell copies of the Software, and to permit persons to whom the
50+
# Software is furnished to do so, subject to the following conditions:
51+
#
52+
# The above copyright notice and this permission notice shall be included
53+
# in all copies or substantial portions of the Software.
54+
#
55+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
56+
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
57+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
58+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
59+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
60+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
61+
# DEALINGS IN THE SOFTWARE.
62+
63+
64+
def raises(exc, func, *args):
65+
try:
66+
func(*args)
67+
except exc:
68+
pass
69+
else:
70+
assert False
71+
72+
73+
class ExecTests:
74+
def test_string(self):
75+
g = {}
76+
l = {}
77+
exec("a = 3", g, l)
78+
assert l['a'] == 3
79+
80+
def test_localfill(self):
81+
g = {}
82+
exec("a = 3", g)
83+
assert g['a'] == 3
84+
85+
def test_builtinsupply(self):
86+
g = {}
87+
exec("pass", g)
88+
assert '__builtins__' in g
89+
90+
def test_invalidglobal(self):
91+
def f():
92+
exec('pass', 1)
93+
raises(TypeError, f)
94+
95+
def test_invalidlocal(self):
96+
def f():
97+
exec('pass', {}, 2)
98+
raises(TypeError, f)
99+
100+
def test_codeobject(self):
101+
co = compile("a = 3", '<string>', 'exec')
102+
g = {}
103+
l = {}
104+
exec(co, g, l)
105+
assert l['a'] == 3
106+
107+
def test_implicit(self):
108+
a = 4
109+
exec("a = 3")
110+
assert a == 4
111+
112+
def test_tuplelocals(self):
113+
g = {}
114+
l = {}
115+
exec("a = 3", g, l)
116+
assert l['a'] == 3
117+
118+
def test_tupleglobals(self):
119+
g = {}
120+
exec("a = 3", g)
121+
assert g['a'] == 3
122+
123+
def test_exceptionfallthrough(self):
124+
def f():
125+
exec('raise TypeError', {})
126+
raises(TypeError, f)
127+
128+
def test_global_stmt(self):
129+
g = {}
130+
l = {}
131+
co = compile("global a; a=5", '', 'exec')
132+
#import dis
133+
#dis.dis(co)
134+
exec(co, g, l)
135+
assert l == {}
136+
assert g['a'] == 5
137+
138+
def test_specialcase_free_load(self):
139+
exec("""if 1:
140+
def f():
141+
exec('a=3')
142+
return a
143+
raises(NameError, f)\n""")
144+
145+
def test_specialcase_free_load2(self):
146+
exec("""if 1:
147+
def f(a):
148+
exec('a=3')
149+
return a
150+
x = f(4)\n""")
151+
assert eval("x") == 4
152+
153+
def test_nested_names_are_not_confused(self):
154+
def get_nested_class():
155+
method_and_var = "var"
156+
class Test(object):
157+
def method_and_var(self):
158+
return "method"
159+
def test(self):
160+
return method_and_var
161+
def actual_global(self):
162+
return str("global")
163+
def str(self):
164+
return str(self)
165+
return Test()
166+
t = get_nested_class()
167+
assert t.actual_global() == "global"
168+
assert t.test() == 'var'
169+
assert t.method_and_var() == 'method'
170+
171+
def test_exec_load_name(self):
172+
d = {'x': 2}
173+
exec("""if 1:
174+
def f():
175+
save = x
176+
exec("x=3")
177+
return x,save
178+
\n""", d)
179+
res = d['f']()
180+
assert res == (2, 2)
181+
182+
def test_space_bug(self):
183+
d = {}
184+
exec("x=5 ", d)
185+
assert d['x'] == 5
186+
187+
def test_synerr(self):
188+
def x():
189+
exec("1 2")
190+
raises(SyntaxError, x)
191+
192+
def test_mapping_as_locals(self):
193+
class M(object):
194+
def __getitem__(self, key):
195+
return key
196+
def __setitem__(self, key, value):
197+
self.result[key] = value
198+
def setdefault(self, key, value):
199+
assert key == '__builtins__'
200+
m = M()
201+
m.result = {}
202+
exec("x=m", {}, m)
203+
assert m.result == {'x': 'm'}
204+
try:
205+
exec("y=n", m)
206+
except TypeError:
207+
pass
208+
else:
209+
assert False, 'Expected TypeError'
210+
raises(TypeError, eval, "m", m)
211+
212+
def test_filename(self):
213+
try:
214+
exec("'unmatched_quote")
215+
except SyntaxError as msg:
216+
assert msg.filename == '<string>', msg.filename
217+
try:
218+
eval("'unmatched_quote")
219+
except SyntaxError as msg:
220+
assert msg.filename == '<string>', msg.filename
221+
222+
def test_exec_and_name_lookups(self):
223+
ns = {}
224+
exec("""def f():
225+
exec('x=1', globals())
226+
return x\n""", ns)
227+
228+
f = ns['f']
229+
230+
try:
231+
res = f()
232+
except NameError as e: # keep py.test from exploding confused
233+
raise e
234+
235+
assert res == 1
236+
237+
def test_exec_unicode(self):
238+
# 's' is a bytes string
239+
s = b"x = '\xd0\xb9\xd1\x86\xd1\x83\xd0\xba\xd0\xb5\xd0\xbd'"
240+
# 'u' is a unicode
241+
u = s.decode('utf-8')
242+
ns = {}
243+
exec(u, ns)
244+
x = ns['x']
245+
assert len(x) == 6
246+
assert ord(x[0]) == 0x0439
247+
assert ord(x[1]) == 0x0446
248+
assert ord(x[2]) == 0x0443
249+
assert ord(x[3]) == 0x043a
250+
assert ord(x[4]) == 0x0435
251+
assert ord(x[5]) == 0x043d
252+
253+
def test_eval_unicode(self):
254+
u = "'%s'" % chr(0x1234)
255+
v = eval(u)
256+
assert v == chr(0x1234)
257+
258+
def test_compile_bytes(self):
259+
s = b"x = '\xd0\xb9\xd1\x86\xd1\x83\xd0\xba\xd0\xb5\xd0\xbd'"
260+
c = compile(s, '<input>', 'exec')
261+
ns = {}
262+
exec(c, ns)
263+
x = ns['x']
264+
assert len(x) == 6
265+
assert ord(x[0]) == 0x0439
266+
267+
def test_issue3297(self):
268+
c = compile("a, b = '\U0001010F', '\\U0001010F'", "dummy", "exec")
269+
d = {}
270+
exec(c, d)
271+
assert d['a'] == d['b']
272+
assert len(d['a']) == len(d['b'])
273+
assert d['a'] == d['b']

0 commit comments

Comments
 (0)