Re: Simple unicode-safe version of str(exception)?

2008-04-29 Thread Bjoern Schliessmann
Martin v. Löwis wrote:

 e is an exception object, not a Unicode object.

Er, sure, thanks for pointing that out. At first sight he should
substitute e with e.message then since he tries to convert to
string (for display?).

Regards,


Björn

-- 
BOFH excuse #366:

ATM cell has no roaming feature turned on, notebooks can't connect

--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple unicode-safe version of str(exception)?

2008-04-29 Thread Russell E. Owen
In article [EMAIL PROTECTED],
 Bjoern Schliessmann [EMAIL PROTECTED] 
 wrote:

 Martin v. Löwis wrote:
 
  e is an exception object, not a Unicode object.
 
 Er, sure, thanks for pointing that out. At first sight he should
 substitute e with e.message then since he tries to convert to
 string (for display?).

No. e.message is only set if the exeption object receives exactly one 
argument.

That is why my replacement code reads:
errStr = ,.join([unicode(s) for s in f.args])
self.setState(self.Failed, errStr)
(where e is an Exception of some kind)

Notice that I'm iterating over the args. But again, this is far messier 
than:
self.setState(self.Failed, str(e)

So...to repeat the original question, is there any simpler unicode-safe 
replacement for str(exception)?

-- Russell
--
http://mail.python.org/mailman/listinfo/python-list

Re: Simple unicode-safe version of str(exception)?

2008-04-29 Thread Russell E. Owen
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

Re: Simple unicode-safe version of str(exception)?

2008-04-29 Thread Torsten Bronger
Hallöchen!

Russell E. Owen writes:

 [...]

 So...to repeat the original question, is there any simpler
 unicode-safe replacement for str(exception)?

Please show us the tracebacks you get becuae unicode(s) must fail,
too, if there are non-ASCII characters involved.

Tschö,
Torsten.

-- 
Torsten Bronger, aquisgrana, europa vetus
  Jabber ID: [EMAIL PROTECTED]
   (See http://ime.webhop.org for further contact info.)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple unicode-safe version of str(exception)?

2008-04-29 Thread Sion Arrowsmith
Russell E. Owen [EMAIL PROTECTED] wrote:
No. e.message is only set if the exeption object receives exactly one 
argument.

And not always then:

 e1 = Exception(u\u00fe)
 e1.message
Traceback (most recent call last):
  File stdin, line 1, in ?
AttributeError: Exception instance has no attribute 'message'

That is why my replacement code reads:
errStr = ,.join([unicode(s) for s in f.args])

errStr = ,.join(e.args)

There is something distinctly odd going on here, though:

 str(e1)
Traceback (most recent call last):
  File stdin, line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfe' in position 0: 
ordinal not in range(128)
 e2 = Exception(u\u00fe, foo)
 str(e2)
(u'\\xfe', 'foo')
 

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
   Frankly I have no feelings towards penguins one way or the other
-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
--
http://mail.python.org/mailman/listinfo/python-list

Re: Simple unicode-safe version of str(exception)?

2008-04-29 Thread Russell E. Owen
In article [EMAIL PROTECTED],
 Torsten Bronger [EMAIL PROTECTED] wrote:

 Hallöchen!
 
 Russell E. Owen writes:
 
  [...]
 
  So...to repeat the original question, is there any simpler
  unicode-safe replacement for str(exception)?
 
 Please show us the tracebacks you get becuae unicode(s) must fail,
 too, if there are non-ASCII characters involved.

Why?

What I am trying to do is get a unicode representation of the arguments 
of an exception. str(e), the obvious solution, fails if the exception 
contains one argument that is a unicode string that cannot be decoded to 
ASCII:

UnicodeEncodeError: 'ascii' codec can't encode character...ordinal not 
in range(128)

What I do with the resulting unicode string afterwards is a different 
issue. (As a matter of fact I display it on a widget that can display 
unicode, but if I tried to print it to my Mac Terminal it would complain 
about the non-ASCII characters--something I should look into fixing 
someday).

But in any case, here are the exceptions you wanted to see. This is 
using Python 2.5.2 on a Mac:

 d =u\N{DEGREE SIGN}
 d
u'\xb0'
 str(d)
Traceback (most recent call last):
  File stdin, line 1, in module
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in 
position 0: ordinal not in range(128)
 e = Exception(d)
 str(e)
Traceback (most recent call last):
  File stdin, line 1, in module
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in 
position 0: ordinal not in range(128)
 e
Exception(u'\xb0',)
 ,.join([unicode(s) for s in e.args])
u'\xb0'
 

Based on the dearth of better replacements I've gone with the solution 
that is shown as the last line above, coded as the following function 
(incorporating Donn Cave's excellent suggestion to use repr as a 
fallback). It has the minor issue that it can mask KeyboardInterrupt on 
older versions of Python but it's close enough. A lot of work to replace 
str(exc).

def strFromException(exc):
Unicode-safe replacement for str(exception)
try:
return str(exc)
except Exception:
try:
return ,.join([unicode(s) for s in exc.args])
except Exception:
# in case exc is some unexpected type
return repr(exc)

-- Russell
--
http://mail.python.org/mailman/listinfo/python-list

Re: Simple unicode-safe version of str(exception)?

2008-04-29 Thread Torsten Bronger
Hallöchen!

Russell E. Owen writes:

 Torsten Bronger [EMAIL PROTECTED] wrote:

 Russell E. Owen writes:
 
 [...]

 So...to repeat the original question, is there any simpler
 unicode-safe replacement for str(exception)?
 
 Please show us the tracebacks you get becuae unicode(s) must
 fail, too, if there are non-ASCII characters involved.

 Why?

 [...]

 ,.join([unicode(s) for s in e.args])
 u'\xb0'

I thought you wanted to have a string.  But this is again a unicode.

Tschö,
Torsten.

-- 
Torsten Bronger, aquisgrana, europa vetus
  Jabber ID: [EMAIL PROTECTED]
   (See http://ime.webhop.org for further contact info.)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple unicode-safe version of str(exception)?

2008-04-29 Thread Martin v. Löwis
 Should I report this as a bug? I suspect it's a misfeature.

Please no. There isn't much that can be done about it, IMO.

Regards,
Martin
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple unicode-safe version of str(exception)?

2008-04-29 Thread Torsten Bronger
Hallöchen!

Martin v. Löwis writes:

 Should I report this as a bug? I suspect it's a misfeature.

 Please no. There isn't much that can be done about it, IMO.

One could give Exception a __unicode__ method.  On the other hand,
this kind of conversion of an exception to something printable is a
quite ugly kludge anyway in my opinion, so it needn't special
support.

Tschö,
Torsten.

-- 
Torsten Bronger, aquisgrana, europa vetus
  Jabber ID: [EMAIL PROTECTED]
   (See http://ime.webhop.org for further contact info.)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple unicode-safe version of str(exception)?

2008-04-29 Thread Jim
I often struggle with the problem outlined in part in this thread.  I
know that I'm going to repeat some of what is said elsewhere but I'd
like to present the question all in one place.

I believe that the routines in the Python standard library do not
document which exceptions they could raise (I understand some reasons
why, but they nontheless do not).  So I often find out the hard way
that a library call can raise an exception that I was not smart enough
to anticipate.  So I often find myself doing
  try:
 make_a_routine_call(x,y,z)
  except AnticipatedError, err:
 do_something_with_it(x,y,z,err)
  exception Exception, err:
 print something crazy happened +str(err)
where the print statement is just supposed to give me some idea of
what happened.  (I actually usually log it instead of printing it.  If
there is a better way, I'd love to hear it.)  But str(err) sometimes
fails, as the OP noted.

Further, xtr(err) alone is not enough, I believe.  I believe that some
routines in the standard library do not return strings (by that I mean
something approximately like err.value is a pair; forgive me, I
don't remember the full details).

Surely as lovely a language as Python has a better idiom for dealing
with exceptions in the standard library than something that works out
to
  print something crazy happened+ .join([xtra(x) for x in err])
?  (I would understand better if it was a user's code; they are free
to do what works for them, of course.)  But I've been unable to think
of one and I haven't seen in this thread another direction.  Is there
one?

Jim
--
http://mail.python.org/mailman/listinfo/python-list


Simple unicode-safe version of str(exception)?

2008-04-28 Thread Russell E. Owen
I have code like this:
except Exception, e:
   self.setState(self.Failed, str(e))
which fails if the exception contains a unicode argument.

I did, of course, try unicode(e) but that fails.

The following works, but seems rather messy:

except Exception, e:
errStr = ,.join([unicode(s) for s in f.args])
self.setState(self.Failed, errStr)

Is there a simpler solution that works in Python 2.3-2.5?

-- Russell
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple unicode-safe version of str(exception)?

2008-04-28 Thread Bjoern Schliessmann
Russell E. Owen 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?
 
 I did, of course, try unicode(e) but that fails.

Converting unicode to unicode doesn't help. Instead of just e, try
using e.encode(some-encoding) where some-encoding should be your
system's default encoding like utf-8 or iso8859-1.

Regards,


Björn

-- 
BOFH excuse #434:

Please state the nature of the technical emergency

--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple unicode-safe version of str(exception)?

2008-04-28 Thread Martin v. Löwis
 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.

 I did, of course, try unicode(e) but that fails.
 
 Converting unicode to unicode doesn't help.

e is an exception object, not a Unicode object.

Regards,
Martin

--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple unicode-safe version of str(exception)?

2008-04-28 Thread Donn Cave
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?

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)

...
self.setState(self.Failed, xtr(e))

   Donn Cave, [EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list