Re: Problems with DateValidator (strftime raises TypeError as it gets unicode, not string)
Philip Jenvey <[email protected]>
| Newsgroups | gmane.comp.python.formencode |
|---|---|
| Message-ID | <[email protected]> |
On Sep 8, 2007, at 5:11 AM, Gregor Horvath wrote:
> 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.
>
The date_format message used here is strictly a strftime format
string, so it shouldn't even be passed through gettext. The strftime
output is used within the before/after messages, so it would be safe
to say that date_format should always be a strftime string, even when
customized. If you want to change the text surrounding the strftime
output, modify the before/after messages.
Though this gets tricky when strftime uses locale directives, because
it can return non-ascii characters. FormEncode's default date_format
string is actually using locale directives:
>>> import datetime, locale
>>> after = u"Date must be after %(date)s"
>>> date_format = "%A, %d %B %Y"
>>> locale.setlocale(locale.LC_ALL, ('de', None))
'de_DE.ISO8859-1'
>>> idesofmarch = datetime.datetime(2007, 3, 15).strftime(date_format)
>>> idesofmarch
'Donnerstag, 15 M\xe4rz 2007'
>>> after % dict(date=idesofmarch)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position
16: ordinal not in range(128)
I haven't played much with locale stuff, is
locale.getpreferredencoding(False) The Right Way (tm) to handle this?:
>>> after % dict(date=idesofmarch.decode(locale.getpreferredencoding
(False)))
u'Date must be after Donnerstag, 15 M\xe4rz 2007'
--
Philip Jenvey
-------------------------------------------------------------------------
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/