โžœ

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 ezio.melotti, ignas
Date 2009-02-20.06:13:57
SpamBayes Score 4.6465813e-09
Marked as misclassified No
Message-id <1235110441.07.0.42368948747.issue3446@psf.upfronthosting.co.za>
In-reply-to
Content
Indeed this behavior doesn't seem to be documented.

When the string is unicode and the fillchar non-unicode Python
implicitly tries to decode the fillchar (and possibly it raises a
TypeError if it's not in range(0,128)):
>>> u'x'.center(5, 'y') # unicode string, non-unicode (str) fillchar
u'yyxyy' # the fillchar is decoded


When the string is non-unicode it only accepts a non-unicode fillchar
(e.g. 'x'.center(5, 'y')) and it raises a TypeError if the fillchar is
unicode:
>>> 'x'.center(5, u'y') # non-unicode (str) string, unicode fillchar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: center() argument 2 must be char, not unicode

If it tries to decode the fillchar when the string is unicode, it could
also try to encode the unicode fillchar (and possibly raise a TypeError)
when the string is non-unicode.

Py3, instead, seems to have the opposite behavior. It implicitly encodes
unicode fillchars into byte strings when the string is a byte string but
it doesn't decode a byte fillchar if the string is unicode:

>>> b'x'.center(5, 'y') # byte string, unicode fillchar
b'yyxyy' # the fillchar is encoded
>>> 'x'.center(5, b'y') # unicode string, byte fillchar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: The fill character cannot be converted to Unicode

In the doc [1] there's written that "The methods on bytes and bytearray
objects donโ€™t accept strings as their arguments, just as the methods on
strings donโ€™t accept bytes as their arguments." so b'x'.center(5, 'y')
should probably raise an error on Py3 (I could open a new issue for this).

[1]:
http://docs.python.org/3.0/library/stdtypes.html#bytes-and-byte-array-methods
- In the note
History
Date User Action Args
2009-02-20 06:14:01ezio.melottisetrecipients: + ezio.melotti, ignas
2009-02-20 06:14:01ezio.melottisetmessageid: <1235110441.07.0.42368948747.issue3446@psf.upfronthosting.co.za>
2009-02-20 06:13:59ezio.melottilinkissue3446 messages
2009-02-20 06:13:57ezio.melotticreate