mirror of
https://github.com/python/cpython.git
synced 2025-10-06 15:11:58 +00:00
[3.6] bpo-28298: make array 'Q', 'L' and 'I' accept big intables as elements (#579)
This commit is contained in:
parent
9cef253ae3
commit
26d013e00f
3 changed files with 91 additions and 63 deletions
|
@ -14,14 +14,6 @@ import warnings
|
|||
import array
|
||||
from array import _array_reconstructor as array_reconstructor
|
||||
|
||||
try:
|
||||
# Try to determine availability of long long independently
|
||||
# of the array module under test
|
||||
struct.calcsize('@q')
|
||||
have_long_long = True
|
||||
except struct.error:
|
||||
have_long_long = False
|
||||
|
||||
sizeof_wchar = array.array('u').itemsize
|
||||
|
||||
|
||||
|
@ -32,9 +24,7 @@ class ArraySubclassWithKwargs(array.array):
|
|||
def __init__(self, typecode, newarg=None):
|
||||
array.array.__init__(self)
|
||||
|
||||
typecodes = "ubBhHiIlLfd"
|
||||
if have_long_long:
|
||||
typecodes += 'qQ'
|
||||
typecodes = 'ubBhHiIlLfdqQ'
|
||||
|
||||
class MiscTest(unittest.TestCase):
|
||||
|
||||
|
@ -1240,7 +1230,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]
|
||||
|
@ -1251,8 +1260,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]
|
||||
|
@ -1263,6 +1273,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)
|
||||
|
@ -1314,12 +1325,10 @@ class UnsignedLongTest(UnsignedNumberTest, unittest.TestCase):
|
|||
typecode = 'L'
|
||||
minitemsize = 4
|
||||
|
||||
@unittest.skipIf(not have_long_long, 'need long long support')
|
||||
class LongLongTest(SignedNumberTest, unittest.TestCase):
|
||||
typecode = 'q'
|
||||
minitemsize = 8
|
||||
|
||||
@unittest.skipIf(not have_long_long, 'need long long support')
|
||||
class UnsignedLongLongTest(UnsignedNumberTest, unittest.TestCase):
|
||||
typecode = 'Q'
|
||||
minitemsize = 8
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue