Discussion:
[FE-discuss] Don't use Windows in March ;-)
Christoph Zwerschke
2009-03-05 20:35:38 UTC
Permalink
I have just stumbled over a crazy FormEncode DateValidator bug that only
appears on Windows systems in March:

----------------------------------------------------

import sys
import locale
from datetime import datetime
from formencode.validators import DateValidator

locale.setlocale(locale.LC_ALL, 'german')
print "Python", sys.version
print "Platform", sys.platform
print "Encoding", locale.getlocale()[1]
print "Month", datetime.now().strftime("%B")

# Only on a German Win XP systems in March, we get
# a UnicodeDecodeError instead of an Invalid Exception
# because Win XP uses the "cp1252" encoding
# and the month March has an Umlaut in German.

d = DateValidator(today_or_after=True)
try:
d.to_python(datetime(2000, 1, 1))
except Exception, e:
print "Error", e

----------------------------------------------------

The reason is that FormEncode tries to decode the output of strftime()
with utf-8, but Win XP uses cp1252. Now the month March has an umlaut in
German (März), so FormEncode tries to decode the cp1252 Umlaut as utf-8,
which fails with a UnicodeDecodeError.

The reason why FormEncode decodes the strftime() value is that it needs
to be merged into the messages which are Unicode (see also bug #1693684
at
http://sf.net/tracker2/index.php?func=detail&aid=1693684&group_id=91231&atid=596416).

The solution is to take the encoding from the current locale setting
that is used by strftime() instead of always assuming utf-8.

I have already submitted this to the SF Patch tracker:
http://sf.net/tracker2/?func=detail&aid=2666139&group_id=91231&atid=596418

-- Christoph

Loading...