Re: [Pyobjc-dev] NSError.localizedDescription() and Application Bundles
Brought to you by:
ronaldoussoren
From: James R E. <Jam...@lr...> - 2009-07-11 11:16:47
|
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. If anyone has any alternative suggestions, btw, I'd love to hear them. Unicode in python is often a source of headaches. Cheers! James Le 11 juil. 09 à 11:18, Jean-Pierre a écrit : > Hello, > > I am having problems to get the localized error message from an > NSError object. > > The following code runs fine when launched as a simple script in the > Terminal: > > from Foundation import * > url = NSURL.URLWithString_("http://invalid") > request = NSURLRequest.requestWithURL_(url) > (data, response, error)= > NSURLConnection > .sendSynchronousRequest_returningResponse_error_(request) > NSLog("error:%@", error.localizedDescription()) > print unicode(error.localizedDescription()) > > But fails with an UnicodeError Exception: <type > 'exceptions.UnicodeEncodeError'>: 'ascii' codec can't encode character > u'\u2019' in position 3: ordinal not in range(128) > when ran from a Cocoa-Python Application project from Xcode. > The error string has indeed an non ascii character in its fourth > position ("can’t find host"), but the type of the > error.localizedDescription() object is objc.pyobjc_unicode so it > shouldn't be a problem. > > Am I doing something wrong ? > > 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 |