# HG changeset patch # Parent eeb7a0d882f7edcae9580a9de8de8039b09d82ca diff -r eeb7a0d882f7 Doc/library/array.rst --- a/Doc/library/array.rst Thu Jul 01 09:32:02 2010 +0200 +++ b/Doc/library/array.rst Thu Jul 01 16:21:52 2010 +0200 @@ -60,7 +60,7 @@ 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) + :meth:`fromlist`, :meth:`frombytes`, or :meth:`fromunicode` method (see below) to add initial items to the array. Otherwise, the iterable initializer is passed to the :meth:`extend` method. @@ -152,16 +152,20 @@ .. method:: array.fromstring(s) +.. method:: array.frombytes(s) 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). +.. deprecated:: 3.2 + :meth:`fromstring` has been deprecated in favour of :meth:`frombytes` + .. method:: array.fromunicode(s) Extends this array with data from the given unicode 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(unicodestring.encode(enc))`` to append Unicode data to an array of some other type. @@ -205,16 +209,20 @@ .. 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.) +.. deprecated:: 3.2 + :meth:`tostring` has been deprecated in favour of :meth:`tobytes` + .. 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 + otherwise a :exc:`ValueError` is raised. Use ``array.tobytes().decode(enc)`` to obtain a unicode string from an array of some other type. diff -r eeb7a0d882f7 Lib/test/test_array.py --- a/Lib/test/test_array.py Thu Jul 01 09:32:02 2010 +0200 +++ b/Lib/test/test_array.py Thu Jul 01 16:21:52 2010 +0200 @@ -355,16 +355,18 @@ 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()) + 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) @@ -887,8 +889,8 @@ 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. @@ -902,7 +904,7 @@ 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()) @@ -921,7 +923,7 @@ 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) @@ -1099,6 +1101,23 @@ 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' @@ -1161,7 +1180,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) diff -r eeb7a0d882f7 Modules/arraymodule.c --- a/Modules/arraymodule.c Thu Jul 01 09:32:02 2010 +0200 +++ b/Modules/arraymodule.c Thu Jul 01 16:21:52 2010 +0200 @@ -1175,7 +1175,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) @@ -1212,7 +1212,7 @@ if (args == NULL) return NULL; - res = array_fromstring(self, args); + res = array_frombytes(self, args); Py_DECREF(args); if (res == NULL) return NULL; @@ -1331,45 +1331,83 @@ \n\ Convert array to an ordinary list with the same items."); - static PyObject * -array_fromstring(arrayobject *self, PyObject *args) +frombytes(arrayobject *self, Py_buffer *buffer) { - char *str; + int itemsize = self->ob_descr->itemsize; Py_ssize_t n; - int itemsize = self->ob_descr->itemsize; - if (!PyArg_ParseTuple(args, "s#:fromstring", &str, &n)) + if (buffer->itemsize != 1) { + PyBuffer_Release(buffer); + PyErr_SetString(PyExc_TypeError, "string/buffer of bytes required."); return NULL; + } + n = buffer->len; if (n % itemsize != 0) { + PyBuffer_Release(buffer); PyErr_SetString(PyExc_ValueError, "string length not a multiple of item size"); return NULL; } n = n / itemsize; if (n > 0) { - Py_ssize_t old_size = Py_SIZE(self); + Py_ssize_t old_size = Py_SIZE(self); if ((n > PY_SSIZE_T_MAX - old_size) || ((old_size + n) > PY_SSIZE_T_MAX / itemsize)) { + PyBuffer_Release(buffer); return PyErr_NoMemory(); } - if (array_resize(self, old_size + n) == -1) + if (array_resize(self, old_size + n) == -1) { + PyBuffer_Release(buffer); return NULL; + } memcpy(self->ob_item + old_size * itemsize, - str, n * itemsize); + buffer->buf, n * itemsize); } + PyBuffer_Release(buffer); Py_INCREF(Py_None); return Py_None; } +static PyObject * +array_fromstring(arrayobject *self, PyObject *args) +{ + Py_buffer buffer; + PyErr_WarnEx(PyExc_DeprecationWarning, + "fromstring() is deprecated. Use frombytes() instead.", 2); + if (!PyArg_ParseTuple(args, "s*:fromstring", &buffer)) + return NULL; + else + return frombytes(self, &buffer); +} + PyDoc_STRVAR(fromstring_doc, "fromstring(string)\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).\n\ +\n\ +This method is deprecated. Use frombytes instead."); + + +static PyObject * +array_frombytes(arrayobject *self, PyObject *args) +{ + Py_buffer buffer; + if (!PyArg_ParseTuple(args, "y*:frombytes", &buffer)) + return NULL; + else + return frombytes(self, &buffer); +} + +PyDoc_STRVAR(frombytes_doc, +"frombytes(bytestring)\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)."); 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, @@ -1379,13 +1417,29 @@ } } -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."); +static PyObject * +array_tostring(arrayobject *self, PyObject *unused) +{ + PyErr_WarnEx(PyExc_DeprecationWarning, + "tostring() is deprecated. Use tobytes() instead.", 2); + return array_tobytes(self, unused); +} + +PyDoc_STRVAR(tostring_doc, +"tostring() -> bytes\n\ +\n\ +Convert the array to an array of machine values and return the bytes\n\ +representation.\n\ +\n\ +This method is deprecated. Use tobytes instead."); + static PyObject * array_fromunicode(arrayobject *self, PyObject *args) @@ -1420,7 +1474,7 @@ \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\ +is raised. Use array.frombytes(ustr.decode(...)) to\n\ append Unicode data to an array of some other type."); @@ -1983,6 +2037,8 @@ 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, @@ -2005,6 +2061,8 @@ 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 */ @@ -2386,7 +2444,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) {