forcing exceptions

N

Nikola Skoric

Is there a way to tell the interpreter to display exceptions, even those
which were captured with except?
 
G

Gary Herron

Nikola said:
Is there a way to tell the interpreter to display exceptions, even those
which were captured with except?
Yes, sort of ... You have to trigger the display yourself within the
capturing "except" -- it's not automatic.

The traceback module provides a number of functions that can be used to
display a traceback of the current (or any other) exception. In
addition, it can produce a traceback of the current state of the
execution stack even without an exception:

Example (type this into the Python interpreter):

import traceback
try:
assert False
except:
traceback.print_exc()

and you'll get a traceback (a very short one in this case):

Traceback (most recent call last):
File "<stdin>", line 2, in ?
AssertionError

Gary Herron
 
P

Paul Rubin

Nikola Skoric said:
Is there a way to tell the interpreter to display exceptions, even those
which were captured with except?

Normally you wouldn't do that unless you were trying to debug the
interpreter itself. It uses caught exceptions for all sorts of things
that you probably don't want displayed. I think even ordinary loop
termination may be implemented using exceptions.
 
N

Nikola Skoric

Paul Rubin said:
Normally you wouldn't do that unless you were trying to debug the
interpreter itself. It uses caught exceptions for all sorts of things
that you probably don't want displayed. I think even ordinary loop
termination may be implemented using exceptions.

Yes, thanks for your quick responses, all three. You're right, I don't
want to debug python :) But I figured out that I don't need captured
exceptions, the thing is that I just didn't belive the problem was that
obvious. In fact, problem was in the except block, not in it's try
block. The except block had this inocent statement:

print self.sect[1].encode('utf-8')

Which results in:

Traceback (most recent call last):
File "AIDbot2.py", line 238, in ?
bot.checkNominations()
File "AIDbot2.py", line 201, in checkNominations
if sect.parseSect() == 1:
File "AIDbot2.py", line 96, in parseSect
print self.sect[1].encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 15:
ordinal
not in range(128)

Now, who can it complain about 'ascii' when I said loud and clear I want
it to encode the string to 'utf-8'??? Damn unicode.
 
R

Robert Kern

Nikola said:
Traceback (most recent call last):
File "AIDbot2.py", line 238, in ?
bot.checkNominations()
File "AIDbot2.py", line 201, in checkNominations
if sect.parseSect() == 1:
File "AIDbot2.py", line 96, in parseSect
print self.sect[1].encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 15:
ordinal
not in range(128)

Now, who can it complain about 'ascii' when I said loud and clear I want
it to encode the string to 'utf-8'??? Damn unicode.

Presumably, self.sect[1] is not a unicode string, but a regular string. The
utf-8 encoder only takes unicode strings, so it tries to convert the input into
a unicode string first. ASCII is the default encoding when no encoding is specified.

Standard practice with unicode is to only do conversions at the boundaries
between your code and interfaces that really need bytes (files, the console, the
network, etc.). Convert to unicode strings as soon you get the data and only
convert to regular strings when you have to. Everything *inside* your code
should be unicode strings, and you shouldn't try to pass around encoded regular
strings if at all possible, although pure ASCII strings generally work because
it is the default encoding.

So double-check self.sect[1] and figure out why it isn't a unicode string.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
P

Peter Otten

Nikola said:
print self.sect[1].encode('utf-8')

Which results in:

Traceback (most recent call last):
File "AIDbot2.py", line 238, in ?
bot.checkNominations()
File "AIDbot2.py", line 201, in checkNominations
if sect.parseSect() == 1:
File "AIDbot2.py", line 96, in parseSect
print self.sect[1].encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 15:
ordinal
not in range(128)

Now, who can it complain about 'ascii' when I said loud and clear I want
it to encode the string to 'utf-8'??? Damn unicode.

Trying to encode a str s results in something like

unicode(s, "ascii").encode("utf-8")

where the first step, i. e. the conversion to unicode fails for non-ascii
strings. The solution is to make that conversion explicit and provide the
proper encoding. Assuming the initial string is in latin1:

unicode(self.sect[1], "latin1").encode("utf-8")

Of course, if you can ensure that self.sect[1] is a unicode instance earlier
in your code, that would be preferable.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,108
Latest member
Virus9283

Latest Threads

Top