Re: [Pyobjc-dev] NSError.localizedDescription() and Application Bundles
Brought to you by:
ronaldoussoren
From: Ronald O. <ron...@ma...> - 2009-07-14 09:19:30
|
On 14 Jul, 2009, at 9:53, Jean-Pierre wrote: > 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 ? The bridge is fine. There are three ways to fix the error: 1) Copy a sitecustomize.py file into your application bundle (if you use an Xcode project to build the application you can just add the file to the project, using py2app adding the file to the directory containing setup.py should be enough). That file should contain the following code: import sys sys.setdefaultencoding('utf-8') 2) Explicitly encode unicode strings to UTF-8 before printing them, your print statement would end up as: print (u"error:" + error.localizedDescription()).encode('utf-8') 3) Use NSLog instead of print to print debug information: NSLog("error: %@", error.localizedDescription()) NOTE: all code was typed in Mail.app, I haven't actually tested the code. The problem you run into is that the python interpreter needs to convert a unicode string (either Python's unicode or NSString) to a sequence of bytes when you print it. The interpreter uses the default encoding for that. On the command-line the default encoding is 'utf-8', but for some reason the default encoding in an app bundle is 'ascii'. The latter encoding is a problem for you because the localizedDescription contains some characters that are not pure ASCII (most likely some fancy accent). Ronald > > Thanks, > > - Jean-Pierre. > ------------------------------------------------------------------------------ > Enter the BlackBerry Developer Challenge > This is your chance to win up to $100,000 in prizes! For a limited > time, > vendors submitting new applications to BlackBerry App World(TM) will > have > the opportunity to enter the BlackBerry Developer Challenge. See > full prize > details at: http://p.sf.net/sfu/Challenge_______________________________________________ > Pyobjc-dev mailing list > Pyo...@li... > https://lists.sourceforge.net/lists/listinfo/pyobjc-dev |