Index: Doc/library/array.rst =================================================================== --- Doc/library/array.rst (revision 71822) +++ Doc/library/array.rst (working copy) @@ -22,7 +22,7 @@ +-----------+----------------+-------------------+-----------------------+ | ``'B'`` | unsigned char | int | 1 | +-----------+----------------+-------------------+-----------------------+ -| ``'u'`` | Py_UNICODE | Unicode character | 2 | +| ``'u'`` | Py_UNICODE | str of length 1 | 2 | +-----------+----------------+-------------------+-----------------------+ | ``'h'`` | signed short | int | 2 | +-----------+----------------+-------------------+-----------------------+ @@ -55,10 +55,10 @@ supporting the buffer interface, or iterable over elements of the appropriate type. - If given a list or string, the initializer is passed to the new array's - :meth:`fromlist`, :meth:`fromstring`, or :meth:`fromunicode` method (see below) - to add initial items to the array. Otherwise, the iterable initializer is - passed to the :meth:`extend` method. + If given a list, bytes or str, the initializer is passed to the new array's + :meth:`fromlist`, :meth:`frombytes`, or :meth:`fromunicode` method + respectively (see below), to add initial items to the array. Otherwise, + the iterable initializer is passed to the :meth:`extend` method. .. data:: ArrayType @@ -148,17 +148,20 @@ a.append(x)`` except that if there is a type error, the array is unchanged. -.. method:: array.fromstring(s) +.. method:: array.frombytes(bytes) - Appends items from the string, interpreting the string as an array of machine - values (as if it had been read from a file using the :meth:`fromfile` method). + Appends items from a bytes object, interpreting it as an array of + machine values (as if it had been read from a file using the + :meth:`fromfile` method). + Also accepts a str, which is encoded as UTF-8 bytes. -.. method:: array.fromunicode(s) - Extends this array with data from the given unicode string. The array must +.. method:: array.fromunicode(string) + + Extends this array with data from the given string. The array must be a type ``'u'`` array; otherwise a :exc:`ValueError` is raised. Use - ``array.fromstring(unicodestring.encode(enc))`` to append Unicode data to an + ``array.frombytes(string.encode(enc))`` to append Unicode data to an array of some other type. @@ -201,18 +204,18 @@ Convert the array to an ordinary list with the same items. -.. method:: array.tostring() +.. method:: array.tobytes() - Convert the array to an array of machine values and return the string + Convert the array to an array of machine values and return the bytes representation (the same sequence of bytes that would be written to a file by the :meth:`tofile` method.) .. method:: array.tounicode() - Convert the array to a unicode string. The array must be a type ``'u'`` array; - otherwise a :exc:`ValueError` is raised. Use ``array.tostring().decode(enc)`` to - obtain a unicode string from an array of some other type. + Convert the array to a Unicode string. The array must be a type ``'u'`` array; + otherwise a :exc:`ValueError` is raised. Use ``array.tobytes().decode(enc)`` to + obtain a string from an array of some other type. When an array object is printed or converted to a string, it is represented as @@ -223,9 +226,15 @@ :func:`array` function has been imported using ``from array import array``. Examples:: + >>> array('l') array('l') - array('u', 'hello \u2641') + >>> array('b', b'hello world') + array('b', [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]) + >>> array('u', 'hello \u2641') + array('u', 'hello ♁') + >>> array('l', [1, 2, 3, 4, 5]) array('l', [1, 2, 3, 4, 5]) + >>> array('d', [1.0, 2, 3.14]) array('d', [1.0, 2.0, 3.14]) Index: Lib/wave.py =================================================================== --- Lib/wave.py (revision 71822) +++ Lib/wave.py (working copy) @@ -248,7 +248,7 @@ 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: Index: Lib/multiprocessing/managers.py =================================================================== --- Lib/multiprocessing/managers.py (revision 71822) +++ Lib/multiprocessing/managers.py (working copy) @@ -32,7 +32,7 @@ # 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')] Index: Lib/test/test_array.py =================================================================== --- Lib/test/test_array.py (revision 71822) +++ Lib/test/test_array.py (working copy) @@ -192,16 +192,16 @@ b.fromlist(a.tolist()) self.assertEqual(a, b) - def test_tofromstring(self): + 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()) self.assertEqual(a, b) if a.itemsize>1: - self.assertRaises(ValueError, b.fromstring, "x") + self.assertRaises(ValueError, b.frombytes, "x") def test_repr(self): a = array.array(self.typecode, 2*self.example) @@ -712,7 +712,7 @@ m = memoryview(a) b = bytes(m) self.assertEqual(b, a.tostring()) - self.assertEqual(b[0], a.tostring()[0]) + self.assertEqual(b[0], a.tobytes()[0]) # Resizing is forbidden when there are buffer exports self.assertRaises(BufferError, a.append, a[0]) self.assertRaises(BufferError, a.extend, a[0:1]) @@ -730,7 +730,7 @@ def test_weakref(self): s = array.array(self.typecode, self.example) p = proxy(s) - self.assertEqual(p.tostring(), s.tostring()) + self.assertEqual(p.tobytes(), s.tobytes()) s = None self.assertRaises(ReferenceError, len, p) @@ -965,7 +965,7 @@ # 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) Index: Lib/test/test_file.py =================================================================== --- Lib/test/test_file.py (revision 71822) +++ Lib/test/test_file.py (working copy) @@ -44,7 +44,7 @@ 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 @@ -282,7 +282,7 @@ 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)) Index: Lib/test/test_memoryio.py =================================================================== --- Lib/test/test_memoryio.py (revision 71822) +++ Lib/test/test_memoryio.py (working copy) @@ -373,7 +373,7 @@ 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) Index: Lib/test/test_struct.py =================================================================== --- Lib/test/test_struct.py (revision 71822) +++ Lib/test/test_struct.py (working copy) @@ -437,12 +437,12 @@ # 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. @@ -462,12 +462,12 @@ # 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. Index: Lib/sre_compile.py =================================================================== --- Lib/sre_compile.py (revision 71822) +++ Lib/sre_compile.py (working copy) @@ -343,7 +343,7 @@ 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 Index: Modules/arraymodule.c =================================================================== --- Modules/arraymodule.c (revision 71822) +++ Modules/arraymodule.c (working copy) @@ -1193,7 +1193,7 @@ /* Forward */ -static PyObject *array_fromstring(arrayobject *self, PyObject *args); +static PyObject *array_frombytes(arrayobject *self, PyObject *args); static PyObject * array_fromfile(arrayobject *self, PyObject *args) @@ -1230,7 +1230,7 @@ if (args == NULL) return NULL; - res = array_fromstring(self, args); + res = array_frombytes(self, args); Py_DECREF(args); if (res == NULL) return NULL; @@ -1351,12 +1351,12 @@ static PyObject * -array_fromstring(arrayobject *self, PyObject *args) +array_frombytes(arrayobject *self, PyObject *args) { char *str; Py_ssize_t n; int itemsize = self->ob_descr->itemsize; - if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n)) + if (!PyArg_ParseTuple(args, "s#:frombytes", &str, &n)) return NULL; if (n % itemsize != 0) { PyErr_SetString(PyExc_ValueError, @@ -1379,15 +1379,17 @@ return Py_None; } -PyDoc_STRVAR(fromstring_doc, -"fromstring(string)\n\ +PyDoc_STRVAR(frombytes_doc, +"frombytes(bytes)\n\ \n\ -Appends items from the string, interpreting it as an array of machine\n\ -values, as if it had been read from a file using the fromfile() method)."); +Appends items from the bytes object, interpreting it as an array of machine\n\ +values, as if it had been read from a file using the fromfile() method).\n\ +\n\ +Also accepts a str, which is encoded as UTF-8 bytes."); static PyObject * -array_tostring(arrayobject *self, PyObject *unused) +array_tobytes(arrayobject *self, PyObject *unused) { if (Py_SIZE(self) <= PY_SSIZE_T_MAX / self->ob_descr->itemsize) { return PyBytes_FromStringAndSize(self->ob_item, @@ -1397,10 +1399,10 @@ } } -PyDoc_STRVAR(tostring_doc, -"tostring() -> string\n\ +PyDoc_STRVAR(tobytes_doc, +"tobytes() -> bytes\n\ \n\ -Convert the array to an array of machine values and return the string\n\ +Convert the array to an array of machine values and return the bytes\n\ representation."); @@ -1434,11 +1436,11 @@ } PyDoc_STRVAR(fromunicode_doc, -"fromunicode(ustr)\n\ +"fromunicode(string)\n\ \n\ -Extends this array with data from the unicode string ustr.\n\ -The array must be a unicode type array; otherwise a ValueError\n\ -is raised. Use array.fromstring(ustr.decode(...)) to\n\ +Extends this array with data from the given string.\n\ +The array must be a type 'u' array; otherwise a ValueError\n\ +is raised. Use array.frombytes(string.encode(enc)) to\n\ append Unicode data to an array of some other type."); @@ -1456,12 +1458,12 @@ } PyDoc_STRVAR(tounicode_doc, -"tounicode() -> unicode\n\ +"tounicode() -> str\n\ \n\ -Convert the array to a unicode string. The array must be\n\ -a unicode type array; otherwise a ValueError is raised. Use\n\ -array.tostring().decode() to obtain a unicode string from\n\ -an array of some other type."); +Convert the array to a Unicode string. The array must be\n\ +a type 'u' array; otherwise a ValueError is raised. Use\n\ +array.tobytes().decode(enc) to obtain a string from an array\n\ +of some other type."); @@ -1505,8 +1507,8 @@ fromfile_doc}, {"fromlist", (PyCFunction)array_fromlist, METH_O, fromlist_doc}, - {"fromstring", (PyCFunction)array_fromstring, METH_VARARGS, - fromstring_doc}, + {"frombytes", (PyCFunction)array_frombytes, METH_VARARGS, + frombytes_doc}, {"fromunicode", (PyCFunction)array_fromunicode, METH_VARARGS, fromunicode_doc}, {"index", (PyCFunction)array_index, METH_O, @@ -1527,8 +1529,8 @@ tofile_doc}, {"tolist", (PyCFunction)array_tolist, METH_NOARGS, tolist_doc}, - {"tostring", (PyCFunction)array_tostring, METH_NOARGS, - tostring_doc}, + {"tobytes", (PyCFunction)array_tobytes, METH_NOARGS, + tobytes_doc}, {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS, tounicode_doc}, {NULL, NULL} /* sentinel */ @@ -1899,7 +1901,7 @@ Py_DECREF(a); return NULL; } - v = array_fromstring((arrayobject *)a, + v = array_frombytes((arrayobject *)a, t_initial); Py_DECREF(t_initial); if (v == NULL) { @@ -1977,7 +1979,7 @@ \n\ Return a new array whose items are restricted by typecode, and\n\ initialized from the optional initializer value, which must be a list,\n\ -string. or iterable over elements of the appropriate type.\n\ +bytes, string or iterable over elements of the appropriate type.\n\ \n\ Arrays represent basic values and behave very much like lists, except\n\ the type of objects stored in them is constrained.\n\ @@ -1990,8 +1992,9 @@ count() -- return number of occurrences of an object\n\ extend() -- extend array by appending multiple elements from an iterable\n\ fromfile() -- read items from a file object\n\ -fromlist() -- append items from the list\n\ -fromstring() -- append items from the string\n\ +fromlist() -- append items from a list\n\ +frombytes() -- append items from a bytes object\n\ +fromunicode() -- append items from a string\n\ index() -- return index of first occurrence of an object\n\ insert() -- insert a new item into the array at a provided position\n\ pop() -- remove and return item (default last)\n\ @@ -1999,7 +2002,8 @@ reverse() -- reverse the order of the items in the array\n\ tofile() -- write all items to a file object\n\ tolist() -- return the array converted to an ordinary list\n\ -tostring() -- return the array converted to a string\n\ +tobytes() -- return the array converted to a bytes\n\ +tounicode() -- return the array converted to a string\n\ \n\ Attributes:\n\ \n\