-
Notifications
You must be signed in to change notification settings - Fork 20
/
actual_python.py
260 lines (187 loc) · 6.14 KB
/
actual_python.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
# The code in this file is executed after being compiled as restricted code,
# and given a globals() dict with our idea of safe builtins, and the
# Zope production implementations of the special restricted-Python functions
# (like _getitem_ and _getiter_, etc).
#
# This isn't trying to provoke security problems, it's just trying to verify
# that Python code continues to work as intended after all the transformations,
# and with all the special wrappers we supply.
def f1():
iterator = iter(range(3))
assert next(iterator) == 0
assert next(iterator) == 1
assert next(iterator) == 2
try:
next(iterator)
except StopIteration:
pass
else: # pragma: no cover , Safety belt
assert 0, "expected StopIteration"
f1()
def f2():
assert list(map(lambda x: x + 1, range(3))) == list(range(1, 4))
f2()
def f3():
assert list(filter(None, range(10))) == list(range(1, 10))
f3()
def f4():
assert [i + 1 for i in range(3)] == list(range(*(1, 4)))
f4()
def f5():
def add(a, b):
return a + b
x = range(5)
result = reduce(add, x, 0) # noqa, We have this as guarded_reduce
assert sum(x) == result
f5()
def f6():
class C:
def display(self):
return str(self.value)
c1 = C()
c2 = C()
c1.value = 12
assert getattr(c1, 'value') == 12
assert c1.display() == '12'
assert not hasattr(c2, 'value')
setattr(c2, 'value', 34)
assert c2.value == 34
assert hasattr(c2, 'value')
del c2.value
assert not hasattr(c2, 'value')
# OK, if we can't set new attributes, at least verify that we can't.
# try:
# c1.value = 12
# except TypeError:
# pass
# else:
# assert 0, "expected direct attribute creation to fail"
# try:
# setattr(c1, 'value', 12)
# except TypeError:
# pass
# else:
# assert 0, "expected indirect attribute creation to fail"
assert getattr(C, "display", None) == getattr(C, "display")
delattr(C, "display")
# try:
# setattr(C, "display", lambda self: "replaced")
# except TypeError:
# pass
# else:
# assert 0, "expected setattr() attribute replacement to fail"
# try:
# delattr(C, "display")
# except TypeError:
# pass
# else:
# assert 0, "expected delattr() attribute deletion to fail"
f6()
def f7():
d = apply(dict, [((1, 2), (3, 4))]) # NOQA: F821 {1: 2, 3: 4}
methods = [('keys', 'k'),
('items', 'i'),
('values', 'v')]
expected = {'k': [1, 3],
'v': [2, 4],
'i': [(1, 2), (3, 4)]}
for meth, kind in methods:
access = getattr(d, meth)
result = sorted(access())
assert result == expected[kind], (meth, kind, result, expected[kind])
assert len(access()) == len(expected[kind]), (meth, kind, "len")
iter_ = access() # iterate twice on the same view
assert list(iter_) == list(iter_)
assert sorted([k for k in getattr(d, meth)()]) == expected[kind]
assert sorted(k for k in getattr(d, meth)()) == expected[kind]
assert {k: v for k, v in d.items()} == d
assert 1 in d
assert 1 in d.keys()
assert 2 in d.values()
assert (1, 2) in d.items()
assert d
assert d.keys()
assert d.values()
assert d.items()
empty_d = {}
assert not empty_d
assert not empty_d.keys()
assert not empty_d.values()
assert not empty_d.items()
smaller_d = {1: 2}
for m, _ in methods:
assert getattr(d, m)() != getattr(smaller_d, m)()
assert not getattr(d, m)() == getattr(smaller_d, m)()
if m != 'values':
assert getattr(d, m)() > getattr(smaller_d, m)()
assert getattr(d, m)() >= getattr(smaller_d, m)()
assert getattr(smaller_d, m)() < getattr(d, m)()
assert getattr(smaller_d, m)() <= getattr(d, m)()
f7()
def f8():
import math
ceil = getattr(math, 'ceil')
smallest = 1e100
smallest_index = None
largest = -1e100
largest_index = None
all = []
for i, x in enumerate((2.2, 1.1, 3.3, 5.5, 4.4)):
all.append(x)
effective = ceil(x)
if effective < smallest:
assert min(effective, smallest) == effective
smallest = effective
smallest_index = i
if effective > largest:
assert max(effective, largest) == effective
largest = effective
largest_index = i
assert smallest == 2
assert smallest_index == 1
assert largest == 6
assert largest_index == 3
assert min([ceil(x) for x in all]) == smallest
assert max(map(ceil, all)) == largest
f8()
# After all the above, these wrappers were still untouched:
# ['DateTime', '_print_', 'reorder', 'same_type', 'test']
# So do something to touch them.
def f9():
d = DateTime() # NOQA: F821
print(d) # this one provoked _print_
# Funky. This probably isn't an intended use of reorder, but I'm
# not sure why it exists.
assert reorder('edcbaxyz', 'abcdef', 'c') == zip('abde', 'abde') # NOQA
assert test(0, 'a', 0, 'b', 1, 'c', 0, 'd') == 'c' # NOQA: F821
assert test(0, 'a', 0, 'b', 0, 'c', 0, 'd', 'e') == 'e' # NOQA: F821
# Unclear that the next one is *intended* to return None (it falls off
# the end of test's implementation without explicitly returning anything).
assert test(0, 'a', 0, 'b', 0, 'c', 0, 'd') is None # NOQA: F821
assert same_type(3, 2, 1), 'expected same type' # NOQA: F821
assert not same_type(3, 2, 'a'), 'expected not same type' # NOQA: F821
return printed # NOQA: F821
f9()
def f10():
assert next(iter(enumerate(iter(iter(range(9)))))) == (0, 0)
f10()
def f11():
x = 1
x += 1
f11()
def f12():
assert all([True, True, True]) is True
assert all([True, False, True]) is False
f12()
def f13():
assert any([True, True, True]) is True
assert any([True, False, True]) is True
assert any([False, False, False]) is False
f13()
def f14():
"""provoke _unpack_sequence_"""
(a, (b, c)) = (1, (3, (4, 5)))
assert a == 1
assert b == 3
assert c == (4, 5)
f14()