Index: Doc/library/array.rst =================================================================== --- Doc/library/array.rst (revision 65708) +++ Doc/library/array.rst (working copy) @@ -18,13 +18,11 @@ +-----------+----------------+-------------------+-----------------------+ | Type code | C Type | Python Type | Minimum size in bytes | +===========+================+===================+=======================+ -| ``'c'`` | char | character | 1 | -+-----------+----------------+-------------------+-----------------------+ | ``'b'`` | signed char | int | 1 | +-----------+----------------+-------------------+-----------------------+ | ``'B'`` | unsigned char | int | 1 | +-----------+----------------+-------------------+-----------------------+ -| ``'u'`` | Py_UNICODE | Unicode character | 2 | +| ``'u'`` | Py_UNICODE | str of length 1 | 2 | +-----------+----------------+-------------------+-----------------------+ | ``'h'`` | signed short | int | 2 | +-----------+----------------+-------------------+-----------------------+ @@ -57,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 @@ -150,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. @@ -203,32 +204,37 @@ 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 ``array(typecode, initializer)``. The *initializer* is omitted if the array is -empty, otherwise it is a string if the *typecode* is ``'c'``, otherwise it is a +empty, otherwise it is a string if the *typecode* is ``'u'``, otherwise it is a list of numbers. The string is guaranteed to be able to be converted back to an array with the same type and value using :func:`eval`, so long as the :func:`array` function has been imported using ``from array import array``. Examples:: + >>> array('l') array('l') - array('c', 'hello world') - array('u', 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 65708) +++ 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 65708) +++ Lib/multiprocessing/managers.py (working copy) @@ -37,7 +37,7 @@ # def reduce_array(a): - return array.array, (a.typecode, a.tostring()) + return array.array, (a.typecode, a.tobytes()) copyreg.pickle(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 65708) +++ 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) @@ -710,12 +710,12 @@ def test_buffer(self): a = array.array(self.typecode, self.example) b = bytes(memoryview(a)) - self.assertEqual(b[0], a.tostring()[0]) + self.assertEqual(b[0], a.tobytes()[0]) 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) @@ -950,7 +950,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 65708) +++ Lib/test/test_file.py (working copy) @@ -41,7 +41,7 @@ a = array('b', b'x'*10) self.f = 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 @@ -270,7 +270,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 65708) +++ Lib/test/test_memoryio.py (working copy) @@ -334,7 +334,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 65708) +++ Lib/test/test_struct.py (working copy) @@ -504,12 +504,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. @@ -525,12 +525,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 65708) +++ Lib/sre_compile.py (working copy) @@ -349,7 +349,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 65708) +++ Modules/arraymodule.c (working copy) @@ -1214,7 +1214,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) @@ -1255,7 +1255,7 @@ if (args == NULL) return NULL; - res = array_fromstring(self, args); + res = array_frombytes(self, args); Py_DECREF(args); return res; @@ -1381,12 +1381,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, @@ -1415,15 +1415,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, @@ -1433,10 +1435,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."); @@ -1479,11 +1481,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."); @@ -1501,12 +1503,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."); @@ -1550,8 +1552,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, @@ -1572,8 +1574,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 */ @@ -1944,7 +1946,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) { @@ -2022,7 +2024,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\ @@ -2035,8 +2037,9 @@ count() -- return number of occurences 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 occurence 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\ @@ -2044,7 +2047,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\