unicode(obj, errors='foo') raises TypeError - bug?

Mike Brown mike at skew.org
Wed Feb 23 01:56:30 EST 2005


This works as expected (this is on an ASCII terminal):

>>> unicode('asdf\xff', errors='replace')
u'asdf\ufffd'


This does not work as I expect it to:

>>> class C:
...   def __str__(self):
...      return 'asdf\xff'
...
>>> o = C()
>>> unicode(o, errors='replace')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: coercing to Unicode: need string or buffer, instance found



Shouldn't it work the same as calling unicode(str(self), errors='replace')?

It doesn't matter what value you use for 'errors' (ignore, replace, strict);
you'll get the same TypeError.

What am I doing wrong? Is this a bug in Python?



More information about the Python-list mailing list