Lino Website

Fixed bugs

setlocalencoding() in no longer necessary

Monday, 9. October 2006 10:48.

Explanation for UnicodeEncodeError in Sync when exefied (06.10.06): I must never write Unicode strings directly to sys.stdout. This works only on machines where changes sys.setdefaultencoding() by calling setlocalencoding(). And py2exe does not call

On the other hand there must be a way to find the right encoding because Pythons print statement does it correctly:

#coding: latin1
import sys
print sys.version
print sys.stdout.encoding
s=u"Ännchen Müller machte große Augen"
print s

2.3.4 (#53, May 25 2004, 21:17:02) cp850
Ännchen Müller machte große Augen
Traceback (most recent call last):
  File "", line 5, in ?
    sys.stdout.write(s+"\n")#coding: latin1
UnicodeEncodeError: 'ascii' codec can't encode character u'\xc4' in position 0: ordinal not in range(128)

(Tested also with Python 2.4.1)

The solution is to encode myself every string. For most strings I did this already in Console, but Console.confirm() forwarded the unencoded message string to raw_input(). This caused an Error only when used on a machine with French locale and no setlocalencoding() during

itr("Start?", de=u"Arbeitsvorgang starten?", fr=u"Démarrer?")

Todo: some tests fail if doesn't call setlocalencoding().

Refering articles:

Copyright 2001-2007 Luc Saffre.
Generated 2007-06-07 16:22:51