-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathbuiltin.py
184 lines (158 loc) · 3.44 KB
/
builtin.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
# Copyright 2018 The go-python Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
doc="abs"
assert abs(0) == 0
assert abs(10) == 10
assert abs(-10) == 10
doc="all"
assert all((0,0,0)) == False
assert all((1,1,0)) == False
assert all(["hello", "world"]) == True
assert all([]) == False
doc="any"
assert any((0,0,0)) == False
assert any((1,1,0)) == True
assert any(["hello", "world"]) == True
assert any([]) == False
doc="chr"
assert chr(65) == "A"
assert chr(163) == "£"
assert chr(0x263A) == "☺"
doc="compile"
code = compile("pass", "<string>", "exec")
assert code is not None
# FIXME
doc="dir"
def testDir():
a = 1
assert dir() == ["a"]
b = 2
assert dir() == ["a", "b"]
class A:
def method(self): pass
assert "method" in dir(A())
testDir()
try:
dir(1,2,3)
ok=False
except TypeError: ok=True
assert ok, "no exception raised"
doc="divmod"
assert divmod(34,7) == (4, 6)
doc="eval"
# smoke test only - see vm/tests/builtin.py for more tests
assert eval("1+2") == 3
doc="exec"
# smoke test only - see vm/tests/builtin.py for more tests
glob = {"a":100}
assert exec("b = a+100", glob) == None
assert glob["b"] == 200
doc="getattr"
class C:
def __init__(self):
self.potato = 42
c = C()
assert getattr(c, "potato") == 42
assert getattr(c, "potato", 43) == 42
assert getattr(c, "sausage", 43) == 43
doc="globals"
a = 1
assert globals()["a"] == 1
doc="hasattr"
assert hasattr(c, "potato")
assert not hasattr(c, "sausage")
doc="len"
assert len(()) == 0
assert len((1,2,3)) == 3
assert len("hello") == 5
assert len("£☺") == 2
doc="locals"
def fn(x):
assert locals()["x"] == 1
fn(1)
doc="next no default"
def gen():
yield 1
yield 2
g = gen()
assert next(g) == 1
assert next(g) == 2
ok = False
try:
next(g)
except StopIteration:
ok = True
assert ok, "StopIteration not raised"
doc="next with default"
g = gen()
assert next(g, 42) == 1
assert next(g, 42) == 2
assert next(g, 42) == 42
assert next(g, 42) == 42
doc="next no default with exception"
def gen2():
yield 1
raise ValueError("potato")
g = gen2()
assert next(g) == 1
ok = False
try:
next(g)
except ValueError:
ok = True
assert ok, "ValueError not raised"
doc="next with default and exception"
g = gen2()
assert next(g, 42) == 1
ok = False
try:
next(g)
except ValueError:
ok = True
assert ok, "ValueError not raised"
doc="ord"
assert 65 == ord("A")
assert 163 == ord("£")
assert 0x263A == ord("☺")
assert 65 == ord(b"A")
ok = False
try:
ord("AA")
except TypeError as e:
if e.args[0] != "ord() expected a character, but string of length 2 found":
raise
ok = True
assert ok, "TypeError not raised"
try:
ord(None)
except TypeError as e:
if e.args[0] != "ord() expected string of length 1, but NoneType found":
raise
ok = True
assert ok, "TypeError not raised"
doc="pow"
assert pow(2, 10) == 1024
assert pow(2, 10, 17) == 4
doc="repr"
assert repr(5) == "5"
assert repr("hello") == "'hello'"
doc="print"
# FIXME - need io redirection to test
#print("hello world")
#print(1,2,3,sep=",",end=",\n")
doc="round"
assert round(1.1) == 1.0
doc="setattr"
class C: pass
c = C()
assert not hasattr(c, "potato")
setattr(c, "potato", "spud")
assert getattr(c, "potato") == "spud"
assert c.potato == "spud"
doc="__import__"
lib = __import__("lib")
assert lib.libfn() == 42
assert lib.libvar == 43
assert lib.libclass().method() == 44
doc="finished"