Author Guillaume.Bouchard
Recipients Guillaume.Bouchard, docs@python, pitrou, r.david.murray
Date 2011-12-06.13:56:40
SpamBayes Score 2.22045e-16
Marked as misclassified No
Message-id <1323179801.38.0.915954488581.issue13538@psf.upfronthosting.co.za>
In-reply-to
Content
> str always falls back to the repr; in general str(obj) should always return some value, otherwise the assumptions of a *lot* of Python code would be broken.

Perhaps it may raises a warning ?

ie, the only reason encoding exists if for the conversion of bytes (or something which looks like bytes) to str. Do you think it may be possible to special case the use of str for bytes (and bytesarray) with something like this:

def str(object, encoding=None, errors=None):
    if encoding is not None:
         # usual work
    else:
       if isinstance(object, (bytes, bytesarray)):
             warning('Converting bytes/bytesarray to str without encoding, it may not be what you expect')
             return object.__str__()

But by the way, adding warnings and special case everywhere seems not too pythonic.

> Do you want to propose a doc patch?

The docstring for str() should looks like something like, in my frenglish way of writing english ::

  Create a new string object from the given encoded string.

  If object is bytes, bytesarray or a buffer-like object, encoding and error
  can be set. errors can be 'strict', 'replace' or 'ignore' and defaults to
  'strict'.

  WARNING, if encoding is not set, the object is converted to a nicely
  printable representation, which is totally different from what you may expect.

Perhaps a warning may be added in the on-line documentation, such as ::

  .. warning::
     When str() converts a bytes/bytesarray or a buffer-like object and
     *encoding* is not specified, the result will an unicode nicely printable
     representation, which is totally different from the unicode representation of
     you object using a specified encoding.

Whould you like a .diff on top of the current mercurial repository ?
History
Date User Action Args
2011-12-06 13:56:41Guillaume.Bouchardsetrecipients: + Guillaume.Bouchard, pitrou, r.david.murray, docs@python
2011-12-06 13:56:41Guillaume.Bouchardsetmessageid: <1323179801.38.0.915954488581.issue13538@psf.upfronthosting.co.za>
2011-12-06 13:56:40Guillaume.Bouchardlinkissue13538 messages
2011-12-06 13:56:40Guillaume.Bouchardcreate