File: apptest_exec.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (202 lines) | stat: -rw-r--r-- 4,493 bytes parent folder | download | duplicates (4)
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
"""Test the exec statement functionality.

New for PyPy - Could be incorporated into CPython regression tests.
"""
import pytest

def test_string():
    g = {}
    l = {}
    exec("a = 3", g, l)
    assert l['a'] == 3

def test_localfill():
    g = {}
    exec("a = 3", g)
    assert g['a'] == 3

def test_builtinsupply():
    g = {}
    exec("pass", g)
    assert '__builtins__' in g

def test_invalidglobal():
    with pytest.raises(TypeError):
        exec('pass', 1)

def test_invalidlocal():
    with pytest.raises(TypeError):
        exec('pass', {}, 2)

def test_codeobject():
    co = compile("a = 3", '<string>', 'exec')
    g = {}
    l = {}
    exec(co, g, l)
    assert l['a'] == 3

def test_implicit():
    a = 4
    exec("a = 3")
    assert a == 4

def test_tuplelocals():
    g = {}
    l = {}
    exec("a = 3", g, l)
    assert l['a'] == 3

def test_tupleglobals():
    g = {}
    exec("a = 3", g)
    assert g['a'] == 3

def test_exceptionfallthrough():
    with pytest.raises(TypeError):
        exec('raise TypeError', {})

def test_global_stmt():
    g = {}
    l = {}
    co = compile("global a; a=5", '', 'exec')
    #import dis
    #dis.dis(co)
    exec(co, g, l)
    assert l == {}
    assert g['a'] == 5

def test_specialcase_free_load():
    def f():
        exec('a=3')
        return a

    with raises(NameError):
        f()

def test_specialcase_free_load2():
    exec("""if 1:
        def f(a):
            exec('a=3')
            return a
        x = f(4)\n""")
    assert eval("x") == 4

def test_nested_names_are_not_confused():
    def get_nested_class():
        method_and_var = "var"
        class Test(object):
            def method_and_var(self):
                return "method"
            def test(self):
                return method_and_var
            def actual_global(self):
                return str("global")
            def str(self):
                return str(self)
        return Test()
    t = get_nested_class()
    assert t.actual_global() == "global"
    assert t.test() == 'var'
    assert t.method_and_var() == 'method'

def test_exec_load_name():
    d = {'x': 2}
    exec("""if 1:
        def f():
            save = x
            exec("x=3")
            return x,save
    \n""", d)
    res = d['f']()
    assert res == (2, 2)

def test_space_bug():
    d = {}
    exec("x=5 ", d)
    assert d['x'] == 5

def test_synerr():
    with pytest.raises(SyntaxError):
        exec("1 2")

def test_mapping_as_locals():
    class M(object):
        def __getitem__(self, key):
            return key
        def __setitem__(self, key, value):
            self.result[key] = value
        def setdefault(self, key, value):
            assert key == '__builtins__'
    m = M()
    m.result = {}
    exec("x=m", {}, m)
    assert m.result == {'x': 'm'}
    with raises(TypeError):
        exec("y=n", m)
    with raises(TypeError):
        eval("m", m)

def test_filename():
    with pytest.raises(SyntaxError) as excinfo:
        exec("'unmatched_quote")
    assert excinfo.value.filename == '<string>'
    with pytest.raises(SyntaxError) as excinfo:
        eval("'unmatched_quote")
    assert excinfo.value.filename == '<string>'

def test_exec_and_name_lookups():
    ns = {}
    exec("""def f():
        exec('x=1', globals())
        return x\n""", ns)

    f = ns['f']
    assert f() == 1

def test_exec_unicode():
    # 's' is a bytes string
    s = b"x = '\xd0\xb9\xd1\x86\xd1\x83\xd0\xba\xd0\xb5\xd0\xbd'"
    # 'u' is a unicode
    u = s.decode('utf-8')
    ns = {}
    exec(u, ns)
    x = ns['x']
    assert len(x) == 6
    assert ord(x[0]) == 0x0439
    assert ord(x[1]) == 0x0446
    assert ord(x[2]) == 0x0443
    assert ord(x[3]) == 0x043a
    assert ord(x[4]) == 0x0435
    assert ord(x[5]) == 0x043d

def test_eval_unicode():
    u = "'%s'" % chr(0x1234)
    v = eval(u)
    assert v == chr(0x1234)

def test_compile_bytes():
    s = b"x = '\xd0\xb9\xd1\x86\xd1\x83\xd0\xba\xd0\xb5\xd0\xbd'"
    c = compile(s, '<input>', 'exec')
    ns = {}
    exec(c, ns)
    x = ns['x']
    assert len(x) == 6
    assert ord(x[0]) == 0x0439

def test_issue3297():
    c = compile("a, b = '\U0001010F', '\\U0001010F'", "dummy", "exec")
    d = {}
    exec(c, d)
    assert d['a'] == d['b']
    assert len(d['a']) == len(d['b'])
    assert ascii(d['a']) == ascii(d['b'])

def test_exec_nonlocal():
    x = 0
    def set_x(value):
        nonlocal x
        x = value
    set_x(1)
    assert x == 1
    exec('set_x(2)')
    assert x == 2