Re: formencode.api.Invalid class lack __unicode__ method

Ian Bicking <[email protected]> Thu, 18 Dec 2008 15:12:22 -0600
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
rabio-h7QdYz1kt/[email protected] wrote:
> Sorry for so late response.
> 
> This is rediscovery of a problem that I mention in post from
> 2008-10-07. Unfortunately, solution proposed, in my opinion, is not
> sufficient. As I highlighted in earlier post, there is no guarantee
> that ``self.msg`` is of ``unicode`` type so just returning it by
> ``__unicode__()`` is not enough.
> 
> Minimal implementation may look like::
> 
>   def __unicode__(self):                                   
>       val = self.msg
>       if isinstance(val, unicode):
>           return val
>       else:
>           return unicode(val)  
> 
> This still can be source of trouble as if ``self.msg`` will be string
> we have no information about the encoding. If it will be latin-1,
> above code will work, but in general -- and 'utf-8' in particular --
> ``UnicodeEncodeError`` will be raised again.

OK, committed in r3738.  I used a default encoding of utf8 for str being 
turned into unicode... I don't think it should come up, but I think 
it'll be a little less likely to cause regressions.  Perhaps.  And I 
like utf8 enough to let that bias my software ;)


-- 
Ian Bicking : [email protected] : http://blog.ianbicking.org

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/