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:`fromstring`, 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.fromstring(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.fromstring(string.encode(enc))`` to append Unicode data to an array of some other type. @@ -205,30 +206,35 @@ .. method:: array.tostring() - 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; + 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. + 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: Modules/arraymodule.c =================================================================== --- Modules/arraymodule.c (revision 65708) +++ Modules/arraymodule.c (working copy) @@ -1416,10 +1416,12 @@ } PyDoc_STRVAR(fromstring_doc, -"fromstring(string)\n\ +"fromstring(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 * @@ -1434,9 +1436,9 @@ } PyDoc_STRVAR(tostring_doc, -"tostring() -> string\n\ +"tostring() -> 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.fromstring(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.tostring().decode(enc) to obtain a string from an array\n\ +of some other type."); @@ -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\ +fromstring() -- 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\ +tostring() -- return the array converted to a bytes\n\ +tounicode() -- return the array converted to a string\n\ \n\ Attributes:\n\ \n\