Skip to content

Commit 8d3bac9

Browse files
CPython developersyouknowone
authored andcommitted
Update test_array from CPython 3.10.5
1 parent 003f9db commit 8d3bac9

File tree

1 file changed

+95
-120
lines changed

1 file changed

+95
-120
lines changed

Lib/test/test_array.py

Lines changed: 95 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
Roger E. Masse
33
"""
44

5+
import collections.abc
56
import unittest
67
from test import support
7-
from test.support import _2G, os_helper
8+
from test.support import os_helper
9+
from test.support import _2G
810
import weakref
911
import pickle
1012
import operator
1113
import struct
1214
import sys
13-
import warnings
1415

1516
import array
1617
from array import _array_reconstructor as array_reconstructor
@@ -29,12 +30,29 @@ def __init__(self, typecode, newarg=None):
2930

3031
class MiscTest(unittest.TestCase):
3132

33+
def test_array_is_sequence(self):
34+
self.assertIsInstance(array.array("B"), collections.abc.MutableSequence)
35+
self.assertIsInstance(array.array("B"), collections.abc.Reversible)
36+
3237
def test_bad_constructor(self):
3338
self.assertRaises(TypeError, array.array)
3439
self.assertRaises(TypeError, array.array, spam=42)
3540
self.assertRaises(TypeError, array.array, 'xx')
3641
self.assertRaises(ValueError, array.array, 'x')
3742

43+
@support.cpython_only
44+
def test_disallow_instantiation(self):
45+
my_array = array.array("I")
46+
support.check_disallow_instantiation(
47+
self, type(iter(my_array)), my_array
48+
)
49+
50+
@support.cpython_only
51+
def test_immutable(self):
52+
# bpo-43908: check that array.array is immutable
53+
with self.assertRaises(TypeError):
54+
array.array.foo = 1
55+
3856
def test_empty(self):
3957
# Exercise code for handling zero-length arrays
4058
a = array.array('B')
@@ -335,6 +353,67 @@ def test_exhausted_iterator(self):
335353
self.assertEqual(list(empit), [self.outside])
336354
self.assertEqual(list(a), list(self.example) + [self.outside])
337355

356+
def test_reverse_iterator(self):
357+
a = array.array(self.typecode, self.example)
358+
self.assertEqual(list(a), list(self.example))
359+
self.assertEqual(list(reversed(a)), list(iter(a))[::-1])
360+
361+
def test_reverse_iterator_picking(self):
362+
orig = array.array(self.typecode, self.example)
363+
data = list(orig)
364+
data2 = [self.outside] + data
365+
rev_data = data[len(data)-2::-1] + [self.outside]
366+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
367+
# initial iterator
368+
itorig = reversed(orig)
369+
d = pickle.dumps((itorig, orig), proto)
370+
it, a = pickle.loads(d)
371+
a.insert(0, self.outside)
372+
self.assertEqual(type(it), type(itorig))
373+
self.assertEqual(list(it), rev_data)
374+
self.assertEqual(list(a), data2)
375+
376+
# running iterator
377+
next(itorig)
378+
d = pickle.dumps((itorig, orig), proto)
379+
it, a = pickle.loads(d)
380+
a.insert(0, self.outside)
381+
self.assertEqual(type(it), type(itorig))
382+
self.assertEqual(list(it), rev_data[1:])
383+
self.assertEqual(list(a), data2)
384+
385+
# empty iterator
386+
for i in range(1, len(data)):
387+
next(itorig)
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), [])
393+
self.assertEqual(list(a), data2)
394+
395+
# exhausted iterator
396+
self.assertRaises(StopIteration, next, itorig)
397+
d = pickle.dumps((itorig, orig), proto)
398+
it, a = pickle.loads(d)
399+
a.insert(0, self.outside)
400+
self.assertEqual(list(it), [])
401+
self.assertEqual(list(a), data2)
402+
403+
def test_exhausted_reverse_iterator(self):
404+
a = array.array(self.typecode, self.example)
405+
self.assertEqual(list(a), list(self.example))
406+
exhit = reversed(a)
407+
empit = reversed(a)
408+
for x in exhit: # exhaust the iterator
409+
next(empit) # Pointing past the 0th position.
410+
a.insert(0, self.outside)
411+
self.assertEqual(list(exhit), [])
412+
# The iterator index points past the 0th position so inserting
413+
# an element in the beginning does not make it appear.
414+
self.assertEqual(list(empit), [])
415+
self.assertEqual(list(a), [self.outside] + list(self.example))
416+
338417
def test_insert(self):
339418
a = array.array(self.typecode, self.example)
340419
a.insert(0, self.example[0])
@@ -430,28 +509,6 @@ def test_tofromlist(self):
430509
b.fromlist(a.tolist())
431510
self.assertEqual(a, b)
432511

433-
# TODO: RUSTPYTHON
434-
@unittest.expectedFailure
435-
def test_tofromstring(self):
436-
# Warnings not raised when arguments are incorrect as Argument Clinic
437-
# handles that before the warning can be raised.
438-
nb_warnings = 2
439-
with warnings.catch_warnings(record=True) as r:
440-
warnings.filterwarnings("always",
441-
message=r"(to|from)string\(\) is deprecated",
442-
category=DeprecationWarning)
443-
a = array.array(self.typecode, 2*self.example)
444-
b = array.array(self.typecode)
445-
self.assertRaises(TypeError, a.tostring, 42)
446-
self.assertRaises(TypeError, b.fromstring)
447-
self.assertRaises(TypeError, b.fromstring, 42)
448-
b.fromstring(a.tostring())
449-
self.assertEqual(a, b)
450-
if a.itemsize>1:
451-
self.assertRaises(ValueError, b.fromstring, "x")
452-
nb_warnings += 1
453-
self.assertEqual(len(r), nb_warnings)
454-
455512
def test_tofrombytes(self):
456513
a = array.array(self.typecode, 2*self.example)
457514
b = array.array(self.typecode)
@@ -878,6 +935,17 @@ def test_index(self):
878935
self.assertRaises(ValueError, a.index, None)
879936
self.assertRaises(ValueError, a.index, self.outside)
880937

938+
a = array.array('i', [-2, -1, 0, 0, 1, 2])
939+
self.assertEqual(a.index(0), 2)
940+
self.assertEqual(a.index(0, 2), 2)
941+
self.assertEqual(a.index(0, -4), 2)
942+
self.assertEqual(a.index(-2, -10), 0)
943+
self.assertEqual(a.index(0, 3), 3)
944+
self.assertEqual(a.index(0, -3), 3)
945+
self.assertEqual(a.index(0, 3, 4), 3)
946+
self.assertEqual(a.index(0, -3, -2), 3)
947+
self.assertRaises(ValueError, a.index, 2, 0, -10)
948+
881949
def test_count(self):
882950
example = 2*self.example
883951
a = array.array(self.typecode, example)
@@ -1033,6 +1101,7 @@ def test_weakref(self):
10331101
p = weakref.proxy(s)
10341102
self.assertEqual(p.tobytes(), s.tobytes())
10351103
s = None
1104+
support.gc_collect() # For PyPy or other GCs.
10361105
self.assertRaises(ReferenceError, len, p)
10371106

10381107
@unittest.skipUnless(hasattr(sys, 'getrefcount'),
@@ -1111,105 +1180,11 @@ class UnicodeTest(StringTest, unittest.TestCase):
11111180
outside = str('\x33')
11121181
minitemsize = 2
11131182

1114-
def test_add(self):
1115-
super().test_add()
1116-
1117-
def test_buffer(self):
1118-
super().test_buffer()
1119-
1120-
def test_buffer_info(self):
1121-
super().test_buffer_info()
1122-
1123-
def test_byteswap(self):
1124-
super().test_byteswap()
1125-
1126-
def test_cmp(self):
1127-
super().test_cmp()
1128-
1129-
def test_constructor(self):
1130-
super().test_constructor()
1131-
1132-
def test_constructor_with_iterable_argument(self):
1133-
super().test_constructor_with_iterable_argument()
1134-
1135-
def test_copy(self):
1136-
super().test_copy()
1137-
1138-
def test_count(self):
1139-
super().test_count()
1140-
1141-
def test_coveritertraverse(self):
1142-
super().test_coveritertraverse()
1143-
1144-
def test_deepcopy(self):
1145-
super().test_deepcopy()
1146-
1147-
def test_delitem(self):
1148-
super().test_delitem()
1149-
1150-
def test_exhausted_iterator(self):
1151-
super().test_exhausted_iterator()
1152-
1153-
def test_extend(self):
1154-
super().test_extend()
1155-
1156-
def test_extended_getslice(self):
1157-
super().test_extended_getslice()
1158-
1159-
def test_extended_set_del_slice(self):
1160-
super().test_extended_set_del_slice()
1161-
1162-
def test_fromarray(self):
1163-
super().test_fromarray()
1164-
1165-
def test_getitem(self):
1166-
super().test_getitem()
1167-
1168-
def test_getslice(self):
1169-
super().test_getslice()
1170-
1171-
def test_iadd(self):
1172-
super().test_iadd()
1173-
1174-
def test_imul(self):
1175-
super().test_imul()
1176-
1183+
# TODO: RUSTPYTHON
1184+
@unittest.expectedFailure
11771185
def test_index(self):
11781186
super().test_index()
11791187

1180-
def test_insert(self):
1181-
super().test_insert()
1182-
1183-
def test_len(self):
1184-
super().test_len()
1185-
1186-
def test_mul(self):
1187-
super().test_mul()
1188-
1189-
def test_pop(self):
1190-
super().test_pop()
1191-
1192-
def test_remove(self):
1193-
super().test_remove()
1194-
1195-
def test_repr(self):
1196-
super().test_repr()
1197-
1198-
def test_reverse(self):
1199-
super().test_reverse()
1200-
1201-
def test_setslice(self):
1202-
super().test_setslice()
1203-
1204-
def test_str(self):
1205-
super().test_str()
1206-
1207-
def test_tofrombytes(self):
1208-
super().test_tofrombytes()
1209-
1210-
def test_tofromlist(self):
1211-
super().test_tofromlist()
1212-
12131188
def test_unicode(self):
12141189
self.assertRaises(TypeError, array.array, 'b', 'foo')
12151190

0 commit comments

Comments
 (0)