summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-09-01 20:29:34 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-09-01 20:29:34 (GMT)
commit1ce3eb5c5b4830e69b21865e2d723e22749544e0 (patch)
tree324241bc0190ec3316b48ae4f5bd5b20e101bcf0 /Lib
parent42cb4626820e466177e49c283e37e15375c3efed (diff)
downloadcpython-1ce3eb5c5b4830e69b21865e2d723e22749544e0.zip
cpython-1ce3eb5c5b4830e69b21865e2d723e22749544e0.tar.gz
cpython-1ce3eb5c5b4830e69b21865e2d723e22749544e0.tar.bz2
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')
-rw-r--r--Lib/multiprocessing/managers.py2
-rw-r--r--Lib/sre_compile.py2
-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
-rw-r--r--Lib/wave.py2
9 files changed, 60 insertions, 22 deletions
diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py
index 8faf34e..44f749b 100644
--- a/Lib/multiprocessing/managers.py
+++ b/Lib/multiprocessing/managers.py
@@ -32,7 +32,7 @@ from multiprocessing.util import Finalize, info
#
def reduce_array(a):
- return array.array, (a.typecode, a.tostring())
+ return array.array, (a.typecode, a.tobytes())
ForkingPickler.register(array.array, reduce_array)
view_types = [type(getattr({}, name)()) for name in ('items','keys','values')]
diff --git a/Lib/sre_compile.py b/Lib/sre_compile.py
index 47e1701..f52ea01 100644
--- a/Lib/sre_compile.py
+++ b/Lib/sre_compile.py
@@ -343,7 +343,7 @@ def _optimize_unicode(charset, fixup):
else:
code = 'I'
# Convert block indices to byte array of 256 bytes
- mapping = array.array('b', mapping).tostring()
+ mapping = array.array('b', mapping).tobytes()
# Convert byte array to word array
mapping = array.array(code, mapping)
assert mapping.itemsize == _sre.CODESIZE
diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py
index d8d4ea7..d7b4fa8 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 6d51fd5..ebaa38b 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 fc109f3..76c8536 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 1e7d351..0decda5 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 95071b5..6ca23fc 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 6ac8fdc..1151662 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.
diff --git a/Lib/wave.py b/Lib/wave.py
index 950d8e2..57f9d17 100644
--- a/Lib/wave.py
+++ b/Lib/wave.py
@@ -248,7 +248,7 @@ class Wave_read:
chunk = chunk.file
chunk.size_read = chunk.size_read + nitems * self._sampwidth
data.byteswap()
- data = data.tostring()
+ data = data.tobytes()
else:
data = self._data_chunk.read(nframes * self._framesize)
if self._convert and data: