Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork252
Expand file tree
/
Copy pathtest_simpleeval.py
More file actions
259 lines (196 loc) · 8.02 KB
/
test_simpleeval.py
File metadata and controls
259 lines (196 loc) · 8.02 KB
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
# -*- coding: utf-8 -*-
importast
importnumbers
frombpython.simpleevalimport (
simple_eval,
evaluate_current_expression,
EvaluationError,
safe_get_attribute,
safe_get_attribute_new_style,
)
frombpython.testimportunittest
frombpython._py3compatimportpy3
classTestSimpleEval(unittest.TestCase):
defassertMatchesStdlib(self,expr):
self.assertEqual(ast.literal_eval(expr),simple_eval(expr))
deftest_matches_stdlib(self):
"""Should match the stdlib literal_eval if no names or indexing"""
self.assertMatchesStdlib("[1]")
self.assertMatchesStdlib("{(1,): [2,3,{}]}")
deftest_indexing(self):
"""Literals can be indexed into"""
self.assertEqual(simple_eval("[1,2][0]"),1)
self.assertEqual(simple_eval("a", {"a":1}),1)
deftest_name_lookup(self):
"""Names can be lookup up in a namespace"""
self.assertEqual(simple_eval("a", {"a":1}),1)
self.assertEqual(simple_eval("map"),map)
self.assertEqual(simple_eval("a[b]", {"a": {"c":1},"b":"c"}),1)
deftest_allow_name_lookup(self):
"""Names can be lookup up in a namespace"""
self.assertEqual(simple_eval("a", {"a":1}),1)
deftest_lookup_on_suspicious_types(self):
classFakeDict(object):
pass
withself.assertRaises(ValueError):
simple_eval("a[1]", {"a":FakeDict()})
classTrickyDict(dict):
def__getitem__(self,index):
self.fail("doing key lookup isn't safe")
withself.assertRaises(ValueError):
simple_eval("a[1]", {"a":TrickyDict()})
classSchrodingersDict(dict):
def__getattribute__(inner_self,attr):
self.fail("doing attribute lookup might have side effects")
withself.assertRaises(ValueError):
simple_eval("a[1]", {"a":SchrodingersDict()})
classSchrodingersCatsDict(dict):
def__getattr__(inner_self,attr):
self.fail("doing attribute lookup might have side effects")
withself.assertRaises(ValueError):
simple_eval("a[1]", {"a":SchrodingersDict()})
deftest_operators_on_suspicious_types(self):
classSpam(numbers.Number):
def__add__(inner_self,other):
self.fail("doing attribute lookup might have side effects")
withself.assertRaises(ValueError):
simple_eval("a + 1", {"a":Spam()})
deftest_operators_on_numbers(self):
self.assertEqual(simple_eval("-2"),-2)
self.assertEqual(simple_eval("1 + 1"),2)
self.assertEqual(simple_eval("a - 2", {"a":1}),-1)
withself.assertRaises(ValueError):
simple_eval("2 * 3")
withself.assertRaises(ValueError):
simple_eval("2 ** 3")
deftest_function_calls_raise(self):
withself.assertRaises(ValueError):
simple_eval("1()")
deftest_nonexistant_names_raise(self):
withself.assertRaises(EvaluationError):
simple_eval("a")
deftest_attribute_access(self):
classFoo(object):
abc=1
self.assertEqual(simple_eval("foo.abc", {"foo":Foo()}),1)
classTestEvaluateCurrentExpression(unittest.TestCase):
defassertEvaled(self,line,value,ns=None):
assertline.count("|")==1
cursor_offset=line.find("|")
line=line.replace("|","")
self.assertEqual(
evaluate_current_expression(cursor_offset,line,ns),value
)
defassertCannotEval(self,line,ns=None):
assertline.count("|")==1
cursor_offset=line.find("|")
line=line.replace("|","")
withself.assertRaises(EvaluationError):
evaluate_current_expression(cursor_offset,line,ns)
deftest_simple(self):
self.assertEvaled("[1].a|bc", [1])
self.assertEvaled("[1].abc|", [1])
self.assertEvaled("[1].|abc", [1])
self.assertEvaled("[1]. |abc", [1])
self.assertEvaled("[1] .|abc", [1])
self.assertCannotEval("[1].abc |", [1])
self.assertCannotEval("[1]. abc |", [1])
self.assertCannotEval("[2][1].a|bc", [1])
deftest_nonsense(self):
self.assertEvaled("!@#$ [1].a|bc", [1])
self.assertEvaled("--- [2][0].a|bc",2)
self.assertCannotEval('"asdf".centered()[1].a|bc')
self.assertEvaled('"asdf"[1].a|bc',"s")
deftest_with_namespace(self):
self.assertEvaled("a[1].a|bc","d", {"a":"adsf"})
self.assertCannotEval("a[1].a|bc", {})
classA(object):
a="a"
classB(A):
b="b"
classProperty(object):
@property
defprop(self):
raiseAssertionError("Property __get__ executed")
classSlots(object):
__slots__= ["s1","s2","s3"]
ifnotpy3:
@property
defs3(self):
raiseAssertionError("Property __get__ executed")
classSlotsSubclass(Slots):
@property
defs4(self):
raiseAssertionError("Property __get__ executed")
classOverriddenGetattr(object):
def__getattr__(self,attr):
raiseAssertionError("custom __getattr__ executed")
a=1
classOverriddenGetattribute(object):
def__getattribute__(self,attr):
raiseAssertionError("custom __getattribute__ executed")
a=1
classOverriddenMRO(object):
def__mro__(self):
raiseAssertionError("custom mro executed")
a=1
member_descriptor=type(Slots.s1)
classTestSafeGetAttribute(unittest.TestCase):
deftest_lookup_on_object(self):
a=A()
a.x=1
self.assertEqual(safe_get_attribute_new_style(a,"x"),1)
self.assertEqual(safe_get_attribute_new_style(a,"a"),"a")
b=B()
b.y=2
self.assertEqual(safe_get_attribute_new_style(b,"y"),2)
self.assertEqual(safe_get_attribute_new_style(b,"a"),"a")
self.assertEqual(safe_get_attribute_new_style(b,"b"),"b")
deftest_avoid_running_properties(self):
p=Property()
self.assertEqual(safe_get_attribute_new_style(p,"prop"),Property.prop)
@unittest.skipIf(py3,"Old-style classes not in Python 3")
deftest_raises_on_old_style_class(self):
classOld:
pass
withself.assertRaises(ValueError):
safe_get_attribute_new_style(Old,"asdf")
deftest_lookup_with_slots(self):
s=Slots()
s.s1="s1"
self.assertEqual(safe_get_attribute(s,"s1"),"s1")
self.assertIsInstance(
safe_get_attribute_new_style(s,"s1"),member_descriptor
)
withself.assertRaises(AttributeError):
safe_get_attribute(s,"s2")
self.assertIsInstance(
safe_get_attribute_new_style(s,"s2"),member_descriptor
)
deftest_lookup_on_slots_classes(self):
sga=safe_get_attribute
s=SlotsSubclass()
self.assertIsInstance(sga(Slots,"s1"),member_descriptor)
self.assertIsInstance(sga(SlotsSubclass,"s1"),member_descriptor)
self.assertIsInstance(sga(SlotsSubclass,"s4"),property)
self.assertIsInstance(sga(s,"s4"),property)
@unittest.skipIf(py3,"Py 3 doesn't allow slots and prop in same class")
deftest_lookup_with_property_and_slots(self):
sga=safe_get_attribute
s=SlotsSubclass()
self.assertIsInstance(sga(Slots,"s3"),property)
self.assertEqual(safe_get_attribute(s,"s3"),Slots.__dict__["s3"])
self.assertIsInstance(sga(SlotsSubclass,"s3"),property)
deftest_lookup_on_overridden_methods(self):
sga=safe_get_attribute
self.assertEqual(sga(OverriddenGetattr(),"a"),1)
self.assertEqual(sga(OverriddenGetattribute(),"a"),1)
self.assertEqual(sga(OverriddenMRO(),"a"),1)
withself.assertRaises(AttributeError):
sga(OverriddenGetattr(),"b")
withself.assertRaises(AttributeError):
sga(OverriddenGetattribute(),"b")
withself.assertRaises(AttributeError):
sga(OverriddenMRO(),"b")
if__name__=="__main__":
unittest.main()