This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ezio.melotti
Recipients Arfrever, chris.jerdonek, docs@python, eric.smith, ezio.melotti
Date 2012-09-23.11:15:10
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1348398911.27.0.679749148325.issue15952@psf.upfronthosting.co.za>
In-reply-to
Content
``format(value, format_spec)`` merely calls
-      ``value.__format__(format_spec)``.
+      ``value.__format__(format_spec)`` and, if *format_spec* is Unicode,
+      converts the value to Unicode if it is not already Unicode.

This is correct, but should be rephrased (and "value" should be "return value").

+      The method ``value.__format__(format_spec)`` may return 8-bit strings
+      for some built-in types when *format_spec* is Unicode.

This is not limited to built-in types.  __format__() might return either str or unicode, and format() returns the same -- except for the aforementioned case.


This is a summary of the possible cases.


__format__ can return unicode or str:

  >>> class Uni(object):
  ...   def __format__(*args): return u'uni'
  ... 
  >>> class Str(object):
  ...   def __format__(*args): return 'str'
  ... 


format() and __format__ return the same value, except when the format_spec is unicode and __format__ returns str:

  >>> format(Uni(),  'd'),  Uni().__format__( 'd')  # same
  (u'uni', u'uni')
  >>> format(Uni(), u'd'),  Uni().__format__(u'd')  # same
  (u'uni', u'uni')
  >>> format(Str(),  'd'),  Str().__format__( 'd')  # same
  ('str', 'str')
  >>> format(Str(), u'd'),  Str().__format__(u'd')  # different
  (u'str', 'str')

It is also not true that the type of return value is the same of the format_spec, because in the first case the returned type is unicode even if the format_spec is str.  Therefore this part of the patch should be changed:

+   Per :pep:`3101`, the function returns a Unicode object if *format_spec* is
+   Unicode.  Otherwise, it returns an 8-bit string.

The behavior might be against PEP 3101 (see quotation in msg170669), even thought the wording of the PEP is somewhat lenient IMHO ("proper type" doesn't necessary mean "same type").
History
Date User Action Args
2012-09-23 11:15:11ezio.melottisetrecipients: + ezio.melotti, eric.smith, Arfrever, chris.jerdonek, docs@python
2012-09-23 11:15:11ezio.melottisetmessageid: <1348398911.27.0.679749148325.issue15952@psf.upfronthosting.co.za>
2012-09-23 11:15:10ezio.melottilinkissue15952 messages
2012-09-23 11:15:10ezio.melotticreate