mirror of
https://github.com/python/cpython.git
synced 2025-10-06 07:02:33 +00:00
bpo-28298: make array 'Q', 'L' and 'I' accept big intables as elements
This commit is contained in:
parent
5fad493dc6
commit
e2c88bdd6b
3 changed files with 90 additions and 50 deletions
|
@ -1225,7 +1225,26 @@ class NumberTest(BaseTest):
|
|||
b = array.array(self.typecode, a)
|
||||
self.assertEqual(a, b)
|
||||
|
||||
class SignedNumberTest(NumberTest):
|
||||
class IntegerNumberTest(NumberTest):
|
||||
def test_type_error(self):
|
||||
a = array.array(self.typecode)
|
||||
a.append(42)
|
||||
with self.assertRaises(TypeError):
|
||||
a.append(42.0)
|
||||
with self.assertRaises(TypeError):
|
||||
a[0] = 42.0
|
||||
|
||||
class Intable:
|
||||
def __init__(self, num):
|
||||
self._num = num
|
||||
def __int__(self):
|
||||
return self._num
|
||||
def __sub__(self, other):
|
||||
return Intable(int(self) - int(other))
|
||||
def __add__(self, other):
|
||||
return Intable(int(self) + int(other))
|
||||
|
||||
class SignedNumberTest(IntegerNumberTest):
|
||||
example = [-1, 0, 1, 42, 0x7f]
|
||||
smallerexample = [-1, 0, 1, 42, 0x7e]
|
||||
biggerexample = [-1, 0, 1, 43, 0x7f]
|
||||
|
@ -1236,8 +1255,9 @@ class SignedNumberTest(NumberTest):
|
|||
lower = -1 * int(pow(2, a.itemsize * 8 - 1))
|
||||
upper = int(pow(2, a.itemsize * 8 - 1)) - 1
|
||||
self.check_overflow(lower, upper)
|
||||
self.check_overflow(Intable(lower), Intable(upper))
|
||||
|
||||
class UnsignedNumberTest(NumberTest):
|
||||
class UnsignedNumberTest(IntegerNumberTest):
|
||||
example = [0, 1, 17, 23, 42, 0xff]
|
||||
smallerexample = [0, 1, 17, 23, 42, 0xfe]
|
||||
biggerexample = [0, 1, 17, 23, 43, 0xff]
|
||||
|
@ -1248,6 +1268,7 @@ class UnsignedNumberTest(NumberTest):
|
|||
lower = 0
|
||||
upper = int(pow(2, a.itemsize * 8)) - 1
|
||||
self.check_overflow(lower, upper)
|
||||
self.check_overflow(Intable(lower), Intable(upper))
|
||||
|
||||
def test_bytes_extend(self):
|
||||
s = bytes(self.example)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue