Unicode error in exefied Python script

Tim Daneliuk tundra at tundraware.com
Mon Jan 20 09:40:07 CET 2003

Christian Bretterklieber wrote:
> Hi,
>   I have a problem with a Python script that I want to make executable
> on Win2K (I'm using ActivePython 2.2.1 in case that matters). The
> script does some database stuff. The exefied version (both with py2exe
> and Gordon McMillan's Installer) on a simple statement:
>   stmt = "UPDATE blah SET val1 = '%s', [...]" % (row[xval1],...)
>   The error message it throws is "UnicodeError: ASCII decoding error:
> Ordinal not in range (128)". Interestingly enough, the script as is
> works, just not the exefied version (The --packages encodings option to
> py2exe was given, of course).
>   I searched the web for this error message and tried some of the advice
> I found (creating a sitecustomize.py; changing the default encoding in
> site.py). None of this helped, though. What also puzzles me is the fact
> that an older version of the program works (exefied on the same system).
> The program has undergone some major changes meanwhile but the code in
> question was just copied and pasted from the old version.
>   Any ideas?
> TIA,
> c/hris.

I just finished beating a problem just like this to death. I had the
situtation where I wanted to combine the results of an lstat call with
other results from a Win32 api call (via win32all). "Combining" in this
case, meant something like:

        s = ""
        s += "My string"
        s += Win32Result
        s += "Another of my strings"
        s += Result of a ctime call
        s += Name of a file

I got the error you mentioned. After poking at it for a while, I
discovered that the Win32 API was returning *Unicode* strings, but all
my stuff was in ASCII. What was truly wierd about it is that the error
didn't get thrown until the last s += statement, even though the error
was introduced in the Win32Result statement. Python GURUS: Any idea why
this is??????

The fix was to change the offending line to:  s += str(Win32Result).

Tim Daneliuk
tundra at tundraware.com

More information about the Python-list mailing list