Steven Bethard wrote:
Mike Brown wrote:
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
[snip]
What am I doing
Kent Johnson wrote:
Steven Bethard wrote:
No, this is documented behavior[1]:
unicode([object[, encoding [, errors]]])
...
For objects which provide a __unicode__() method, it will call
this method without arguments to create a Unicode string. For all
other objects, the 8-bit string
Steven Bethard wrote:
Yeah, I agree it's weird. I suspect if someone supplied a patch for
this behavior it would be accepted -- I don't think this should break
backwards compatibility (much).
Notice that the right thing to do would be to pass encoding and errors
to __unicode__. If the string
Martin v. Lwis wrote:
Steven Bethard wrote:
Yeah, I agree it's weird. I suspect if someone supplied a patch for
this behavior it would be accepted -- I don't think this should break
backwards compatibility (much).
Notice that the right thing to do would be to pass encoding and errors
to
Kent Johnson wrote:
Could this be handled with a try / except in unicode()? Something like
this:
Perhaps. However, this would cause a significant performance hit, and
possbibly undesired side effects. So due process would require that the
interface of __unicode__ first, and then change the actual
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
Mike Brown wrote:
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
[snip]
What am I doing wrong? Is this a bug in