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:`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 @@ -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.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) +.. method:: array.fromunicode(string) - Extends this array with data from the given unicode string. The array must + 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. @@ -203,16 +206,16 @@ .. 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 @@ -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: Modules/arraymodule.c =================================================================== --- Modules/arraymodule.c (revision 71822) +++ Modules/arraymodule.c (working copy) @@ -1380,10 +1380,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 * @@ -1398,9 +1400,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."); @@ -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.fromstring(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.tostring().decode(enc) to obtain a string from an array\n\ +of some other type."); @@ -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\ +fromstring() -- 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\ +tostring() -- return the array converted to a bytes\n\ +tounicode() -- return the array converted to a string\n\ \n\ Attributes:\n\ \n\