Re: formencode.api.Invalid class lack _ _unicode__ method
rabio-h7QdYz1kt/[email protected] <rabio-h7QdYz1kt/[email protected]> Thu, 18 Dec 2008 19:00:05 +0100
| Newsgroups | gmane.comp.python.formencode |
|---|---|
| Message-ID | <[email protected]> |
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.
It can be solved in a way I described in earlier post or by requiring
that ``msg`` argument for ``formencode.api.Invalid`` be of ``unicode``
type and checking/converting it in ``Invalid.__init__()``. In latter
case if problem with encoding occur it will be easy to find and solve
by user just by casting ``msg`` argument to unicode with proper
encoding.
Regards, Krzysiek
> I've noticed that error messages which are unicode objects give the
> following error when used in htmlfill:
>
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u0119' in
> position 5: ordinal not in range(128)
>
> In rewritingparser.html_quote there is a fragment which checks if
> given object has __unicode__ attribute:
>
> (...)
> else:
> if hasattr(v, '__unicode__'):
> v = unicode(v)
>
> but objects of 'formencode.api.Invalid' class don't have it, so the following:
> (...)
> else:
> v = str(v)
>
> fails with UnicodeDecodeError.
>
> Adding:
>
> def __unicode__(self):
> val = self.msg
> return val
>
> to formencode.api.Invalid fixes the problem for me.
>
> Regards,
> Marcin
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
> Build the coolest Linux based applications with Moblin SDK & win great prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> FormEncode-discuss mailing list
> FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> https://lists.sourceforge.net/lists/listinfo/formencode-discuss
>
------------------------------------------------------------------------------
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/