diff options
Diffstat (limited to 'Lib/test/test_array.py')
-rw-r--r-- | Lib/test/test_array.py | 70 |
1 files changed, 55 insertions, 15 deletions
diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 07c9bf999b..2a21e745b1 100644 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -38,14 +38,24 @@ typecodes = "ubBhHiIlLfd" if have_long_long: typecodes += 'qQ' -class BadConstructorTest(unittest.TestCase): +class MiscTest(unittest.TestCase): - def test_constructor(self): + def test_bad_constructor(self): self.assertRaises(TypeError, array.array) self.assertRaises(TypeError, array.array, spam=42) self.assertRaises(TypeError, array.array, 'xx') self.assertRaises(ValueError, array.array, 'x') + def test_empty(self): + # Exercise code for handling zero-length arrays + a = array.array('B') + a[:] = a + self.assertEqual(len(a), 0) + self.assertEqual(len(a + a), 0) + self.assertEqual(len(a * 3), 0) + a += a + self.assertEqual(len(a), 0) + # Machine format codes. # @@ -284,19 +294,42 @@ class BaseTest: self.assertEqual(type(a), type(b)) def test_iterator_pickle(self): - data = array.array(self.typecode, self.example) + orig = array.array(self.typecode, self.example) + data = list(orig) + data2 = data[::-1] for proto in range(pickle.HIGHEST_PROTOCOL + 1): - orgit = iter(data) - d = pickle.dumps(orgit, proto) - it = pickle.loads(d) - self.assertEqual(type(orgit), type(it)) - self.assertEqual(list(it), list(data)) - - if len(data): - it = pickle.loads(d) - next(it) - d = pickle.dumps(it, proto) - self.assertEqual(list(it), list(data)[1:]) + # initial iterator + itorig = iter(orig) + d = pickle.dumps((itorig, orig), proto) + it, a = pickle.loads(d) + a.fromlist(data2) + self.assertEqual(type(it), type(itorig)) + self.assertEqual(list(it), data + data2) + + # running iterator + next(itorig) + d = pickle.dumps((itorig, orig), proto) + it, a = pickle.loads(d) + a.fromlist(data2) + self.assertEqual(type(it), type(itorig)) + self.assertEqual(list(it), data[1:] + data2) + + # empty iterator + for i in range(1, len(data)): + next(itorig) + d = pickle.dumps((itorig, orig), proto) + it, a = pickle.loads(d) + a.fromlist(data2) + self.assertEqual(type(it), type(itorig)) + self.assertEqual(list(it), data2) + + # exhausted iterator + self.assertRaises(StopIteration, next, itorig) + d = pickle.dumps((itorig, orig), proto) + it, a = pickle.loads(d) + a.fromlist(data2) + self.assertEqual(type(it), type(itorig)) + self.assertEqual(list(it), data2) def test_insert(self): a = array.array(self.typecode, self.example) @@ -394,7 +427,9 @@ class BaseTest: self.assertEqual(a, b) def test_tofromstring(self): - nb_warnings = 4 + # Warnings not raised when arguments are incorrect as Argument Clinic + # handles that before the warning can be raised. + nb_warnings = 2 with warnings.catch_warnings(record=True) as r: warnings.filterwarnings("always", message=r"(to|from)string\(\) is deprecated", @@ -1039,6 +1074,11 @@ class BaseTest: a = array.array(self.typecode, "foo") a = array.array(self.typecode, array.array('u', 'foo')) + @support.cpython_only + def test_obsolete_write_lock(self): + from _testcapi import getbuffer_with_null_view + a = array.array('B', b"") + self.assertRaises(BufferError, getbuffer_with_null_view, a) class StringTest(BaseTest): |