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

Gregor Horvath <gh-LfrpOBifzZo+ytmZ3SK/[email protected]>
Newsgroups gmane.comp.python.formencode
Organization Ing. Gregor Horvath, Industrieberatung & Softwareentwicklung
Message-ID <[email protected]>
Hello,

Marcin Kasperski schrieb:
> Take a look at the following example:
> 
>>from datetime import date
> import formencode
> since = formencode.All(
>     formencode.validators.DateValidator(
>         earliest_date = date(1995, 1, 1),
>         latest_date = date.today(),
>         ),
>     formencode.validators.DateConverter(
>         month_style = 'dd/mm/yyyy', not_empty=True, strip=True),
>     )
> 
> since.to_python('10/12/2001')
> # Above works correctly, returns datetime.date(2001,12,10)
> 
> since.to_python('strangetext')
> # Also works correctly, raises formencode.api.Invalid
> 
> since.to_python('10/12/1991')
> # buuuuuuum!!!!!
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "/usr/lib/python2.4/site-packages/FormEncode-0.7.1-py2.4.egg/formencode/api.py", line 368, in to_python
>     value = tp(value, state)
>   File "/usr/lib/python2.4/site-packages/FormEncode-0.7.1-py2.4.egg/formencode/compound.py", line 57, in _to_python
>     to_python)
>   File "/usr/lib/python2.4/site-packages/FormEncode-0.7.1-py2.4.egg/formencode/compound.py", line 118, in attempt_convert
>     value = validate(validator, value, state)
>   File "/usr/lib/python2.4/site-packages/FormEncode-0.7.1-py2.4.egg/formencode/compound.py", line 15, in to_python
>     return validator.to_python(value, state)
>   File "/usr/lib/python2.4/site-packages/FormEncode-0.7.1-py2.4.egg/formencode/api.py", line 371, in to_python
>     vp(value, state)
>   File "/usr/lib/python2.4/site-packages/FormEncode-0.7.1-py2.4.egg/formencode/validators.py", line 827, in validate_python
>     date_formatted = earliest_date.strftime(
> TypeError: strftime() argument 1 must be str, not unicode
> 

This seems to be a bug introduced by the fact, that internationalized
error messages now return unicode instead of str.

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.

As a workaround you can use a version < 0.7 if you don't need
internationalized failure messages.

Greg




-------------------------------------------------------------------------
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.