summaryrefslogtreecommitdiff
path: root/Lib/test
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-09-01 20:29:34 +0000
committerAntoine Pitrou <solipsis@pitrou.net>2010-09-01 20:29:34 +0000
commit1ce3eb5c5b4830e69b21865e2d723e22749544e0 (patch)
tree324241bc0190ec3316b48ae4f5bd5b20e101bcf0 /Lib/test
parent42cb4626820e466177e49c283e37e15375c3efed (diff)
downloadcpython-git-1ce3eb5c5b4830e69b21865e2d723e22749544e0.tar.gz
Issue #8990: array.fromstring() and array.tostring() get renamed to
frombytes() and tobytes(), respectively, to avoid confusion. Furthermore, array.frombytes(), array.extend() as well as the array.array() constructor now accept bytearray objects. Patch by Thomas Jollans.
Diffstat (limited to 'Lib/test')
-rwxr-xr-xLib/test/test_array.py58
-rw-r--r--Lib/test/test_file.py4
-rw-r--r--Lib/test/test_io.py2
-rw-r--r--Lib/test/test_memoryio.py2
-rw-r--r--Lib/test/test_memoryview.py2
-rw-r--r--Lib/test/test_struct.py8
6 files changed, 57 insertions, 19 deletions
diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py
index d8d4ea79c5..d7b4fa8547 100755
--- a/Lib/test/test_array.py
+++ b/Lib/test/test_array.py
@@ -11,6 +11,7 @@ import operator
import io
import math
import struct
+import warnings
import array
from array import _array_reconstructor as array_reconstructor
@@ -367,15 +368,35 @@ class BaseTest(unittest.TestCase):
self.assertEqual(a, b)
def test_tofromstring(self):
+ nb_warnings = 4
+ with warnings.catch_warnings(record=True) as r:
+ warnings.filterwarnings("always",
+ message=r"(to|from)string\(\) is deprecated",
+ category=DeprecationWarning)
+ a = array.array(self.typecode, 2*self.example)
+ b = array.array(self.typecode)
+ self.assertRaises(TypeError, a.tostring, 42)
+ self.assertRaises(TypeError, b.fromstring)
+ self.assertRaises(TypeError, b.fromstring, 42)
+ b.fromstring(a.tostring())
+ self.assertEqual(a, b)
+ if a.itemsize>1:
+ self.assertRaises(ValueError, b.fromstring, "x")
+ nb_warnings += 1
+ self.assertEqual(len(r), nb_warnings)
+
+ def test_tofrombytes(self):
a = array.array(self.typecode, 2*self.example)
b = array.array(self.typecode)
- self.assertRaises(TypeError, a.tostring, 42)
- self.assertRaises(TypeError, b.fromstring)
- self.assertRaises(TypeError, b.fromstring, 42)
- b.fromstring(a.tostring())
+ self.assertRaises(TypeError, a.tobytes, 42)
+ self.assertRaises(TypeError, b.frombytes)
+ self.assertRaises(TypeError, b.frombytes, 42)
+ b.frombytes(a.tobytes())
+ c = array.array(self.typecode, bytearray(a.tobytes()))
self.assertEqual(a, b)
+ self.assertEqual(a, c)
if a.itemsize>1:
- self.assertRaises(ValueError, b.fromstring, "x")
+ self.assertRaises(ValueError, b.frombytes, b"x")
def test_repr(self):
a = array.array(self.typecode, 2*self.example)
@@ -898,8 +919,8 @@ class BaseTest(unittest.TestCase):
a = array.array(self.typecode, self.example)
m = memoryview(a)
expected = m.tobytes()
- self.assertEqual(a.tostring(), expected)
- self.assertEqual(a.tostring()[0], expected[0])
+ self.assertEqual(a.tobytes(), expected)
+ self.assertEqual(a.tobytes()[0], expected[0])
# Resizing is forbidden when there are buffer exports.
# For issue 4509, we also check after each error that
# the array was not modified.
@@ -913,7 +934,7 @@ class BaseTest(unittest.TestCase):
self.assertEqual(m.tobytes(), expected)
self.assertRaises(BufferError, a.fromlist, a.tolist())
self.assertEqual(m.tobytes(), expected)
- self.assertRaises(BufferError, a.fromstring, a.tostring())
+ self.assertRaises(BufferError, a.frombytes, a.tobytes())
self.assertEqual(m.tobytes(), expected)
if self.typecode == 'u':
self.assertRaises(BufferError, a.fromunicode, a.tounicode())
@@ -932,7 +953,7 @@ class BaseTest(unittest.TestCase):
def test_weakref(self):
s = array.array(self.typecode, self.example)
p = weakref.proxy(s)
- self.assertEqual(p.tostring(), s.tostring())
+ self.assertEqual(p.tobytes(), s.tobytes())
s = None
self.assertRaises(ReferenceError, len, p)
@@ -1110,6 +1131,23 @@ class UnsignedNumberTest(NumberTest):
upper = int(pow(2, a.itemsize * 8)) - 1
self.check_overflow(lower, upper)
+ def test_bytes_extend(self):
+ s = bytes(self.example)
+
+ a = array.array(self.typecode, self.example)
+ a.extend(s)
+ self.assertEqual(
+ a,
+ array.array(self.typecode, self.example+self.example)
+ )
+
+ a = array.array(self.typecode, self.example)
+ a.extend(bytearray(reversed(s)))
+ self.assertEqual(
+ a,
+ array.array(self.typecode, self.example+self.example[::-1])
+ )
+
class ByteTest(SignedNumberTest):
typecode = 'b'
@@ -1172,7 +1210,7 @@ class FPTest(NumberTest):
# On alphas treating the byte swapped bit patters as
# floats/doubles results in floating point exceptions
# => compare the 8bit string values instead
- self.assertNotEqual(a.tostring(), b.tostring())
+ self.assertNotEqual(a.tobytes(), b.tobytes())
b.byteswap()
self.assertEqual(a, b)
diff --git a/Lib/test/test_file.py b/Lib/test/test_file.py
index 6d51fd547f..ebaa38beb4 100644
--- a/Lib/test/test_file.py
+++ b/Lib/test/test_file.py
@@ -44,7 +44,7 @@ class AutoFileTests(unittest.TestCase):
a = array('b', b'x'*10)
self.f = self.open(TESTFN, 'rb')
n = self.f.readinto(a)
- self.assertEquals(b'12', a.tostring()[:n])
+ self.assertEquals(b'12', a.tobytes()[:n])
def testReadinto_text(self):
# verify readinto refuses text files
@@ -281,7 +281,7 @@ class OtherFileTests(unittest.TestCase):
except ValueError:
self.fail("readinto() after next() with supposedly empty "
"iteration-buffer failed anyway")
- line = buf.tostring()
+ line = buf.tobytes()
if line != testline:
self.fail("readinto() after next() with empty buffer "
"failed. Got %r, expected %r" % (line, testline))
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index fc109f3ff6..76c85369ad 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -480,7 +480,7 @@ class IOTest(unittest.TestCase):
def test_array_writes(self):
a = array.array('i', range(10))
- n = len(a.tostring())
+ n = len(a.tobytes())
with self.open(support.TESTFN, "wb", 0) as f:
self.assertEqual(f.write(a), n)
with self.open(support.TESTFN, "wb") as f:
diff --git a/Lib/test/test_memoryio.py b/Lib/test/test_memoryio.py
index 1e7d3514e1..0decda5ebc 100644
--- a/Lib/test/test_memoryio.py
+++ b/Lib/test/test_memoryio.py
@@ -425,7 +425,7 @@ class PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin, unittest.TestCase):
a = array.array('b', b"hello world")
memio = self.ioclass(buf)
memio.readinto(a)
- self.assertEqual(a.tostring(), b"1234567890d")
+ self.assertEqual(a.tobytes(), b"1234567890d")
memio.close()
self.assertRaises(ValueError, memio.readinto, b)
diff --git a/Lib/test/test_memoryview.py b/Lib/test/test_memoryview.py
index 95071b5d8b..6ca23fc1ae 100644
--- a/Lib/test/test_memoryview.py
+++ b/Lib/test/test_memoryview.py
@@ -231,7 +231,7 @@ class BaseBytesMemoryTests(AbstractMemoryTests):
class BaseArrayMemoryTests(AbstractMemoryTests):
ro_type = None
rw_type = lambda self, b: array.array('i', list(b))
- getitem_type = lambda self, b: array.array('i', list(b)).tostring()
+ getitem_type = lambda self, b: array.array('i', list(b)).tobytes()
itemsize = array.array('i').itemsize
format = 'i'
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py
index 6ac8fdcc9c..1151662c4a 100644
--- a/Lib/test/test_struct.py
+++ b/Lib/test/test_struct.py
@@ -430,12 +430,12 @@ class StructTest(unittest.TestCase):
# Test without offset
s.pack_into(writable_buf, 0, test_string)
- from_buf = writable_buf.tostring()[:len(test_string)]
+ from_buf = writable_buf.tobytes()[:len(test_string)]
self.assertEqual(from_buf, test_string)
# Test with offset.
s.pack_into(writable_buf, 10, test_string)
- from_buf = writable_buf.tostring()[:len(test_string)+10]
+ from_buf = writable_buf.tobytes()[:len(test_string)+10]
self.assertEqual(from_buf, test_string[:10] + test_string)
# Go beyond boundaries.
@@ -458,12 +458,12 @@ class StructTest(unittest.TestCase):
# Test without offset.
pack_into(writable_buf, 0, test_string)
- from_buf = writable_buf.tostring()[:len(test_string)]
+ from_buf = writable_buf.tobytes()[:len(test_string)]
self.assertEqual(from_buf, test_string)
# Test with offset.
pack_into(writable_buf, 10, test_string)
- from_buf = writable_buf.tostring()[:len(test_string)+10]
+ from_buf = writable_buf.tobytes()[:len(test_string)+10]
self.assertEqual(from_buf, test_string[:10] + test_string)
# Go beyond boundaries.