diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -1214,35 +1214,43 @@ .. _func-str: .. function:: str(object='') - str(object[, encoding[, errors]]) + str(bytes, encoding[, errors='strict']) + str(bytes, errors) Return a :ref:`string ` version of an object, using one of the - following modes: + following two modes: - If *encoding* and/or *errors* are given, :func:`str` will decode the - *object* which can either be a byte string or a character buffer using - the codec for *encoding*. The *encoding* parameter is a string giving - the name of an encoding; if the encoding is not known, :exc:`LookupError` - is raised. Error handling is done according to *errors*; this specifies the - treatment of characters which are invalid in the input encoding. If - *errors* is ``'strict'`` (the default), a :exc:`ValueError` is raised on - errors, while a value of ``'ignore'`` causes errors to be silently ignored, - and a value of ``'replace'`` causes the official Unicode replacement character, - U+FFFD, to be used to replace input characters which cannot be decoded. - See also the :mod:`codecs` module. + If no arguments are given, :func:`str` returns the empty string. If only + *object* is given, ``str(object)`` returns a nicely printable + representation of *object*. For strings, this is the string itself. + This behavior differs from :func:`repr` in that the return value of + ``str(object)`` is not usually meant for passing to :func:`eval`. Rather, + its goal is to return a printable string. One can control the return value + for an object by defining the :meth:`__str__` method of *object*. - When only *object* is given, this returns its nicely printable representation. - For strings, this is the string itself. The difference with ``repr(object)`` - is that ``str(object)`` does not always attempt to return a string that is - acceptable to :func:`eval`; its goal is to return a printable string. - With no arguments, this returns the empty string. + If *encoding* or *errors* is given, the object argument should be a + :class:`bytes` object or :ref:`bytes sequence `. In this + case, :func:`str` decodes the object using the :ref:`encoding + ` named by the *encoding* argument. If the encoding + is not known, a :exc:`LookupError` is raised. The *errors* argument + specifies how to handle characters that are invalid for *encoding*. If + *errors* is ``'strict'`` (the default), :exc:`ValueError` is raised on + errors. A value of ``'ignore'`` causes errors to be silently ignored. + A value of ``'replace'`` causes the official Unicode replacement character, + U+FFFD, to replace invalid characters. Also see the :mod:`codecs` module. - Objects can specify what ``str(object)`` returns by defining a :meth:`__str__` - special method. + Passing a :func:`bytes ` object without the *encoding* or *errors* + arguments uses the first mode (see also the :option:`-b` command-line + option to Python). For example:: - For more information on strings and string methods, see the :ref:`textseq` - section. To output formatted strings, see the :ref:`string-formatting` - section. In addition, see the :ref:`stringservices` section. + >>> str(b'Zoot!') + "b'Zoot!'" + + The :class:`str` object is the string class. For more information on + strings and string methods, see the :ref:`textseq` and + :ref:`string-methods` sections. To output formatted strings, see the + :ref:`string-formatting` section. In addition, see the + :ref:`stringservices` section. .. function:: sum(iterable[, start]) diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -1286,6 +1286,7 @@ self.assertRaises(TypeError, setattr, sys, 1, 'spam') self.assertRaises(TypeError, setattr) + # test_str(): see test_unicode.py and test_bytes.py for str() tests. def test_sum(self): self.assertEqual(sum([]), 0) diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -1155,6 +1154,12 @@ self.assertRaises(TypeError, str, 42, 42, 42) + def test_constructor_args(self): + """Test various combinations of positional and keyword arguments.""" + self.assertEqual(str(), "") + # The errors argument without encoding triggers decode mode. + self.assertEqual(str(b'foo', errors="strict"), "foo") + def test_codecs_utf7(self): utfTests = [ ('A\u2262\u0391.', b'A+ImIDkQ.'), # RFC2152 example