Menu

#8 DateValidator bug happening only in March under Windows :)

closed-fixed
nobody
None
5
2009-03-05
2009-03-05
No

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.

See attached patch.

Discussion

  • Christoph Zwerschke

    Patch for DateValidator bug

     
  • Christoph Zwerschke

    • status: open --> closed-fixed
     
  • Christoph Zwerschke

    Applied in r3801.

     

Log in to post a comment.