| 1 |
"""Test the arraymodule. |
| 2 |
Roger E. Masse |
| 3 |
""" |
| 4 |
|
| 5 |
import collections.abc |
| 6 |
import unittest |
| 7 |
from test import support |
| 8 |
from test.support import import_helper |
| 9 |
from test.support import os_helper |
| 10 |
from test.support import _2G |
| 11 |
from test.support import subTests |
| 12 |
import weakref |
| 13 |
import pickle |
| 14 |
import operator |
| 15 |
import struct |
| 16 |
import sys |
| 17 |
import warnings |
| 18 |
|
| 19 |
import array |
| 20 |
from array import _array_reconstructor as array_reconstructor |
| 21 |
|
| 22 |
with warnings.catch_warnings(): |
| 23 |
warnings.simplefilter('ignore', DeprecationWarning) |
| 24 |
sizeof_wchar = array.array('u').itemsize |
| 25 |
|
| 26 |
|
| 27 |
class ArraySubclass(array.array): |
| 28 |
pass |
| 29 |
|
| 30 |
class ArraySubclassWithKwargs(array.array): |
| 31 |
def __init__(self, typecode, newarg=None): |
| 32 |
array.array.__init__(self) |
| 33 |
|
| 34 |
typecodes = 'uwbBhHiIlLfdqQ' |
| 35 |
|
| 36 |
class MiscTest(unittest.TestCase): |
| 37 |
|
| 38 |
def test_array_is_sequence(self): |
| 39 |
self.assertIsInstance(array.array("B"), collections.abc.MutableSequence) |
| 40 |
self.assertIsInstance(array.array("B"), collections.abc.Reversible) |
| 41 |
|
| 42 |
def test_bad_constructor(self): |
| 43 |
self.assertRaises(TypeError, array.array) |
| 44 |
self.assertRaises(TypeError, array.array, spam=42) |
| 45 |
self.assertRaises(TypeError, array.array, 'xx') |
| 46 |
self.assertRaises(ValueError, array.array, 'x') |
| 47 |
|
| 48 |
@support.cpython_only |
| 49 |
def test_disallow_instantiation(self): |
| 50 |
my_array = array.array("I") |
| 51 |
support.check_disallow_instantiation( |
| 52 |
self, type(iter(my_array)), my_array |
| 53 |
) |
| 54 |
|
| 55 |
@support.cpython_only |
| 56 |
def test_immutable(self): |
| 57 |
# bpo-43908: check that array.array is immutable |
| 58 |
with self.assertRaises(TypeError): |
| 59 |
array.array.foo = 1 |
| 60 |
|
| 61 |
def test_empty(self): |
| 62 |
# Exercise code for handling zero-length arrays |
| 63 |
a = array.array('B') |
| 64 |
a[:] = a |
| 65 |
self.assertEqual(len(a), 0) |
| 66 |
self.assertEqual(len(a + a), 0) |
| 67 |
self.assertEqual(len(a * 3), 0) |
| 68 |
a += a |
| 69 |
self.assertEqual(len(a), 0) |
| 70 |
|
| 71 |
|
| 72 |
# Machine format codes. |
| 73 |
# |
| 74 |
# Search for "enum machine_format_code" in Modules/arraymodule.c to get the |
| 75 |
# authoritative values. |
| 76 |
UNKNOWN_FORMAT = -1 |
| 77 |
UNSIGNED_INT8 = 0 |
| 78 |
SIGNED_INT8 = 1 |
| 79 |
UNSIGNED_INT16_LE = 2 |
| 80 |
UNSIGNED_INT16_BE = 3 |
| 81 |
SIGNED_INT16_LE = 4 |
| 82 |
SIGNED_INT16_BE = 5 |
| 83 |
UNSIGNED_INT32_LE = 6 |
| 84 |
UNSIGNED_INT32_BE = 7 |
| 85 |
SIGNED_INT32_LE = 8 |
| 86 |
SIGNED_INT32_BE = 9 |
| 87 |
UNSIGNED_INT64_LE = 10 |
| 88 |
UNSIGNED_INT64_BE = 11 |
| 89 |
SIGNED_INT64_LE = 12 |
| 90 |
SIGNED_INT64_BE = 13 |
| 91 |
IEEE_754_FLOAT_LE = 14 |
| 92 |
IEEE_754_FLOAT_BE = 15 |
| 93 |
IEEE_754_DOUBLE_LE = 16 |
| 94 |
IEEE_754_DOUBLE_BE = 17 |
| 95 |
UTF16_LE = 18 |
| 96 |
UTF16_BE = 19 |
| 97 |
UTF32_LE = 20 |
| 98 |
UTF32_BE = 21 |
| 99 |
|
| 100 |
|
| 101 |
class ArrayReconstructorTest(unittest.TestCase): |
| 102 |
|
| 103 |
def setUp(self): |
| 104 |
self.enterContext(warnings.catch_warnings()) |
| 105 |
warnings.filterwarnings( |
| 106 |
"ignore", |
| 107 |
message="The 'u' type code is deprecated and " |
| 108 |
"will be removed in Python 3.16", |
| 109 |
category=DeprecationWarning) |
| 110 |
|
| 111 |
def test_error(self): |
| 112 |
self.assertRaises(TypeError, array_reconstructor, |
| 113 |
"", "b", 0, b"") |
| 114 |
self.assertRaises(TypeError, array_reconstructor, |
| 115 |
str, "b", 0, b"") |
| 116 |
self.assertRaises(TypeError, array_reconstructor, |
| 117 |
array.array, "b", '', b"") |
| 118 |
self.assertRaises(TypeError, array_reconstructor, |
| 119 |
array.array, "b", 0, "") |
| 120 |
self.assertRaises(ValueError, array_reconstructor, |
| 121 |
array.array, "?", 0, b"") |
| 122 |
self.assertRaises(ValueError, array_reconstructor, |
| 123 |
array.array, "b", UNKNOWN_FORMAT, b"") |
| 124 |
self.assertRaises(ValueError, array_reconstructor, |
| 125 |
array.array, "b", 22, b"") |
| 126 |
self.assertRaises(ValueError, array_reconstructor, |
| 127 |
array.array, "d", 16, b"a") |
| 128 |
|
| 129 |
def test_numbers(self): |
| 130 |
testcases = ( |
| 131 |
(['B', 'H', 'I', 'L'], UNSIGNED_INT8, '=BBBB', |
| 132 |
[0x80, 0x7f, 0, 0xff]), |
| 133 |
(['b', 'h', 'i', 'l'], SIGNED_INT8, '=bbb', |
| 134 |
[-0x80, 0x7f, 0]), |
| 135 |
(['H', 'I', 'L'], UNSIGNED_INT16_LE, '<HHHH', |
| 136 |
[0x8000, 0x7fff, 0, 0xffff]), |
| 137 |
(['H', 'I', 'L'], UNSIGNED_INT16_BE, '>HHHH', |
| 138 |
[0x8000, 0x7fff, 0, 0xffff]), |
| 139 |
(['h', 'i', 'l'], SIGNED_INT16_LE, '<hhh', |
| 140 |
[-0x8000, 0x7fff, 0]), |
| 141 |
(['h', 'i', 'l'], SIGNED_INT16_BE, '>hhh', |
| 142 |
[-0x8000, 0x7fff, 0]), |
| 143 |
(['I', 'L'], UNSIGNED_INT32_LE, '<IIII', |
| 144 |
[1<<31, (1<<31)-1, 0, (1<<32)-1]), |
| 145 |
(['I', 'L'], UNSIGNED_INT32_BE, '>IIII', |
| 146 |
[1<<31, (1<<31)-1, 0, (1<<32)-1]), |
| 147 |
(['i', 'l'], SIGNED_INT32_LE, '<iii', |
| 148 |
[-1<<31, (1<<31)-1, 0]), |
| 149 |
(['i', 'l'], SIGNED_INT32_BE, '>iii', |
| 150 |
[-1<<31, (1<<31)-1, 0]), |
| 151 |
(['L'], UNSIGNED_INT64_LE, '<QQQQ', |
| 152 |
[1<<31, (1<<31)-1, 0, (1<<32)-1]), |
| 153 |
(['L'], UNSIGNED_INT64_BE, '>QQQQ', |
| 154 |
[1<<31, (1<<31)-1, 0, (1<<32)-1]), |
| 155 |
(['l'], SIGNED_INT64_LE, '<qqq', |
| 156 |
[-1<<31, (1<<31)-1, 0]), |
| 157 |
(['l'], SIGNED_INT64_BE, '>qqq', |
| 158 |
[-1<<31, (1<<31)-1, 0]), |
| 159 |
# The following tests for INT64 will raise an OverflowError |
| 160 |
# when run on a 32-bit machine. The tests are simply skipped |
| 161 |
# in that case. |
| 162 |
(['L'], UNSIGNED_INT64_LE, '<QQQQ', |
| 163 |
[1<<63, (1<<63)-1, 0, (1<<64)-1]), |
| 164 |
(['L'], UNSIGNED_INT64_BE, '>QQQQ', |
| 165 |
[1<<63, (1<<63)-1, 0, (1<<64)-1]), |
| 166 |
(['l'], SIGNED_INT64_LE, '<qqq', |
| 167 |
[-1<<63, (1<<63)-1, 0]), |
| 168 |
(['l'], SIGNED_INT64_BE, '>qqq', |
| 169 |
[-1<<63, (1<<63)-1, 0]), |
| 170 |
(['f'], IEEE_754_FLOAT_LE, '<ffff', |
| 171 |
[16711938.0, float('inf'), float('-inf'), -0.0]), |
| 172 |
(['f'], IEEE_754_FLOAT_BE, '>ffff', |
| 173 |
[16711938.0, float('inf'), float('-inf'), -0.0]), |
| 174 |
(['d'], IEEE_754_DOUBLE_LE, '<dddd', |
| 175 |
[9006104071832581.0, float('inf'), float('-inf'), -0.0]), |
| 176 |
(['d'], IEEE_754_DOUBLE_BE, '>dddd', |
| 177 |
[9006104071832581.0, float('inf'), float('-inf'), -0.0]) |
| 178 |
) |
| 179 |
for testcase in testcases: |
| 180 |
valid_typecodes, mformat_code, struct_fmt, values = testcase |
| 181 |
arraystr = struct.pack(struct_fmt, *values) |
| 182 |
for typecode in valid_typecodes: |
| 183 |
try: |
| 184 |
a = array.array(typecode, values) |
| 185 |
except OverflowError: |
| 186 |
continue # Skip this test case. |
| 187 |
b = array_reconstructor( |
| 188 |
array.array, typecode, mformat_code, arraystr) |
| 189 |
self.assertEqual(a, b, |
| 190 |
msg="{0!r} != {1!r}; testcase={2!r}".format(a, b, testcase)) |
| 191 |
|
| 192 |
def test_unicode(self): |
| 193 |
teststr = "Bonne Journ\xe9e \U0002030a\U00020347" |
| 194 |
testcases = ( |
| 195 |
(UTF16_LE, "UTF-16-LE"), |
| 196 |
(UTF16_BE, "UTF-16-BE"), |
| 197 |
(UTF32_LE, "UTF-32-LE"), |
| 198 |
(UTF32_BE, "UTF-32-BE") |
| 199 |
) |
| 200 |
for testcase in testcases: |
| 201 |
mformat_code, encoding = testcase |
| 202 |
for c in 'uw': |
| 203 |
a = array.array(c, teststr) |
| 204 |
b = array_reconstructor( |
| 205 |
array.array, c, mformat_code, teststr.encode(encoding)) |
| 206 |
self.assertEqual(a, b, |
| 207 |
msg="{0!r} != {1!r}; testcase={2!r}".format(a, b, testcase)) |
| 208 |
|
| 209 |
|
| 210 |
class BaseTest: |
| 211 |
# Required class attributes (provided by subclasses |
| 212 |
# typecode: the typecode to test |
| 213 |
# example: an initializer usable in the constructor for this type |
| 214 |
# smallerexample: the same length as example, but smaller |
| 215 |
# biggerexample: the same length as example, but bigger |
| 216 |
# outside: An entry that is not in example |
| 217 |
# minitemsize: the minimum guaranteed itemsize |
| 218 |
|
| 219 |
def setUp(self): |
| 220 |
self.enterContext(warnings.catch_warnings()) |
| 221 |
warnings.filterwarnings( |
| 222 |
"ignore", |
| 223 |
message="The 'u' type code is deprecated and " |
| 224 |
"will be removed in Python 3.16", |
| 225 |
category=DeprecationWarning) |
| 226 |
|
| 227 |
def assertEntryEqual(self, entry1, entry2): |
| 228 |
self.assertEqual(entry1, entry2) |
| 229 |
|
| 230 |
def badtypecode(self): |
| 231 |
# Return a typecode that is different from our own |
| 232 |
return typecodes[(typecodes.index(self.typecode)+1) % len(typecodes)] |
| 233 |
|
| 234 |
def test_constructor(self): |
| 235 |
a = array.array(self.typecode) |
| 236 |
self.assertEqual(a.typecode, self.typecode) |
| 237 |
self.assertGreaterEqual(a.itemsize, self.minitemsize) |
| 238 |
self.assertRaises(TypeError, array.array, self.typecode, None) |
| 239 |
|
| 240 |
def test_len(self): |
| 241 |
a = array.array(self.typecode) |
| 242 |
a.append(self.example[0]) |
| 243 |
self.assertEqual(len(a), 1) |
| 244 |
|
| 245 |
a = array.array(self.typecode, self.example) |
| 246 |
self.assertEqual(len(a), len(self.example)) |
| 247 |
|
| 248 |
def test_buffer_info(self): |
| 249 |
a = array.array(self.typecode, self.example) |
| 250 |
self.assertRaises(TypeError, a.buffer_info, 42) |
| 251 |
bi = a.buffer_info() |
| 252 |
self.assertIsInstance(bi, tuple) |
| 253 |
self.assertEqual(len(bi), 2) |
| 254 |
self.assertIsInstance(bi[0], int) |
| 255 |
self.assertIsInstance(bi[1], int) |
| 256 |
self.assertEqual(bi[1], len(a)) |
| 257 |
|
| 258 |
def test_byteswap(self): |
| 259 |
if self.typecode in ('u', 'w'): |
| 260 |
example = '\U00100100' |
| 261 |
else: |
| 262 |
example = self.example |
| 263 |
a = array.array(self.typecode, example) |
| 264 |
self.assertRaises(TypeError, a.byteswap, 42) |
| 265 |
if a.itemsize in (1, 2, 4, 8): |
| 266 |
b = array.array(self.typecode, example) |
| 267 |
b.byteswap() |
| 268 |
if a.itemsize==1: |
| 269 |
self.assertEqual(a, b) |
| 270 |
else: |
| 271 |
self.assertNotEqual(a, b) |
| 272 |
b.byteswap() |
| 273 |
self.assertEqual(a, b) |
| 274 |
|
| 275 |
def test_copy(self): |
| 276 |
import copy |
| 277 |
a = array.array(self.typecode, self.example) |
| 278 |
b = copy.copy(a) |
| 279 |
self.assertNotEqual(id(a), id(b)) |
| 280 |
self.assertEqual(a, b) |
| 281 |
|
| 282 |
def test_deepcopy(self): |
| 283 |
import copy |
| 284 |
a = array.array(self.typecode, self.example) |
| 285 |
b = copy.deepcopy(a) |
| 286 |
self.assertNotEqual(id(a), id(b)) |
| 287 |
self.assertEqual(a, b) |
| 288 |
|
| 289 |
def test_reduce_ex(self): |
| 290 |
a = array.array(self.typecode, self.example) |
| 291 |
for protocol in range(3): |
| 292 |
self.assertIs(a.__reduce_ex__(protocol)[0], array.array) |
| 293 |
for protocol in range(3, pickle.HIGHEST_PROTOCOL + 1): |
| 294 |
self.assertIs(a.__reduce_ex__(protocol)[0], array_reconstructor) |
| 295 |
|
| 296 |
def test_pickle(self): |
| 297 |
for protocol in range(pickle.HIGHEST_PROTOCOL + 1): |
| 298 |
a = array.array(self.typecode, self.example) |
| 299 |
b = pickle.loads(pickle.dumps(a, protocol)) |
| 300 |
self.assertNotEqual(id(a), id(b)) |
| 301 |
self.assertEqual(a, b) |
| 302 |
|
| 303 |
a = ArraySubclass(self.typecode, self.example) |
| 304 |
a.x = 10 |
| 305 |
b = pickle.loads(pickle.dumps(a, protocol)) |
| 306 |
self.assertNotEqual(id(a), id(b)) |
| 307 |
self.assertEqual(a, b) |
| 308 |
self.assertEqual(a.x, b.x) |
| 309 |
self.assertEqual(type(a), type(b)) |
| 310 |
|
| 311 |
def test_pickle_for_empty_array(self): |
| 312 |
for protocol in range(pickle.HIGHEST_PROTOCOL + 1): |
| 313 |
a = array.array(self.typecode) |
| 314 |
b = pickle.loads(pickle.dumps(a, protocol)) |
| 315 |
self.assertNotEqual(id(a), id(b)) |
| 316 |
self.assertEqual(a, b) |
| 317 |
|
| 318 |
a = ArraySubclass(self.typecode) |
| 319 |
a.x = 10 |
| 320 |
b = pickle.loads(pickle.dumps(a, protocol)) |
| 321 |
self.assertNotEqual(id(a), id(b)) |
| 322 |
self.assertEqual(a, b) |
| 323 |
self.assertEqual(a.x, b.x) |
| 324 |
self.assertEqual(type(a), type(b)) |
| 325 |
|
| 326 |
def test_iterator_pickle(self): |
| 327 |
orig = array.array(self.typecode, self.example) |
| 328 |
data = list(orig) |
| 329 |
data2 = data[::-1] |
| 330 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 331 |
# initial iterator |
| 332 |
itorig = iter(orig) |
| 333 |
d = pickle.dumps((itorig, orig), proto) |
| 334 |
it, a = pickle.loads(d) |
| 335 |
a.fromlist(data2) |
| 336 |
self.assertEqual(type(it), type(itorig)) |
| 337 |
self.assertEqual(list(it), data + data2) |
| 338 |
|
| 339 |
# running iterator |
| 340 |
next(itorig) |
| 341 |
d = pickle.dumps((itorig, orig), proto) |
| 342 |
it, a = pickle.loads(d) |
| 343 |
a.fromlist(data2) |
| 344 |
self.assertEqual(type(it), type(itorig)) |
| 345 |
self.assertEqual(list(it), data[1:] + data2) |
| 346 |
|
| 347 |
# empty iterator |
| 348 |
for i in range(1, len(data)): |
| 349 |
next(itorig) |
| 350 |
d = pickle.dumps((itorig, orig), proto) |
| 351 |
it, a = pickle.loads(d) |
| 352 |
a.fromlist(data2) |
| 353 |
self.assertEqual(type(it), type(itorig)) |
| 354 |
self.assertEqual(list(it), data2) |
| 355 |
|
| 356 |
# exhausted iterator |
| 357 |
self.assertRaises(StopIteration, next, itorig) |
| 358 |
d = pickle.dumps((itorig, orig), proto) |
| 359 |
it, a = pickle.loads(d) |
| 360 |
a.fromlist(data2) |
| 361 |
self.assertEqual(list(it), []) |
| 362 |
|
| 363 |
def test_exhausted_iterator(self): |
| 364 |
a = array.array(self.typecode, self.example) |
| 365 |
self.assertEqual(list(a), list(self.example)) |
| 366 |
exhit = iter(a) |
| 367 |
empit = iter(a) |
| 368 |
for x in exhit: # exhaust the iterator |
| 369 |
next(empit) # not exhausted |
| 370 |
a.append(self.outside) |
| 371 |
self.assertEqual(list(exhit), []) |
| 372 |
self.assertEqual(list(empit), [self.outside]) |
| 373 |
self.assertEqual(list(a), list(self.example) + [self.outside]) |
| 374 |
|
| 375 |
def test_reverse_iterator(self): |
| 376 |
a = array.array(self.typecode, self.example) |
| 377 |
self.assertEqual(list(a), list(self.example)) |
| 378 |
self.assertEqual(list(reversed(a)), list(iter(a))[::-1]) |
| 379 |
|
| 380 |
def test_reverse_iterator_picking(self): |
| 381 |
orig = array.array(self.typecode, self.example) |
| 382 |
data = list(orig) |
| 383 |
data2 = [self.outside] + data |
| 384 |
rev_data = data[len(data)-2::-1] + [self.outside] |
| 385 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 386 |
# initial iterator |
| 387 |
itorig = reversed(orig) |
| 388 |
d = pickle.dumps((itorig, orig), proto) |
| 389 |
it, a = pickle.loads(d) |
| 390 |
a.insert(0, self.outside) |
| 391 |
self.assertEqual(type(it), type(itorig)) |
| 392 |
self.assertEqual(list(it), rev_data) |
| 393 |
self.assertEqual(list(a), data2) |
| 394 |
|
| 395 |
# running iterator |
| 396 |
next(itorig) |
| 397 |
d = pickle.dumps((itorig, orig), proto) |
| 398 |
it, a = pickle.loads(d) |
| 399 |
a.insert(0, self.outside) |
| 400 |
self.assertEqual(type(it), type(itorig)) |
| 401 |
self.assertEqual(list(it), rev_data[1:]) |
| 402 |
self.assertEqual(list(a), data2) |
| 403 |
|
| 404 |
# empty iterator |
| 405 |
for i in range(1, len(data)): |
| 406 |
next(itorig) |
| 407 |
d = pickle.dumps((itorig, orig), proto) |
| 408 |
it, a = pickle.loads(d) |
| 409 |
a.insert(0, self.outside) |
| 410 |
self.assertEqual(type(it), type(itorig)) |
| 411 |
self.assertEqual(list(it), []) |
| 412 |
self.assertEqual(list(a), data2) |
| 413 |
|
| 414 |
# exhausted iterator |
| 415 |
self.assertRaises(StopIteration, next, itorig) |
| 416 |
d = pickle.dumps((itorig, orig), proto) |
| 417 |
it, a = pickle.loads(d) |
| 418 |
a.insert(0, self.outside) |
| 419 |
self.assertEqual(list(it), []) |
| 420 |
self.assertEqual(list(a), data2) |
| 421 |
|
| 422 |
def test_exhausted_reverse_iterator(self): |
| 423 |
a = array.array(self.typecode, self.example) |
| 424 |
self.assertEqual(list(a), list(self.example)) |
| 425 |
exhit = reversed(a) |
| 426 |
empit = reversed(a) |
| 427 |
for x in exhit: # exhaust the iterator |
| 428 |
next(empit) # Pointing past the 0th position. |
| 429 |
a.insert(0, self.outside) |
| 430 |
self.assertEqual(list(exhit), []) |
| 431 |
# The iterator index points past the 0th position so inserting |
| 432 |
# an element in the beginning does not make it appear. |
| 433 |
self.assertEqual(list(empit), []) |
| 434 |
self.assertEqual(list(a), [self.outside] + list(self.example)) |
| 435 |
|
| 436 |
def test_insert(self): |
| 437 |
a = array.array(self.typecode, self.example) |
| 438 |
a.insert(0, self.example[0]) |
| 439 |
self.assertEqual(len(a), 1+len(self.example)) |
| 440 |
self.assertEqual(a[0], a[1]) |
| 441 |
self.assertRaises(TypeError, a.insert) |
| 442 |
self.assertRaises(TypeError, a.insert, None) |
| 443 |
self.assertRaises(TypeError, a.insert, 0, None) |
| 444 |
|
| 445 |
a = array.array(self.typecode, self.example) |
| 446 |
a.insert(-1, self.example[0]) |
| 447 |
self.assertEqual( |
| 448 |
a, |
| 449 |
array.array( |
| 450 |
self.typecode, |
| 451 |
self.example[:-1] + self.example[:1] + self.example[-1:] |
| 452 |
) |
| 453 |
) |
| 454 |
|
| 455 |
a = array.array(self.typecode, self.example) |
| 456 |
a.insert(-1000, self.example[0]) |
| 457 |
self.assertEqual( |
| 458 |
a, |
| 459 |
array.array(self.typecode, self.example[:1] + self.example) |
| 460 |
) |
| 461 |
|
| 462 |
a = array.array(self.typecode, self.example) |
| 463 |
a.insert(1000, self.example[0]) |
| 464 |
self.assertEqual( |
| 465 |
a, |
| 466 |
array.array(self.typecode, self.example + self.example[:1]) |
| 467 |
) |
| 468 |
|
| 469 |
def test_tofromfile(self): |
| 470 |
a = array.array(self.typecode, 2*self.example) |
| 471 |
self.assertRaises(TypeError, a.tofile) |
| 472 |
os_helper.unlink(os_helper.TESTFN) |
| 473 |
f = open(os_helper.TESTFN, 'wb') |
| 474 |
try: |
| 475 |
a.tofile(f) |
| 476 |
f.close() |
| 477 |
b = array.array(self.typecode) |
| 478 |
f = open(os_helper.TESTFN, 'rb') |
| 479 |
self.assertRaises(TypeError, b.fromfile) |
| 480 |
b.fromfile(f, len(self.example)) |
| 481 |
self.assertEqual(b, array.array(self.typecode, self.example)) |
| 482 |
self.assertNotEqual(a, b) |
| 483 |
self.assertRaises(EOFError, b.fromfile, f, len(self.example)+1) |
| 484 |
self.assertEqual(a, b) |
| 485 |
f.close() |
| 486 |
finally: |
| 487 |
if not f.closed: |
| 488 |
f.close() |
| 489 |
os_helper.unlink(os_helper.TESTFN) |
| 490 |
|
| 491 |
def test_fromfile_ioerror(self): |
| 492 |
# Issue #5395: Check if fromfile raises a proper OSError |
| 493 |
# instead of EOFError. |
| 494 |
a = array.array(self.typecode) |
| 495 |
f = open(os_helper.TESTFN, 'wb') |
| 496 |
try: |
| 497 |
self.assertRaises(OSError, a.fromfile, f, len(self.example)) |
| 498 |
finally: |
| 499 |
f.close() |
| 500 |
os_helper.unlink(os_helper.TESTFN) |
| 501 |
|
| 502 |
def test_filewrite(self): |
| 503 |
a = array.array(self.typecode, 2*self.example) |
| 504 |
f = open(os_helper.TESTFN, 'wb') |
| 505 |
try: |
| 506 |
f.write(a) |
| 507 |
f.close() |
| 508 |
b = array.array(self.typecode) |
| 509 |
f = open(os_helper.TESTFN, 'rb') |
| 510 |
b.fromfile(f, len(self.example)) |
| 511 |
self.assertEqual(b, array.array(self.typecode, self.example)) |
| 512 |
self.assertNotEqual(a, b) |
| 513 |
b.fromfile(f, len(self.example)) |
| 514 |
self.assertEqual(a, b) |
| 515 |
f.close() |
| 516 |
finally: |
| 517 |
if not f.closed: |
| 518 |
f.close() |
| 519 |
os_helper.unlink(os_helper.TESTFN) |
| 520 |
|
| 521 |
def test_tofromlist(self): |
| 522 |
a = array.array(self.typecode, 2*self.example) |
| 523 |
b = array.array(self.typecode) |
| 524 |
self.assertRaises(TypeError, a.tolist, 42) |
| 525 |
self.assertRaises(TypeError, b.fromlist) |
| 526 |
self.assertRaises(TypeError, b.fromlist, 42) |
| 527 |
self.assertRaises(TypeError, b.fromlist, [None]) |
| 528 |
b.fromlist(a.tolist()) |
| 529 |
self.assertEqual(a, b) |
| 530 |
|
| 531 |
def test_tofrombytes(self): |
| 532 |
a = array.array(self.typecode, 2*self.example) |
| 533 |
b = array.array(self.typecode) |
| 534 |
self.assertRaises(TypeError, a.tobytes, 42) |
| 535 |
self.assertRaises(TypeError, b.frombytes) |
| 536 |
self.assertRaises(TypeError, b.frombytes, 42) |
| 537 |
b.frombytes(a.tobytes()) |
| 538 |
c = array.array(self.typecode, bytearray(a.tobytes())) |
| 539 |
self.assertEqual(a, b) |
| 540 |
self.assertEqual(a, c) |
| 541 |
if a.itemsize>1: |
| 542 |
self.assertRaises(ValueError, b.frombytes, b"x") |
| 543 |
|
| 544 |
def test_fromarray(self): |
| 545 |
a = array.array(self.typecode, self.example) |
| 546 |
b = array.array(self.typecode, a) |
| 547 |
self.assertEqual(a, b) |
| 548 |
|
| 549 |
def test_repr(self): |
| 550 |
a = array.array(self.typecode, 2*self.example) |
| 551 |
self.assertEqual(a, eval(repr(a), {"array": array.array})) |
| 552 |
|
| 553 |
a = array.array(self.typecode) |
| 554 |
self.assertEqual(repr(a), "array('%s')" % self.typecode) |
| 555 |
|
| 556 |
def test_str(self): |
| 557 |
a = array.array(self.typecode, 2*self.example) |
| 558 |
str(a) |
| 559 |
|
| 560 |
def test_cmp(self): |
| 561 |
a = array.array(self.typecode, self.example) |
| 562 |
self.assertIs(a == 42, False) |
| 563 |
self.assertIs(a != 42, True) |
| 564 |
|
| 565 |
self.assertIs(a == a, True) |
| 566 |
self.assertIs(a != a, False) |
| 567 |
self.assertIs(a < a, False) |
| 568 |
self.assertIs(a <= a, True) |
| 569 |
self.assertIs(a > a, False) |
| 570 |
self.assertIs(a >= a, True) |
| 571 |
|
| 572 |
al = array.array(self.typecode, self.smallerexample) |
| 573 |
ab = array.array(self.typecode, self.biggerexample) |
| 574 |
|
| 575 |
self.assertIs(a == 2*a, False) |
| 576 |
self.assertIs(a != 2*a, True) |
| 577 |
self.assertIs(a < 2*a, True) |
| 578 |
self.assertIs(a <= 2*a, True) |
| 579 |
self.assertIs(a > 2*a, False) |
| 580 |
self.assertIs(a >= 2*a, False) |
| 581 |
|
| 582 |
self.assertIs(a == al, False) |
| 583 |
self.assertIs(a != al, True) |
| 584 |
self.assertIs(a < al, False) |
| 585 |
self.assertIs(a <= al, False) |
| 586 |
self.assertIs(a > al, True) |
| 587 |
self.assertIs(a >= al, True) |
| 588 |
|
| 589 |
self.assertIs(a == ab, False) |
| 590 |
self.assertIs(a != ab, True) |
| 591 |
self.assertIs(a < ab, True) |
| 592 |
self.assertIs(a <= ab, True) |
| 593 |
self.assertIs(a > ab, False) |
| 594 |
self.assertIs(a >= ab, False) |
| 595 |
|
| 596 |
def test_add(self): |
| 597 |
a = array.array(self.typecode, self.example) \ |
| 598 |
+ array.array(self.typecode, self.example[::-1]) |
| 599 |
self.assertEqual( |
| 600 |
a, |
| 601 |
array.array(self.typecode, self.example + self.example[::-1]) |
| 602 |
) |
| 603 |
|
| 604 |
b = array.array(self.badtypecode()) |
| 605 |
self.assertRaises(TypeError, a.__add__, b) |
| 606 |
|
| 607 |
self.assertRaises(TypeError, a.__add__, "bad") |
| 608 |
|
| 609 |
def test_iadd(self): |
| 610 |
a = array.array(self.typecode, self.example[::-1]) |
| 611 |
b = a |
| 612 |
a += array.array(self.typecode, 2*self.example) |
| 613 |
self.assertIs(a, b) |
| 614 |
self.assertEqual( |
| 615 |
a, |
| 616 |
array.array(self.typecode, self.example[::-1]+2*self.example) |
| 617 |
) |
| 618 |
a = array.array(self.typecode, self.example) |
| 619 |
a += a |
| 620 |
self.assertEqual( |
| 621 |
a, |
| 622 |
array.array(self.typecode, self.example + self.example) |
| 623 |
) |
| 624 |
|
| 625 |
b = array.array(self.badtypecode()) |
| 626 |
self.assertRaises(TypeError, a.__add__, b) |
| 627 |
|
| 628 |
self.assertRaises(TypeError, a.__iadd__, "bad") |
| 629 |
|
| 630 |
def test_mul(self): |
| 631 |
a = 5*array.array(self.typecode, self.example) |
| 632 |
self.assertEqual( |
| 633 |
a, |
| 634 |
array.array(self.typecode, 5*self.example) |
| 635 |
) |
| 636 |
|
| 637 |
a = array.array(self.typecode, self.example)*5 |
| 638 |
self.assertEqual( |
| 639 |
a, |
| 640 |
array.array(self.typecode, self.example*5) |
| 641 |
) |
| 642 |
|
| 643 |
a = 0*array.array(self.typecode, self.example) |
| 644 |
self.assertEqual( |
| 645 |
a, |
| 646 |
array.array(self.typecode) |
| 647 |
) |
| 648 |
|
| 649 |
a = (-1)*array.array(self.typecode, self.example) |
| 650 |
self.assertEqual( |
| 651 |
a, |
| 652 |
array.array(self.typecode) |
| 653 |
) |
| 654 |
|
| 655 |
a = 5 * array.array(self.typecode, self.example[:1]) |
| 656 |
self.assertEqual( |
| 657 |
a, |
| 658 |
array.array(self.typecode, [a[0]] * 5) |
| 659 |
) |
| 660 |
|
| 661 |
self.assertRaises(TypeError, a.__mul__, "bad") |
| 662 |
|
| 663 |
def test_imul(self): |
| 664 |
a = array.array(self.typecode, self.example) |
| 665 |
b = a |
| 666 |
|
| 667 |
a *= 5 |
| 668 |
self.assertIs(a, b) |
| 669 |
self.assertEqual( |
| 670 |
a, |
| 671 |
array.array(self.typecode, 5*self.example) |
| 672 |
) |
| 673 |
|
| 674 |
a *= 0 |
| 675 |
self.assertIs(a, b) |
| 676 |
self.assertEqual(a, array.array(self.typecode)) |
| 677 |
|
| 678 |
a *= 1000 |
| 679 |
self.assertIs(a, b) |
| 680 |
self.assertEqual(a, array.array(self.typecode)) |
| 681 |
|
| 682 |
a *= -1 |
| 683 |
self.assertIs(a, b) |
| 684 |
self.assertEqual(a, array.array(self.typecode)) |
| 685 |
|
| 686 |
a = array.array(self.typecode, self.example) |
| 687 |
a *= -1 |
| 688 |
self.assertEqual(a, array.array(self.typecode)) |
| 689 |
|
| 690 |
self.assertRaises(TypeError, a.__imul__, "bad") |
| 691 |
|
| 692 |
def test_getitem(self): |
| 693 |
a = array.array(self.typecode, self.example) |
| 694 |
self.assertEntryEqual(a[0], self.example[0]) |
| 695 |
self.assertEntryEqual(a[0], self.example[0]) |
| 696 |
self.assertEntryEqual(a[-1], self.example[-1]) |
| 697 |
self.assertEntryEqual(a[-1], self.example[-1]) |
| 698 |
self.assertEntryEqual(a[len(self.example)-1], self.example[-1]) |
| 699 |
self.assertEntryEqual(a[-len(self.example)], self.example[0]) |
| 700 |
self.assertRaises(TypeError, a.__getitem__) |
| 701 |
self.assertRaises(IndexError, a.__getitem__, len(self.example)) |
| 702 |
self.assertRaises(IndexError, a.__getitem__, -len(self.example)-1) |
| 703 |
|
| 704 |
def test_setitem(self): |
| 705 |
a = array.array(self.typecode, self.example) |
| 706 |
a[0] = a[-1] |
| 707 |
self.assertEntryEqual(a[0], a[-1]) |
| 708 |
|
| 709 |
a = array.array(self.typecode, self.example) |
| 710 |
a[0] = a[-1] |
| 711 |
self.assertEntryEqual(a[0], a[-1]) |
| 712 |
|
| 713 |
a = array.array(self.typecode, self.example) |
| 714 |
a[-1] = a[0] |
| 715 |
self.assertEntryEqual(a[0], a[-1]) |
| 716 |
|
| 717 |
a = array.array(self.typecode, self.example) |
| 718 |
a[-1] = a[0] |
| 719 |
self.assertEntryEqual(a[0], a[-1]) |
| 720 |
|
| 721 |
a = array.array(self.typecode, self.example) |
| 722 |
a[len(self.example)-1] = a[0] |
| 723 |
self.assertEntryEqual(a[0], a[-1]) |
| 724 |
|
| 725 |
a = array.array(self.typecode, self.example) |
| 726 |
a[-len(self.example)] = a[-1] |
| 727 |
self.assertEntryEqual(a[0], a[-1]) |
| 728 |
|
| 729 |
self.assertRaises(TypeError, a.__setitem__) |
| 730 |
self.assertRaises(TypeError, a.__setitem__, None) |
| 731 |
self.assertRaises(TypeError, a.__setitem__, 0, None) |
| 732 |
self.assertRaises( |
| 733 |
IndexError, |
| 734 |
a.__setitem__, |
| 735 |
len(self.example), self.example[0] |
| 736 |
) |
| 737 |
self.assertRaises( |
| 738 |
IndexError, |
| 739 |
a.__setitem__, |
| 740 |
-len(self.example)-1, self.example[0] |
| 741 |
) |
| 742 |
|
| 743 |
def test_delitem(self): |
| 744 |
a = array.array(self.typecode, self.example) |
| 745 |
del a[0] |
| 746 |
self.assertEqual( |
| 747 |
a, |
| 748 |
array.array(self.typecode, self.example[1:]) |
| 749 |
) |
| 750 |
|
| 751 |
a = array.array(self.typecode, self.example) |
| 752 |
del a[-1] |
| 753 |
self.assertEqual( |
| 754 |
a, |
| 755 |
array.array(self.typecode, self.example[:-1]) |
| 756 |
) |
| 757 |
|
| 758 |
a = array.array(self.typecode, self.example) |
| 759 |
del a[len(self.example)-1] |
| 760 |
self.assertEqual( |
| 761 |
a, |
| 762 |
array.array(self.typecode, self.example[:-1]) |
| 763 |
) |
| 764 |
|
| 765 |
a = array.array(self.typecode, self.example) |
| 766 |
del a[-len(self.example)] |
| 767 |
self.assertEqual( |
| 768 |
a, |
| 769 |
array.array(self.typecode, self.example[1:]) |
| 770 |
) |
| 771 |
|
| 772 |
self.assertRaises(TypeError, a.__delitem__) |
| 773 |
self.assertRaises(TypeError, a.__delitem__, None) |
| 774 |
self.assertRaises(IndexError, a.__delitem__, len(self.example)) |
| 775 |
self.assertRaises(IndexError, a.__delitem__, -len(self.example)-1) |
| 776 |
|
| 777 |
def test_getslice(self): |
| 778 |
a = array.array(self.typecode, self.example) |
| 779 |
self.assertEqual(a[:], a) |
| 780 |
|
| 781 |
self.assertEqual( |
| 782 |
a[1:], |
| 783 |
array.array(self.typecode, self.example[1:]) |
| 784 |
) |
| 785 |
|
| 786 |
self.assertEqual( |
| 787 |
a[:1], |
| 788 |
array.array(self.typecode, self.example[:1]) |
| 789 |
) |
| 790 |
|
| 791 |
self.assertEqual( |
| 792 |
a[:-1], |
| 793 |
array.array(self.typecode, self.example[:-1]) |
| 794 |
) |
| 795 |
|
| 796 |
self.assertEqual( |
| 797 |
a[-1:], |
| 798 |
array.array(self.typecode, self.example[-1:]) |
| 799 |
) |
| 800 |
|
| 801 |
self.assertEqual( |
| 802 |
a[-1:-1], |
| 803 |
array.array(self.typecode) |
| 804 |
) |
| 805 |
|
| 806 |
self.assertEqual( |
| 807 |
a[2:1], |
| 808 |
array.array(self.typecode) |
| 809 |
) |
| 810 |
|
| 811 |
self.assertEqual( |
| 812 |
a[1000:], |
| 813 |
array.array(self.typecode) |
| 814 |
) |
| 815 |
self.assertEqual(a[-1000:], a) |
| 816 |
self.assertEqual(a[:1000], a) |
| 817 |
self.assertEqual( |
| 818 |
a[:-1000], |
| 819 |
array.array(self.typecode) |
| 820 |
) |
| 821 |
self.assertEqual(a[-1000:1000], a) |
| 822 |
self.assertEqual( |
| 823 |
a[2000:1000], |
| 824 |
array.array(self.typecode) |
| 825 |
) |
| 826 |
|
| 827 |
def test_extended_getslice(self): |
| 828 |
# Test extended slicing by comparing with list slicing |
| 829 |
# (Assumes list conversion works correctly, too) |
| 830 |
a = array.array(self.typecode, self.example) |
| 831 |
indices = (0, None, 1, 3, 19, 100, sys.maxsize, -1, -2, -31, -100) |
| 832 |
for start in indices: |
| 833 |
for stop in indices: |
| 834 |
# Everything except the initial 0 (invalid step) |
| 835 |
for step in indices[1:]: |
| 836 |
self.assertEqual(list(a[start:stop:step]), |
| 837 |
list(a)[start:stop:step]) |
| 838 |
|
| 839 |
def test_setslice(self): |
| 840 |
a = array.array(self.typecode, self.example) |
| 841 |
a[:1] = a |
| 842 |
self.assertEqual( |
| 843 |
a, |
| 844 |
array.array(self.typecode, self.example + self.example[1:]) |
| 845 |
) |
| 846 |
|
| 847 |
a = array.array(self.typecode, self.example) |
| 848 |
a[:-1] = a |
| 849 |
self.assertEqual( |
| 850 |
a, |
| 851 |
array.array(self.typecode, self.example + self.example[-1:]) |
| 852 |
) |
| 853 |
|
| 854 |
a = array.array(self.typecode, self.example) |
| 855 |
a[-1:] = a |
| 856 |
self.assertEqual( |
| 857 |
a, |
| 858 |
array.array(self.typecode, self.example[:-1] + self.example) |
| 859 |
) |
| 860 |
|
| 861 |
a = array.array(self.typecode, self.example) |
| 862 |
a[1:] = a |
| 863 |
self.assertEqual( |
| 864 |
a, |
| 865 |
array.array(self.typecode, self.example[:1] + self.example) |
| 866 |
) |
| 867 |
|
| 868 |
a = array.array(self.typecode, self.example) |
| 869 |
a[1:-1] = a |
| 870 |
self.assertEqual( |
| 871 |
a, |
| 872 |
array.array( |
| 873 |
self.typecode, |
| 874 |
self.example[:1] + self.example + self.example[-1:] |
| 875 |
) |
| 876 |
) |
| 877 |
|
| 878 |
a = array.array(self.typecode, self.example) |
| 879 |
a[1000:] = a |
| 880 |
self.assertEqual( |
| 881 |
a, |
| 882 |
array.array(self.typecode, 2*self.example) |
| 883 |
) |
| 884 |
|
| 885 |
a = array.array(self.typecode, self.example) |
| 886 |
a[-1000:] = a |
| 887 |
self.assertEqual( |
| 888 |
a, |
| 889 |
array.array(self.typecode, self.example) |
| 890 |
) |
| 891 |
|
| 892 |
a = array.array(self.typecode, self.example) |
| 893 |
a[:1000] = a |
| 894 |
self.assertEqual( |
| 895 |
a, |
| 896 |
array.array(self.typecode, self.example) |
| 897 |
) |
| 898 |
|
| 899 |
a = array.array(self.typecode, self.example) |
| 900 |
a[:-1000] = a |
| 901 |
self.assertEqual( |
| 902 |
a, |
| 903 |
array.array(self.typecode, 2*self.example) |
| 904 |
) |
| 905 |
|
| 906 |
a = array.array(self.typecode, self.example) |
| 907 |
a[1:0] = a |
| 908 |
self.assertEqual( |
| 909 |
a, |
| 910 |
array.array(self.typecode, self.example[:1] + self.example + self.example[1:]) |
| 911 |
) |
| 912 |
|
| 913 |
a = array.array(self.typecode, self.example) |
| 914 |
a[2000:1000] = a |
| 915 |
self.assertEqual( |
| 916 |
a, |
| 917 |
array.array(self.typecode, 2*self.example) |
| 918 |
) |
| 919 |
|
| 920 |
a = array.array(self.typecode, self.example) |
| 921 |
self.assertRaises(TypeError, a.__setitem__, slice(0, 0), None) |
| 922 |
self.assertRaises(TypeError, a.__setitem__, slice(0, 1), None) |
| 923 |
|
| 924 |
b = array.array(self.badtypecode()) |
| 925 |
self.assertRaises(TypeError, a.__setitem__, slice(0, 0), b) |
| 926 |
self.assertRaises(TypeError, a.__setitem__, slice(0, 1), b) |
| 927 |
|
| 928 |
def test_extended_set_del_slice(self): |
| 929 |
indices = (0, None, 1, 3, 19, 100, sys.maxsize, -1, -2, -31, -100) |
| 930 |
for start in indices: |
| 931 |
for stop in indices: |
| 932 |
# Everything except the initial 0 (invalid step) |
| 933 |
for step in indices[1:]: |
| 934 |
a = array.array(self.typecode, self.example) |
| 935 |
L = list(a) |
| 936 |
# Make sure we have a slice of exactly the right length, |
| 937 |
# but with (hopefully) different data. |
| 938 |
data = L[start:stop:step] |
| 939 |
data.reverse() |
| 940 |
L[start:stop:step] = data |
| 941 |
a[start:stop:step] = array.array(self.typecode, data) |
| 942 |
self.assertEqual(a, array.array(self.typecode, L)) |
| 943 |
|
| 944 |
del L[start:stop:step] |
| 945 |
del a[start:stop:step] |
| 946 |
self.assertEqual(a, array.array(self.typecode, L)) |
| 947 |
|
| 948 |
def test_index(self): |
| 949 |
example = 2*self.example |
| 950 |
a = array.array(self.typecode, example) |
| 951 |
self.assertRaises(TypeError, a.index) |
| 952 |
for x in example: |
| 953 |
self.assertEqual(a.index(x), example.index(x)) |
| 954 |
self.assertRaises(ValueError, a.index, None) |
| 955 |
self.assertRaises(ValueError, a.index, self.outside) |
| 956 |
|
| 957 |
a = array.array('i', [-2, -1, 0, 0, 1, 2]) |
| 958 |
self.assertEqual(a.index(0), 2) |
| 959 |
self.assertEqual(a.index(0, 2), 2) |
| 960 |
self.assertEqual(a.index(0, -4), 2) |
| 961 |
self.assertEqual(a.index(-2, -10), 0) |
| 962 |
self.assertEqual(a.index(0, 3), 3) |
| 963 |
self.assertEqual(a.index(0, -3), 3) |
| 964 |
self.assertEqual(a.index(0, 3, 4), 3) |
| 965 |
self.assertEqual(a.index(0, -3, -2), 3) |
| 966 |
self.assertRaises(ValueError, a.index, 2, 0, -10) |
| 967 |
|
| 968 |
def test_count(self): |
| 969 |
example = 2*self.example |
| 970 |
a = array.array(self.typecode, example) |
| 971 |
self.assertRaises(TypeError, a.count) |
| 972 |
for x in example: |
| 973 |
self.assertEqual(a.count(x), example.count(x)) |
| 974 |
self.assertEqual(a.count(self.outside), 0) |
| 975 |
self.assertEqual(a.count(None), 0) |
| 976 |
|
| 977 |
def test_remove(self): |
| 978 |
for x in self.example: |
| 979 |
example = 2*self.example |
| 980 |
a = array.array(self.typecode, example) |
| 981 |
pos = example.index(x) |
| 982 |
example2 = example[:pos] + example[pos+1:] |
| 983 |
a.remove(x) |
| 984 |
self.assertEqual(a, array.array(self.typecode, example2)) |
| 985 |
|
| 986 |
a = array.array(self.typecode, self.example) |
| 987 |
self.assertRaises(ValueError, a.remove, self.outside) |
| 988 |
|
| 989 |
self.assertRaises(ValueError, a.remove, None) |
| 990 |
|
| 991 |
def test_pop(self): |
| 992 |
a = array.array(self.typecode) |
| 993 |
self.assertRaises(IndexError, a.pop) |
| 994 |
|
| 995 |
a = array.array(self.typecode, 2*self.example) |
| 996 |
self.assertRaises(TypeError, a.pop, 42, 42) |
| 997 |
self.assertRaises(TypeError, a.pop, None) |
| 998 |
self.assertRaises(IndexError, a.pop, len(a)) |
| 999 |
self.assertRaises(IndexError, a.pop, -len(a)-1) |
| 1000 |
|
| 1001 |
self.assertEntryEqual(a.pop(0), self.example[0]) |
| 1002 |
self.assertEqual( |
| 1003 |
a, |
| 1004 |
array.array(self.typecode, self.example[1:]+self.example) |
| 1005 |
) |
| 1006 |
self.assertEntryEqual(a.pop(1), self.example[2]) |
| 1007 |
self.assertEqual( |
| 1008 |
a, |
| 1009 |
array.array(self.typecode, self.example[1:2]+self.example[3:]+self.example) |
| 1010 |
) |
| 1011 |
self.assertEntryEqual(a.pop(0), self.example[1]) |
| 1012 |
self.assertEntryEqual(a.pop(), self.example[-1]) |
| 1013 |
self.assertEqual( |
| 1014 |
a, |
| 1015 |
array.array(self.typecode, self.example[3:]+self.example[:-1]) |
| 1016 |
) |
| 1017 |
|
| 1018 |
def test_clear(self): |
| 1019 |
a = array.array(self.typecode, self.example) |
| 1020 |
with self.assertRaises(TypeError): |
| 1021 |
a.clear(42) |
| 1022 |
a.clear() |
| 1023 |
self.assertEqual(len(a), 0) |
| 1024 |
self.assertEqual(a.typecode, self.typecode) |
| 1025 |
|
| 1026 |
a = array.array(self.typecode) |
| 1027 |
a.clear() |
| 1028 |
self.assertEqual(len(a), 0) |
| 1029 |
self.assertEqual(a.typecode, self.typecode) |
| 1030 |
|
| 1031 |
a = array.array(self.typecode, self.example) |
| 1032 |
a.clear() |
| 1033 |
a.append(self.example[2]) |
| 1034 |
a.append(self.example[3]) |
| 1035 |
self.assertEqual(a, array.array(self.typecode, self.example[2:4])) |
| 1036 |
|
| 1037 |
with memoryview(a): |
| 1038 |
with self.assertRaises(BufferError): |
| 1039 |
a.clear() |
| 1040 |
|
| 1041 |
def test_reverse(self): |
| 1042 |
a = array.array(self.typecode, self.example) |
| 1043 |
self.assertRaises(TypeError, a.reverse, 42) |
| 1044 |
a.reverse() |
| 1045 |
self.assertEqual( |
| 1046 |
a, |
| 1047 |
array.array(self.typecode, self.example[::-1]) |
| 1048 |
) |
| 1049 |
|
| 1050 |
def test_extend(self): |
| 1051 |
a = array.array(self.typecode, self.example) |
| 1052 |
self.assertRaises(TypeError, a.extend) |
| 1053 |
a.extend(array.array(self.typecode, self.example[::-1])) |
| 1054 |
self.assertEqual( |
| 1055 |
a, |
| 1056 |
array.array(self.typecode, self.example+self.example[::-1]) |
| 1057 |
) |
| 1058 |
|
| 1059 |
a = array.array(self.typecode, self.example) |
| 1060 |
a.extend(a) |
| 1061 |
self.assertEqual( |
| 1062 |
a, |
| 1063 |
array.array(self.typecode, self.example+self.example) |
| 1064 |
) |
| 1065 |
|
| 1066 |
b = array.array(self.badtypecode()) |
| 1067 |
self.assertRaises(TypeError, a.extend, b) |
| 1068 |
|
| 1069 |
a = array.array(self.typecode, self.example) |
| 1070 |
a.extend(self.example[::-1]) |
| 1071 |
self.assertEqual( |
| 1072 |
a, |
| 1073 |
array.array(self.typecode, self.example+self.example[::-1]) |
| 1074 |
) |
| 1075 |
|
| 1076 |
def test_constructor_with_iterable_argument(self): |
| 1077 |
a = array.array(self.typecode, iter(self.example)) |
| 1078 |
b = array.array(self.typecode, self.example) |
| 1079 |
self.assertEqual(a, b) |
| 1080 |
|
| 1081 |
# non-iterable argument |
| 1082 |
self.assertRaises(TypeError, array.array, self.typecode, 10) |
| 1083 |
|
| 1084 |
# pass through errors raised in __iter__ |
| 1085 |
class A: |
| 1086 |
def __iter__(self): |
| 1087 |
raise UnicodeError |
| 1088 |
self.assertRaises(UnicodeError, array.array, self.typecode, A()) |
| 1089 |
|
| 1090 |
# pass through errors raised in next() |
| 1091 |
def B(): |
| 1092 |
raise UnicodeError |
| 1093 |
yield None |
| 1094 |
self.assertRaises(UnicodeError, array.array, self.typecode, B()) |
| 1095 |
|
| 1096 |
def test_coveritertraverse(self): |
| 1097 |
try: |
| 1098 |
import gc |
| 1099 |
except ImportError: |
| 1100 |
self.skipTest('gc module not available') |
| 1101 |
a = array.array(self.typecode) |
| 1102 |
l = [iter(a)] |
| 1103 |
l.append(l) |
| 1104 |
gc.collect() |
| 1105 |
|
| 1106 |
def test_buffer(self): |
| 1107 |
a = array.array(self.typecode, self.example) |
| 1108 |
m = memoryview(a) |
| 1109 |
expected = m.tobytes() |
| 1110 |
self.assertEqual(a.tobytes(), expected) |
| 1111 |
self.assertEqual(a.tobytes()[0], expected[0]) |
| 1112 |
# Resizing is forbidden when there are buffer exports. |
| 1113 |
# For issue 4509, we also check after each error that |
| 1114 |
# the array was not modified. |
| 1115 |
self.assertRaises(BufferError, a.append, a[0]) |
| 1116 |
self.assertEqual(m.tobytes(), expected) |
| 1117 |
self.assertRaises(BufferError, a.extend, a[0:1]) |
| 1118 |
self.assertEqual(m.tobytes(), expected) |
| 1119 |
self.assertRaises(BufferError, a.remove, a[0]) |
| 1120 |
self.assertEqual(m.tobytes(), expected) |
| 1121 |
self.assertRaises(BufferError, a.pop, 0) |
| 1122 |
self.assertEqual(m.tobytes(), expected) |
| 1123 |
self.assertRaises(BufferError, a.fromlist, a.tolist()) |
| 1124 |
self.assertEqual(m.tobytes(), expected) |
| 1125 |
self.assertRaises(BufferError, a.frombytes, a.tobytes()) |
| 1126 |
self.assertEqual(m.tobytes(), expected) |
| 1127 |
if self.typecode in ('u', 'w'): |
| 1128 |
self.assertRaises(BufferError, a.fromunicode, a.tounicode()) |
| 1129 |
self.assertEqual(m.tobytes(), expected) |
| 1130 |
self.assertRaises(BufferError, operator.imul, a, 2) |
| 1131 |
self.assertEqual(m.tobytes(), expected) |
| 1132 |
self.assertRaises(BufferError, operator.imul, a, 0) |
| 1133 |
self.assertEqual(m.tobytes(), expected) |
| 1134 |
self.assertRaises(BufferError, operator.setitem, a, slice(0, 0), a) |
| 1135 |
self.assertEqual(m.tobytes(), expected) |
| 1136 |
self.assertRaises(BufferError, operator.delitem, a, 0) |
| 1137 |
self.assertEqual(m.tobytes(), expected) |
| 1138 |
self.assertRaises(BufferError, operator.delitem, a, slice(0, 1)) |
| 1139 |
self.assertEqual(m.tobytes(), expected) |
| 1140 |
|
| 1141 |
def test_weakref(self): |
| 1142 |
s = array.array(self.typecode, self.example) |
| 1143 |
p = weakref.proxy(s) |
| 1144 |
self.assertEqual(p.tobytes(), s.tobytes()) |
| 1145 |
s = None |
| 1146 |
support.gc_collect() # For PyPy or other GCs. |
| 1147 |
self.assertRaises(ReferenceError, len, p) |
| 1148 |
|
| 1149 |
@unittest.skipUnless(hasattr(sys, 'getrefcount'), |
| 1150 |
'test needs sys.getrefcount()') |
| 1151 |
def test_bug_782369(self): |
| 1152 |
for i in range(10): |
| 1153 |
b = array.array('B', range(64)) |
| 1154 |
rc = sys.getrefcount(10) |
| 1155 |
for i in range(10): |
| 1156 |
b = array.array('B', range(64)) |
| 1157 |
self.assertEqual(rc, sys.getrefcount(10)) |
| 1158 |
|
| 1159 |
def test_subclass_with_kwargs(self): |
| 1160 |
# SF bug #1486663 -- this used to erroneously raise a TypeError |
| 1161 |
ArraySubclassWithKwargs('b', newarg=1) |
| 1162 |
|
| 1163 |
def test_create_from_bytes(self): |
| 1164 |
# XXX This test probably needs to be moved in a subclass or |
| 1165 |
# generalized to use self.typecode. |
| 1166 |
a = array.array('H', b"1234") |
| 1167 |
self.assertEqual(len(a) * a.itemsize, 4) |
| 1168 |
|
| 1169 |
@support.cpython_only |
| 1170 |
def test_sizeof_with_buffer(self): |
| 1171 |
a = array.array(self.typecode, self.example) |
| 1172 |
basesize = support.calcvobjsize('Pn2Pi') |
| 1173 |
buffer_size = a.buffer_info()[1] * a.itemsize |
| 1174 |
support.check_sizeof(self, a, basesize + buffer_size) |
| 1175 |
|
| 1176 |
@support.cpython_only |
| 1177 |
def test_sizeof_without_buffer(self): |
| 1178 |
a = array.array(self.typecode) |
| 1179 |
basesize = support.calcvobjsize('Pn2Pi') |
| 1180 |
support.check_sizeof(self, a, basesize) |
| 1181 |
|
| 1182 |
def test_initialize_with_unicode(self): |
| 1183 |
if self.typecode not in ('u', 'w'): |
| 1184 |
with self.assertRaises(TypeError) as cm: |
| 1185 |
a = array.array(self.typecode, 'foo') |
| 1186 |
self.assertIn("cannot use a str", str(cm.exception)) |
| 1187 |
with self.assertRaises(TypeError) as cm: |
| 1188 |
a = array.array(self.typecode, array.array('w', 'foo')) |
| 1189 |
self.assertIn("cannot use a unicode array", str(cm.exception)) |
| 1190 |
else: |
| 1191 |
a = array.array(self.typecode, "foo") |
| 1192 |
a = array.array(self.typecode, array.array('u', 'foo')) |
| 1193 |
a = array.array(self.typecode, array.array('w', 'foo')) |
| 1194 |
|
| 1195 |
@support.cpython_only |
| 1196 |
def test_obsolete_write_lock(self): |
| 1197 |
_testcapi = import_helper.import_module('_testcapi') |
| 1198 |
a = array.array('B', b"") |
| 1199 |
self.assertRaises(BufferError, _testcapi.getbuffer_with_null_view, a) |
| 1200 |
|
| 1201 |
def test_free_after_iterating(self): |
| 1202 |
support.check_free_after_iterating(self, iter, array.array, |
| 1203 |
(self.typecode,)) |
| 1204 |
support.check_free_after_iterating(self, reversed, array.array, |
| 1205 |
(self.typecode,)) |
| 1206 |
|
| 1207 |
class StringTest(BaseTest): |
| 1208 |
|
| 1209 |
def test_setitem(self): |
| 1210 |
super().test_setitem() |
| 1211 |
a = array.array(self.typecode, self.example) |
| 1212 |
self.assertRaises(TypeError, a.__setitem__, 0, self.example[:2]) |
| 1213 |
|
| 1214 |
class UnicodeTest(StringTest, unittest.TestCase): |
| 1215 |
typecode = 'u' |
| 1216 |
example = '\x01\u263a\x00\ufeff' |
| 1217 |
smallerexample = '\x01\u263a\x00\ufefe' |
| 1218 |
biggerexample = '\x01\u263a\x01\ufeff' |
| 1219 |
outside = str('\x33') |
| 1220 |
minitemsize = sizeof_wchar |
| 1221 |
|
| 1222 |
def test_unicode(self): |
| 1223 |
self.assertRaises(TypeError, array.array, 'b', 'foo') |
| 1224 |
|
| 1225 |
a = array.array(self.typecode, '\xa0\xc2\u1234') |
| 1226 |
a.fromunicode(' ') |
| 1227 |
a.fromunicode('') |
| 1228 |
a.fromunicode('') |
| 1229 |
a.fromunicode('\x11abc\xff\u1234') |
| 1230 |
s = a.tounicode() |
| 1231 |
self.assertEqual(s, '\xa0\xc2\u1234 \x11abc\xff\u1234') |
| 1232 |
self.assertEqual(a.itemsize, self.minitemsize) |
| 1233 |
|
| 1234 |
s = '\x00="\'a\\b\x80\xff\u0000\u0001\u1234' |
| 1235 |
a = array.array(self.typecode, s) |
| 1236 |
self.assertEqual( |
| 1237 |
repr(a), |
| 1238 |
f"array('{self.typecode}', '\\x00=\"\\'a\\\\b\\x80\xff\\x00\\x01\u1234')") |
| 1239 |
|
| 1240 |
self.assertRaises(TypeError, a.fromunicode) |
| 1241 |
|
| 1242 |
def test_issue17223(self): |
| 1243 |
if self.typecode == 'u' and sizeof_wchar == 2: |
| 1244 |
# PyUnicode_FromUnicode() cannot fail with 16-bit wchar_t |
| 1245 |
self.skipTest("specific to 32-bit wchar_t") |
| 1246 |
|
| 1247 |
# this used to crash |
| 1248 |
# U+FFFFFFFF is an invalid code point in Unicode 6.0 |
| 1249 |
invalid_str = b'\xff\xff\xff\xff' |
| 1250 |
|
| 1251 |
a = array.array(self.typecode, invalid_str) |
| 1252 |
self.assertRaises(ValueError, a.tounicode) |
| 1253 |
self.assertRaises(ValueError, str, a) |
| 1254 |
|
| 1255 |
def test_typecode_u_deprecation(self): |
| 1256 |
with self.assertWarns(DeprecationWarning): |
| 1257 |
array.array("u") |
| 1258 |
|
| 1259 |
def test_empty_string_mem_leak_gh140474(self): |
| 1260 |
with warnings.catch_warnings(): |
| 1261 |
warnings.simplefilter('ignore', DeprecationWarning) |
| 1262 |
for _ in range(1000): |
| 1263 |
a = array.array('u', '') |
| 1264 |
self.assertEqual(len(a), 0) |
| 1265 |
self.assertEqual(a.typecode, 'u') |
| 1266 |
|
| 1267 |
|
| 1268 |
class UCS4Test(UnicodeTest): |
| 1269 |
typecode = 'w' |
| 1270 |
minitemsize = 4 |
| 1271 |
|
| 1272 |
|
| 1273 |
class NumberTest(BaseTest): |
| 1274 |
|
| 1275 |
def test_extslice(self): |
| 1276 |
a = array.array(self.typecode, range(5)) |
| 1277 |
self.assertEqual(a[::], a) |
| 1278 |
self.assertEqual(a[::2], array.array(self.typecode, [0,2,4])) |
| 1279 |
self.assertEqual(a[1::2], array.array(self.typecode, [1,3])) |
| 1280 |
self.assertEqual(a[::-1], array.array(self.typecode, [4,3,2,1,0])) |
| 1281 |
self.assertEqual(a[::-2], array.array(self.typecode, [4,2,0])) |
| 1282 |
self.assertEqual(a[3::-2], array.array(self.typecode, [3,1])) |
| 1283 |
self.assertEqual(a[-100:100:], a) |
| 1284 |
self.assertEqual(a[100:-100:-1], a[::-1]) |
| 1285 |
self.assertEqual(a[-100:100:2], array.array(self.typecode, [0,2,4])) |
| 1286 |
self.assertEqual(a[1000:2000:2], array.array(self.typecode, [])) |
| 1287 |
self.assertEqual(a[-1000:-2000:-2], array.array(self.typecode, [])) |
| 1288 |
|
| 1289 |
def test_delslice(self): |
| 1290 |
a = array.array(self.typecode, range(5)) |
| 1291 |
del a[::2] |
| 1292 |
self.assertEqual(a, array.array(self.typecode, [1,3])) |
| 1293 |
a = array.array(self.typecode, range(5)) |
| 1294 |
del a[1::2] |
| 1295 |
self.assertEqual(a, array.array(self.typecode, [0,2,4])) |
| 1296 |
a = array.array(self.typecode, range(5)) |
| 1297 |
del a[1::-2] |
| 1298 |
self.assertEqual(a, array.array(self.typecode, [0,2,3,4])) |
| 1299 |
a = array.array(self.typecode, range(10)) |
| 1300 |
del a[::1000] |
| 1301 |
self.assertEqual(a, array.array(self.typecode, [1,2,3,4,5,6,7,8,9])) |
| 1302 |
# test issue7788 |
| 1303 |
a = array.array(self.typecode, range(10)) |
| 1304 |
del a[9::1<<333] |
| 1305 |
|
| 1306 |
def test_assignment(self): |
| 1307 |
a = array.array(self.typecode, range(10)) |
| 1308 |
a[::2] = array.array(self.typecode, [42]*5) |
| 1309 |
self.assertEqual(a, array.array(self.typecode, [42, 1, 42, 3, 42, 5, 42, 7, 42, 9])) |
| 1310 |
a = array.array(self.typecode, range(10)) |
| 1311 |
a[::-4] = array.array(self.typecode, [10]*3) |
| 1312 |
self.assertEqual(a, array.array(self.typecode, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10])) |
| 1313 |
a = array.array(self.typecode, range(4)) |
| 1314 |
a[::-1] = a |
| 1315 |
self.assertEqual(a, array.array(self.typecode, [3, 2, 1, 0])) |
| 1316 |
a = array.array(self.typecode, range(10)) |
| 1317 |
b = a[:] |
| 1318 |
c = a[:] |
| 1319 |
ins = array.array(self.typecode, range(2)) |
| 1320 |
a[2:3] = ins |
| 1321 |
b[slice(2,3)] = ins |
| 1322 |
c[2:3:] = ins |
| 1323 |
|
| 1324 |
def test_iterationcontains(self): |
| 1325 |
a = array.array(self.typecode, range(10)) |
| 1326 |
self.assertEqual(list(a), list(range(10))) |
| 1327 |
b = array.array(self.typecode, [20]) |
| 1328 |
self.assertEqual(a[-1] in a, True) |
| 1329 |
self.assertEqual(b[0] not in a, True) |
| 1330 |
|
| 1331 |
def check_overflow(self, lower, upper): |
| 1332 |
# method to be used by subclasses |
| 1333 |
|
| 1334 |
# should not overflow assigning lower limit |
| 1335 |
a = array.array(self.typecode, [lower]) |
| 1336 |
a[0] = lower |
| 1337 |
# should overflow assigning less than lower limit |
| 1338 |
self.assertRaises(OverflowError, array.array, self.typecode, [lower-1]) |
| 1339 |
self.assertRaises(OverflowError, a.__setitem__, 0, lower-1) |
| 1340 |
# should not overflow assigning upper limit |
| 1341 |
a = array.array(self.typecode, [upper]) |
| 1342 |
a[0] = upper |
| 1343 |
# should overflow assigning more than upper limit |
| 1344 |
self.assertRaises(OverflowError, array.array, self.typecode, [upper+1]) |
| 1345 |
self.assertRaises(OverflowError, a.__setitem__, 0, upper+1) |
| 1346 |
|
| 1347 |
def test_subclassing(self): |
| 1348 |
typecode = self.typecode |
| 1349 |
class ExaggeratingArray(array.array): |
| 1350 |
__slots__ = ['offset'] |
| 1351 |
|
| 1352 |
def __new__(cls, typecode, data, offset): |
| 1353 |
return array.array.__new__(cls, typecode, data) |
| 1354 |
|
| 1355 |
def __init__(self, typecode, data, offset): |
| 1356 |
self.offset = offset |
| 1357 |
|
| 1358 |
def __getitem__(self, i): |
| 1359 |
return array.array.__getitem__(self, i) + self.offset |
| 1360 |
|
| 1361 |
a = ExaggeratingArray(self.typecode, [3, 6, 7, 11], 4) |
| 1362 |
self.assertEntryEqual(a[0], 7) |
| 1363 |
|
| 1364 |
self.assertRaises(AttributeError, setattr, a, "color", "blue") |
| 1365 |
|
| 1366 |
def test_frombytearray(self): |
| 1367 |
a = array.array('b', range(10)) |
| 1368 |
b = array.array(self.typecode, a) |
| 1369 |
self.assertEqual(a, b) |
| 1370 |
|
| 1371 |
class IntegerNumberTest(NumberTest): |
| 1372 |
def test_type_error(self): |
| 1373 |
a = array.array(self.typecode) |
| 1374 |
a.append(42) |
| 1375 |
with self.assertRaises(TypeError): |
| 1376 |
a.append(42.0) |
| 1377 |
with self.assertRaises(TypeError): |
| 1378 |
a[0] = 42.0 |
| 1379 |
|
| 1380 |
class Intable: |
| 1381 |
def __init__(self, num): |
| 1382 |
self._num = num |
| 1383 |
def __index__(self): |
| 1384 |
return self._num |
| 1385 |
def __int__(self): |
| 1386 |
return self._num |
| 1387 |
def __sub__(self, other): |
| 1388 |
return Intable(int(self) - int(other)) |
| 1389 |
def __add__(self, other): |
| 1390 |
return Intable(int(self) + int(other)) |
| 1391 |
|
| 1392 |
class SignedNumberTest(IntegerNumberTest): |
| 1393 |
example = [-1, 0, 1, 42, 0x7f] |
| 1394 |
smallerexample = [-1, 0, 1, 42, 0x7e] |
| 1395 |
biggerexample = [-1, 0, 1, 43, 0x7f] |
| 1396 |
outside = 23 |
| 1397 |
|
| 1398 |
def test_overflow(self): |
| 1399 |
a = array.array(self.typecode) |
| 1400 |
lower = -1 * int(pow(2, a.itemsize * 8 - 1)) |
| 1401 |
upper = int(pow(2, a.itemsize * 8 - 1)) - 1 |
| 1402 |
self.check_overflow(lower, upper) |
| 1403 |
self.check_overflow(Intable(lower), Intable(upper)) |
| 1404 |
|
| 1405 |
class UnsignedNumberTest(IntegerNumberTest): |
| 1406 |
example = [0, 1, 17, 23, 42, 0xff] |
| 1407 |
smallerexample = [0, 1, 17, 23, 42, 0xfe] |
| 1408 |
biggerexample = [0, 1, 17, 23, 43, 0xff] |
| 1409 |
outside = 0xaa |
| 1410 |
|
| 1411 |
def test_overflow(self): |
| 1412 |
a = array.array(self.typecode) |
| 1413 |
lower = 0 |
| 1414 |
upper = int(pow(2, a.itemsize * 8)) - 1 |
| 1415 |
self.check_overflow(lower, upper) |
| 1416 |
self.check_overflow(Intable(lower), Intable(upper)) |
| 1417 |
|
| 1418 |
def test_bytes_extend(self): |
| 1419 |
s = bytes(self.example) |
| 1420 |
|
| 1421 |
a = array.array(self.typecode, self.example) |
| 1422 |
a.extend(s) |
| 1423 |
self.assertEqual( |
| 1424 |
a, |
| 1425 |
array.array(self.typecode, self.example+self.example) |
| 1426 |
) |
| 1427 |
|
| 1428 |
a = array.array(self.typecode, self.example) |
| 1429 |
a.extend(bytearray(reversed(s))) |
| 1430 |
self.assertEqual( |
| 1431 |
a, |
| 1432 |
array.array(self.typecode, self.example+self.example[::-1]) |
| 1433 |
) |
| 1434 |
|
| 1435 |
|
| 1436 |
class ByteTest(SignedNumberTest, unittest.TestCase): |
| 1437 |
typecode = 'b' |
| 1438 |
minitemsize = 1 |
| 1439 |
|
| 1440 |
class UnsignedByteTest(UnsignedNumberTest, unittest.TestCase): |
| 1441 |
typecode = 'B' |
| 1442 |
minitemsize = 1 |
| 1443 |
|
| 1444 |
class ShortTest(SignedNumberTest, unittest.TestCase): |
| 1445 |
typecode = 'h' |
| 1446 |
minitemsize = 2 |
| 1447 |
|
| 1448 |
class UnsignedShortTest(UnsignedNumberTest, unittest.TestCase): |
| 1449 |
typecode = 'H' |
| 1450 |
minitemsize = 2 |
| 1451 |
|
| 1452 |
class IntTest(SignedNumberTest, unittest.TestCase): |
| 1453 |
typecode = 'i' |
| 1454 |
minitemsize = 2 |
| 1455 |
|
| 1456 |
class UnsignedIntTest(UnsignedNumberTest, unittest.TestCase): |
| 1457 |
typecode = 'I' |
| 1458 |
minitemsize = 2 |
| 1459 |
|
| 1460 |
class LongTest(SignedNumberTest, unittest.TestCase): |
| 1461 |
typecode = 'l' |
| 1462 |
minitemsize = 4 |
| 1463 |
|
| 1464 |
class UnsignedLongTest(UnsignedNumberTest, unittest.TestCase): |
| 1465 |
typecode = 'L' |
| 1466 |
minitemsize = 4 |
| 1467 |
|
| 1468 |
class LongLongTest(SignedNumberTest, unittest.TestCase): |
| 1469 |
typecode = 'q' |
| 1470 |
minitemsize = 8 |
| 1471 |
|
| 1472 |
class UnsignedLongLongTest(UnsignedNumberTest, unittest.TestCase): |
| 1473 |
typecode = 'Q' |
| 1474 |
minitemsize = 8 |
| 1475 |
|
| 1476 |
class FPTest(NumberTest): |
| 1477 |
example = [-42.0, 0, 42, 1e5, -1e10] |
| 1478 |
smallerexample = [-42.0, 0, 42, 1e5, -2e10] |
| 1479 |
biggerexample = [-42.0, 0, 42, 1e5, 1e10] |
| 1480 |
outside = 23 |
| 1481 |
|
| 1482 |
def assertEntryEqual(self, entry1, entry2): |
| 1483 |
self.assertAlmostEqual(entry1, entry2) |
| 1484 |
|
| 1485 |
def test_nan(self): |
| 1486 |
a = array.array(self.typecode, [float('nan')]) |
| 1487 |
b = array.array(self.typecode, [float('nan')]) |
| 1488 |
self.assertIs(a != b, True) |
| 1489 |
self.assertIs(a == b, False) |
| 1490 |
self.assertIs(a > b, False) |
| 1491 |
self.assertIs(a >= b, False) |
| 1492 |
self.assertIs(a < b, False) |
| 1493 |
self.assertIs(a <= b, False) |
| 1494 |
|
| 1495 |
def test_byteswap(self): |
| 1496 |
a = array.array(self.typecode, self.example) |
| 1497 |
self.assertRaises(TypeError, a.byteswap, 42) |
| 1498 |
if a.itemsize in (1, 2, 4, 8): |
| 1499 |
b = array.array(self.typecode, self.example) |
| 1500 |
b.byteswap() |
| 1501 |
if a.itemsize==1: |
| 1502 |
self.assertEqual(a, b) |
| 1503 |
else: |
| 1504 |
# On alphas treating the byte swapped bit patterns as |
| 1505 |
# floats/doubles results in floating-point exceptions |
| 1506 |
# => compare the 8bit string values instead |
| 1507 |
self.assertNotEqual(a.tobytes(), b.tobytes()) |
| 1508 |
b.byteswap() |
| 1509 |
self.assertEqual(a, b) |
| 1510 |
|
| 1511 |
class FloatTest(FPTest, unittest.TestCase): |
| 1512 |
typecode = 'f' |
| 1513 |
minitemsize = 4 |
| 1514 |
|
| 1515 |
class DoubleTest(FPTest, unittest.TestCase): |
| 1516 |
typecode = 'd' |
| 1517 |
minitemsize = 8 |
| 1518 |
|
| 1519 |
def test_alloc_overflow(self): |
| 1520 |
from sys import maxsize |
| 1521 |
a = array.array('d', [-1]*65536) |
| 1522 |
try: |
| 1523 |
a *= maxsize//65536 + 1 |
| 1524 |
except MemoryError: |
| 1525 |
pass |
| 1526 |
else: |
| 1527 |
self.fail("Array of size > maxsize created - MemoryError expected") |
| 1528 |
b = array.array('d', [ 2.71828183, 3.14159265, -1]) |
| 1529 |
try: |
| 1530 |
b * (maxsize//3 + 1) |
| 1531 |
except MemoryError: |
| 1532 |
pass |
| 1533 |
else: |
| 1534 |
self.fail("Array of size > maxsize created - MemoryError expected") |
| 1535 |
|
| 1536 |
|
| 1537 |
class LargeArrayTest(unittest.TestCase): |
| 1538 |
typecode = 'b' |
| 1539 |
|
| 1540 |
def example(self, size): |
| 1541 |
# We assess a base memuse of <=2.125 for constructing this array |
| 1542 |
base = array.array(self.typecode, [0, 1, 2, 3, 4, 5, 6, 7]) * (size // 8) |
| 1543 |
base += array.array(self.typecode, [99]*(size % 8) + [8, 9, 10, 11]) |
| 1544 |
return base |
| 1545 |
|
| 1546 |
@support.bigmemtest(_2G, memuse=2.125) |
| 1547 |
def test_example_data(self, size): |
| 1548 |
example = self.example(size) |
| 1549 |
self.assertEqual(len(example), size+4) |
| 1550 |
|
| 1551 |
@support.bigmemtest(_2G, memuse=2.125) |
| 1552 |
def test_access(self, size): |
| 1553 |
example = self.example(size) |
| 1554 |
self.assertEqual(example[0], 0) |
| 1555 |
self.assertEqual(example[-(size+4)], 0) |
| 1556 |
self.assertEqual(example[size], 8) |
| 1557 |
self.assertEqual(example[-4], 8) |
| 1558 |
self.assertEqual(example[size+3], 11) |
| 1559 |
self.assertEqual(example[-1], 11) |
| 1560 |
|
| 1561 |
@support.bigmemtest(_2G, memuse=2.125+1) |
| 1562 |
def test_slice(self, size): |
| 1563 |
example = self.example(size) |
| 1564 |
self.assertEqual(list(example[:4]), [0, 1, 2, 3]) |
| 1565 |
self.assertEqual(list(example[-4:]), [8, 9, 10, 11]) |
| 1566 |
part = example[1:-1] |
| 1567 |
self.assertEqual(len(part), size+2) |
| 1568 |
self.assertEqual(part[0], 1) |
| 1569 |
self.assertEqual(part[-1], 10) |
| 1570 |
del part |
| 1571 |
part = example[::2] |
| 1572 |
self.assertEqual(len(part), (size+5)//2) |
| 1573 |
self.assertEqual(list(part[:4]), [0, 2, 4, 6]) |
| 1574 |
if size % 2: |
| 1575 |
self.assertEqual(list(part[-2:]), [9, 11]) |
| 1576 |
else: |
| 1577 |
self.assertEqual(list(part[-2:]), [8, 10]) |
| 1578 |
|
| 1579 |
@support.bigmemtest(_2G, memuse=2.125) |
| 1580 |
def test_count(self, size): |
| 1581 |
example = self.example(size) |
| 1582 |
self.assertEqual(example.count(0), size//8) |
| 1583 |
self.assertEqual(example.count(11), 1) |
| 1584 |
|
| 1585 |
@support.bigmemtest(_2G, memuse=2.125) |
| 1586 |
def test_append(self, size): |
| 1587 |
example = self.example(size) |
| 1588 |
example.append(12) |
| 1589 |
self.assertEqual(example[-1], 12) |
| 1590 |
|
| 1591 |
@support.bigmemtest(_2G, memuse=2.125) |
| 1592 |
def test_extend(self, size): |
| 1593 |
example = self.example(size) |
| 1594 |
example.extend(iter([12, 13, 14, 15])) |
| 1595 |
self.assertEqual(len(example), size+8) |
| 1596 |
self.assertEqual(list(example[-8:]), [8, 9, 10, 11, 12, 13, 14, 15]) |
| 1597 |
|
| 1598 |
@support.bigmemtest(_2G, memuse=2.125) |
| 1599 |
def test_frombytes(self, size): |
| 1600 |
example = self.example(size) |
| 1601 |
example.frombytes(b'abcd') |
| 1602 |
self.assertEqual(len(example), size+8) |
| 1603 |
self.assertEqual(list(example[-8:]), [8, 9, 10, 11] + list(b'abcd')) |
| 1604 |
|
| 1605 |
@support.bigmemtest(_2G, memuse=2.125) |
| 1606 |
def test_fromlist(self, size): |
| 1607 |
example = self.example(size) |
| 1608 |
example.fromlist([12, 13, 14, 15]) |
| 1609 |
self.assertEqual(len(example), size+8) |
| 1610 |
self.assertEqual(list(example[-8:]), [8, 9, 10, 11, 12, 13, 14, 15]) |
| 1611 |
|
| 1612 |
@support.bigmemtest(_2G, memuse=2.125) |
| 1613 |
def test_index(self, size): |
| 1614 |
example = self.example(size) |
| 1615 |
self.assertEqual(example.index(0), 0) |
| 1616 |
self.assertEqual(example.index(1), 1) |
| 1617 |
self.assertEqual(example.index(7), 7) |
| 1618 |
self.assertEqual(example.index(11), size+3) |
| 1619 |
|
| 1620 |
@support.bigmemtest(_2G, memuse=2.125) |
| 1621 |
def test_insert(self, size): |
| 1622 |
example = self.example(size) |
| 1623 |
example.insert(0, 12) |
| 1624 |
example.insert(10, 13) |
| 1625 |
example.insert(size+1, 14) |
| 1626 |
self.assertEqual(len(example), size+7) |
| 1627 |
self.assertEqual(example[0], 12) |
| 1628 |
self.assertEqual(example[10], 13) |
| 1629 |
self.assertEqual(example[size+1], 14) |
| 1630 |
|
| 1631 |
@support.bigmemtest(_2G, memuse=2.125) |
| 1632 |
def test_pop(self, size): |
| 1633 |
example = self.example(size) |
| 1634 |
self.assertEqual(example.pop(0), 0) |
| 1635 |
self.assertEqual(example[0], 1) |
| 1636 |
self.assertEqual(example.pop(size+1), 10) |
| 1637 |
self.assertEqual(example[size+1], 11) |
| 1638 |
self.assertEqual(example.pop(1), 2) |
| 1639 |
self.assertEqual(example[1], 3) |
| 1640 |
self.assertEqual(len(example), size+1) |
| 1641 |
self.assertEqual(example.pop(), 11) |
| 1642 |
self.assertEqual(len(example), size) |
| 1643 |
|
| 1644 |
@support.bigmemtest(_2G, memuse=2.125) |
| 1645 |
def test_remove(self, size): |
| 1646 |
example = self.example(size) |
| 1647 |
example.remove(0) |
| 1648 |
self.assertEqual(len(example), size+3) |
| 1649 |
self.assertEqual(example[0], 1) |
| 1650 |
example.remove(10) |
| 1651 |
self.assertEqual(len(example), size+2) |
| 1652 |
self.assertEqual(example[size], 9) |
| 1653 |
self.assertEqual(example[size+1], 11) |
| 1654 |
|
| 1655 |
@support.bigmemtest(_2G, memuse=2.125) |
| 1656 |
def test_reverse(self, size): |
| 1657 |
example = self.example(size) |
| 1658 |
example.reverse() |
| 1659 |
self.assertEqual(len(example), size+4) |
| 1660 |
self.assertEqual(example[0], 11) |
| 1661 |
self.assertEqual(example[3], 8) |
| 1662 |
self.assertEqual(example[-1], 0) |
| 1663 |
example.reverse() |
| 1664 |
self.assertEqual(len(example), size+4) |
| 1665 |
self.assertEqual(list(example[:4]), [0, 1, 2, 3]) |
| 1666 |
self.assertEqual(list(example[-4:]), [8, 9, 10, 11]) |
| 1667 |
|
| 1668 |
# list takes about 9 bytes per element |
| 1669 |
@support.bigmemtest(_2G, memuse=2.125+9) |
| 1670 |
def test_tolist(self, size): |
| 1671 |
example = self.example(size) |
| 1672 |
ls = example.tolist() |
| 1673 |
self.assertEqual(len(ls), len(example)) |
| 1674 |
self.assertEqual(ls[:8], list(example[:8])) |
| 1675 |
self.assertEqual(ls[-8:], list(example[-8:])) |
| 1676 |
|
| 1677 |
def test_gh_128961(self): |
| 1678 |
a = array.array('i') |
| 1679 |
it = iter(a) |
| 1680 |
list(it) |
| 1681 |
it.__setstate__(0) |
| 1682 |
self.assertRaises(StopIteration, next, it) |
| 1683 |
|
| 1684 |
# Tests for NULL pointer dereference in array.__setitem__ |
| 1685 |
# when the index conversion mutates the array. |
| 1686 |
# See: https://github.com/python/cpython/issues/142555. |
| 1687 |
|
| 1688 |
@subTests("dtype", ["b", "B", "h", "H", "i", "l", "q", "I", "L", "Q"]) |
| 1689 |
def test_setitem_use_after_clear_with_int_data(self, dtype): |
| 1690 |
victim = array.array(dtype, list(range(64))) |
| 1691 |
|
| 1692 |
class Index: |
| 1693 |
def __index__(self): |
| 1694 |
victim.clear() |
| 1695 |
return 0 |
| 1696 |
|
| 1697 |
self.assertRaises(IndexError, victim.__setitem__, 1, Index()) |
| 1698 |
self.assertEqual(len(victim), 0) |
| 1699 |
|
| 1700 |
def test_setitem_use_after_shrink_with_int_data(self): |
| 1701 |
victim = array.array('b', [1, 2, 3]) |
| 1702 |
|
| 1703 |
class Index: |
| 1704 |
def __index__(self): |
| 1705 |
victim.pop() |
| 1706 |
victim.pop() |
| 1707 |
return 0 |
| 1708 |
|
| 1709 |
self.assertRaises(IndexError, victim.__setitem__, 1, Index()) |
| 1710 |
|
| 1711 |
@subTests("dtype", ["f", "d"]) |
| 1712 |
def test_setitem_use_after_clear_with_float_data(self, dtype): |
| 1713 |
victim = array.array(dtype, [1.0, 2.0, 3.0]) |
| 1714 |
|
| 1715 |
class Float: |
| 1716 |
def __float__(self): |
| 1717 |
victim.clear() |
| 1718 |
return 0.0 |
| 1719 |
|
| 1720 |
self.assertRaises(IndexError, victim.__setitem__, 1, Float()) |
| 1721 |
self.assertEqual(len(victim), 0) |
| 1722 |
|
| 1723 |
|
| 1724 |
if __name__ == "__main__": |
| 1725 |
unittest.main() |