-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathtest_builtin.py
More file actions
199 lines (167 loc) · 6.66 KB
/
Copy pathtest_builtin.py
File metadata and controls
199 lines (167 loc) · 6.66 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
# Copyright (c) 2018, 2026, Oracle and/or its affiliates.
# Copyright (C) 1996-2020 Python Software Foundation
#
# Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
import os
import subprocess
import sys
import tempfile
import unittest
import io
class MyIndexable(object):
def __init__(self, value):
self.value = value
def __index__(self):
return self.value
class BuiltinTest(unittest.TestCase):
def test_bin(self):
self.assertEqual(bin(0), '0b0')
self.assertEqual(bin(1), '0b1')
self.assertEqual(bin(-1), '-0b1')
self.assertEqual(bin(2**65), '0b1' + '0' * 65)
self.assertEqual(bin(2**65-1), '0b' + '1' * 65)
self.assertEqual(bin(-(2**65)), '-0b1' + '0' * 65)
self.assertEqual(bin(-(2**65-1)), '-0b' + '1' * 65)
# test of specializations
self.assertEqual(bin(MyIndexable(False)), '0b0')
self.assertEqual(bin(MyIndexable(True)), '0b1')
self.assertEqual(bin(MyIndexable(-(2**65))), '-0b1' + '0' * 65)
def test_GR11897(self):
globs = {}
code = 'a' + ' = ' + '8.01234567890123'
exec(code, globs)
self.assertEqual(globs['a'], 8.01234567890123)
def test_divmod_complex(self):
c1, c2 = complex(3, 2), complex(4,1)
self.assertRaises(TypeError, divmod, c1, c2)
self.assertRaises(TypeError, divmod, 10, c2)
self.assertRaises(TypeError, divmod, c1, 10)
def test_aiter_type_errors(self):
class BadAIter:
def __aiter__(self):
return object()
async def async_for_none():
async for _ in None:
pass
with self.assertRaisesRegex(TypeError, "'NoneType' object is not an async iterable"):
aiter(None)
with self.assertRaisesRegex(TypeError, "'int' object is not an async iterable"):
aiter(1)
with self.assertRaisesRegex(TypeError, r"aiter\(\) returned not an async iterator of type 'object'"):
aiter(BadAIter())
with self.assertRaisesRegex(TypeError, "'async for' requires an object with __aiter__ method, got NoneType"):
async_for_none().send(None)
def test_getitem_typeerror(self):
a = object()
try:
a[1]
except TypeError :
pass
else:
self.assertTrue(False)
def test_ascii(self):
self.assertEqual(ascii(1), "1")
self.assertEqual(ascii("錦蛇 \t \0 a \x03"), "'\\u9326\\u86c7 \\t \\x00 a \\x03'")
def test_ascii_preserves_str_subclass_for_ascii_repr(self):
class MyStr(str):
pass
class Foo:
def __repr__(self):
return MyStr("hello")
result = ascii(Foo())
self.assertEqual(result, "hello")
self.assertIs(type(result), MyStr)
def test_input(self):
import sys
class AlwaysLine:
def readline(self):
return 'line\n'
stdin = sys.stdin
try:
sys.stdin = AlwaysLine()
self.assertEqual(input(), 'line')
finally:
sys.stdin = stdin
def test_chr(self):
self.assertEqual(chr(32), ' ')
self.assertEqual(chr(97), 'a')
self.assertEqual(chr(0xfff), '\u0fff')
self.assertEqual(chr(0xf0000), '\U000f0000')
def test_ord(self):
self.assertEqual(ord(' '), 32)
self.assertEqual(ord('a'), 97)
self.assertEqual(ord('\u0fff'), 0xfff)
self.assertEqual(ord('\U000f0000'), 0xf0000)
def test_builtin_attr_write_raises(self):
def set_attr(obj):
obj.foo = 'bar'
self.assertRaises(TypeError, set_attr, object)
self.assertRaises(TypeError, set_attr, ValueError)
def test_builtin_constants(self):
import builtins
self.assertEqual(getattr(builtins, 'None'), None)
self.assertEqual(getattr(builtins, 'False'), False)
self.assertEqual(getattr(builtins, 'True'), True)
def test_missing_builtin_lookup_after_warmup(self):
def read_missing_builtin():
return definitely_missing_builtin_for_review
for _ in range(20):
with self.assertRaises(NameError):
read_missing_builtin()
def test_instance_attr_state_machine_after_warmup(self):
class PaddedFile:
def __init__(self, fileobj, prepend=b""):
self._buffer = prepend
self._length = len(prepend)
self.file = fileobj
self._read = 0
def read(self, size):
if self._read is None:
return self.file.read(size)
if self._read + size <= self._length:
read = self._read
self._read += size
return self._buffer[read:self._read]
read = self._read
self._read = None
return self._buffer[read:] + self.file.read(size - self._length + read)
def prepend(self, prepend=b""):
if self._read is None:
self._buffer = prepend
else:
self._read -= len(prepend)
return
self._length = len(self._buffer)
self._read = 0
def exercise():
padded = PaddedFile(io.BytesIO(b"cdef"), b"ab")
self.assertEqual(padded.read(1), b"a")
self.assertEqual(padded.read(3), b"bcd")
padded.prepend(b"Z")
self.assertEqual(padded.read(2), b"Ze")
self.assertEqual(padded.read(2), b"f")
for _ in range(20):
exercise()
def test_min(self):
self.assertEqual(min((), default=1, key="adsf"), 1)
def test_sort_keyfunc(self):
lists = [[], [1], [1,2], [1,2,3], [1,3,2], [3,2,1], [9,3,8,1,7,9,3,6,7,8]]
for l in lists:
count = 0
def keyfunc(v):
nonlocal count
count += 1
return v
result = sorted(l, key = keyfunc)
self.assertEqual(len(l), count)
self.assertEqual(sorted(l), result)
count = 0
result = sorted(l, key = keyfunc, reverse = True)
self.assertEqual(len(l), count)
self.assertEqual(sorted(l, reverse = True), result)
def test_license(self):
with tempfile.TemporaryDirectory() as tmpdir:
# Test that it can find the license even when ran outside of the distribution
license = subprocess.check_output([sys.executable, "-c", "print(license())"], cwd=tmpdir, text=True, input=("\n" * 100))
if sys.implementation.name == 'graalpy':
self.assertIn('Oracle', license)