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
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to