From: Benny M. <ben...@gm...> - 2010-10-07 11:26:49
|
2010/10/7 Peter Landgren <pet...@te...> > Hi, > > In argparser.parse_args error handling part > msg is an error message generated by > except getopt.GetoptError, msg: > If do str(msg) and unicode(msg) I get two very different results in > Windows. > In Linux I get the same > Windows: > str= 25 'option -\xc3\x84 not recognized' > uni= 41 u"(u'option -\\xc4 not recognized', u'\\xc4')" > getopt recieves sys.argv, and getopt.py does no conversion. So the format is in the format of sys.argv, which you convert to srt and unicode. Have a look in getopt.py (do locate on your linux box to find it), you see there GetoptError has self.msg and self.opt, and has an __str__ method. That is, it seems that msg = GetoptError, and in windows and linux doing str calls the __str__ method of the error. However doing unicode falls back to __str__ method in linux, but not in windows. There the unicode gives you a tuple of self.msg and self.opt: (u'option -\\xc4 not recognized', u'\\xc4') Conclusion, you are not allowed to call unicode on a GetOptError, as that is not defined and it depends on the interpreter how it is handled. You may only call str(...) on objects of type Error. Benny |