Thread: [Pyobjc-dev] NSError.localizedDescription() and Application Bundles
Brought to you by:
ronaldoussoren
From: Jean-Pierre <cho...@fr...> - 2009-07-11 09:31:02
|
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. |
From: James R E. <Jam...@lr...> - 2009-07-11 11:16:47
Attachments:
smime.p7s
|
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 |
From: Jean-Pierre <cho...@fr...> - 2009-07-14 08:51:24
|
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 ? Thanks, - Jean-Pierre. |
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 |
From: Ronald O. <ron...@ma...> - 2009-07-14 13:46:13
Attachments:
LocalizedAppDelegate.py
|
The attached version of app delegate should work. You should not use "decode('utf-8')" on unicode strings that you use with Cocoa API's. Ronald On 14 Jul, 2009, at 15:34, Jean-Pierre wrote: > Le 14 juil. 09 à 13:52, Ronald Oussoren a écrit : > >> If you want to display the message in a window you should be able >> use it as is, that is use aTextField.setStringValue_ >> (error.localizedDescription()) should work just fine. > > That's what I am trying to do indeed. > >> If that doesn't work: could you please create a small program that >> shows the problem (that is, a complete Xcode project)? >> >> Ronald > > Here you are, a small Xcode 3 project with just a Text Label > displaying the error message from an NSURLConnection failure. > > - Jean-Pierre. > > <Localized.zip> > > |
From: Jean-Pierre <cho...@fr...> - 2009-07-14 14:00:42
|
Le 14 juil. 09 à 15:45, Ronald Oussoren a écrit : > The attached version of app delegate should work. You should not use > "decode('utf-8')" on unicode strings that you use with Cocoa API's. > > Ronald Ok, I missed the fact that I had to convert the string only to print it to stdout, everything works fine now. Thanks. Best regards, - Jean-Pierre. |
From: Jean-Pierre <cho...@fr...> - 2009-07-14 13:50:13
Attachments:
Localized.zip
|
Le 14 juil. 09 à 13:52, Ronald Oussoren a écrit : > If you want to display the message in a window you should be able > use it as is, that is use > aTextField.setStringValue_(error.localizedDescription()) should work > just fine. That's what I am trying to do indeed. > If that doesn't work: could you please create a small program that > shows the problem (that is, a complete Xcode project)? > > Ronald Here you are, a small Xcode 3 project with just a Text Label displaying the error message from an NSURLConnection failure. - Jean-Pierre. |