[Python-Dev] Revised PEP 349: Allow str() to return unicode strings
Neil Schemenauer
nas at arctrix.com
Tue Aug 23 19:00:06 CEST 2005
On Tue, Aug 23, 2005 at 05:45:27PM +0200, Wolfgang Lipp wrote:
> i have to revise my last posting -- exporting the new ``str``
> pure-python implementation breaks -- of course! -- as soon
> as ``isinstance(x,str)`` [sic] is used
Right. I tried to come up with a pure Python version so people
could test their code. This was my latest attempt before giving
up (from memory):
# inside site.py
_old_str_new = str.__new__
def _str_new(self, s):
if type(self) not in (str, unicode):
return _old_str_new(self, s)
if type(s) not in (str, unicode):
return s
r = s.__str__()
if not isinstance(r, (str, unicode)):
raise TypeError('__str__ returned non-string')
return r
str.__new__ = _str_new
It doesn't work though:
TypeError: can't set attributes of built-in/extension type 'str'
Maybe someone else has a clever solution.
Neil
More information about the Python-Dev
mailing list