Re: Problems with DateValidator (strftime raises TypeError as it gets unicode, not string)

Marcin Kasperski <[email protected]>
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
> The failure unicode message is passed through strftime and
> unfortunatly it seems that it does not support unicode.
> I am not sure how to fix this.

Well, the patch is fairly simple. Just add .decode before messages are
passed to strftime.

I patched DateValidator in the following way

                date_formatted = earliest_date.strftime(
                    self.message('date_format', state).encode('utf-8'))

(adding this .encode everywhere strftime was called) and it now works
without problems

More sophisticated solution should probably avoid hardcoding the
encoding (although, considering this is just a date format, even ascii
should do in 99.99% of cases) but use some default.

Or, maybe, hardcode utf8, but .decode the result of strftime, so the
result is unicode string. This should work always (although I did not
test it):

                date_formatted = earliest_date.strftime(
                    self.message('date_format', state).encode('utf-8')).decode('utf-8')


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.