In article <[EMAIL PROTECTED]>, Donn Cave <[EMAIL PROTECTED]> wrote:
> In article <[EMAIL PROTECTED]>, > "Martin v. Löwis" <[EMAIL PROTECTED]> wrote: > > > >> I have code like this: > > >> except Exception, e: > > >> self.setState(self.Failed, str(e)) > > >> which fails if the exception contains a unicode argument. > > > > > > Fails how? > > > > ASCII encoding error, I suppose. It fails only if a) one argument > > is a Unicode object, and b) that Unicode object contains non-ASCII > > parameters. > > Seem ironic that this fails even though pretty nearly anything > else is a valid input to str() -- socket, dict, whatever? I agree. In fact str(a) works if a is a dict or list that contains unicode data! Pity the same is not true of exceptions. Should I report this as a bug? I suspect it's a misfeature. > A sort of generic solution might be to follow str's behavior > with respect to '__str__', extending it to fall back to repr() > whatever goes wrong. > > > def xtr(a): > try: > return str(a) > except: > return repr(a) Yes. That is a more flexible alternative to what I've adopted: def exStr(ex): return ",".join([unicode(s) for s in f.args]) the latter gives better output for the case I'm interested in, but only handles exceptions. Oh well. I'll stick to my solution and hope that Python 2.6 and 3.0 fix the problem in a more sensible fashion. -- Russell
-- http://mail.python.org/mailman/listinfo/python-list