Hi James,

Le 11 juil. 09 à 12:45, James R Eagan a écrit :

Hi Jean-Pierre,

The problem you're having is actually at the print statement at the very end.  The problem is that your sys.stdout.defaultencoding is usually set to US-ASCII rather than UTF8.  Unfortunately, the pythonic way of changing this encoding is via the site-config file, which you can't very well distribute with your python application.  You can either restrict yourself to NSLog, which does properly output UTF-8 encoded unicode text, or you can manually encode your output via:

print error.localizedDescription().encode('utf8')

This approach is better suited if you can bury that deep inside your own logging-like mechanism, since you don't want a single missed ".encode('utf8')" to introduce a unicode bug to your code.

I first thought my problem was solved, until I tried to concatenate an utf8 string to the error message returned by the NSURLConnection object, and I still have an UnicodeEncodeError exception.
The problem seems to be that the localizedDescription() function returns an ascii string containing utf-8 characters, instead of an unicode one.
The following code fails for localized error messages containing non ascii characters, returned by the system:

from Foundation import *
url = NSURL.URLWithString_("http://invalid")
request = NSURLRequest.requestWithURL_(url)
(data, response, error)= NSURLConnection.sendSynchronousRequest_returningResponse_error_(request)
print u"error: " + error.localizedDescription().encode('utf-8')

This also fails using the Terminal.

I didn't dig yet into the PyObjc bridge, but could it be a problem in the declared encoding for the NSError.localizedDescription() function ?


 - Jean-Pierre.