Guido van Rossum wrote:
Can we get the same patch without the generic "except:", please?
<snipped part about whether there could be database corruption>

int() happens to raise a bunch of different exceptions, and I think
an unqualified except: clause is okay here (though it needs a
I think this would be a useful note for the Zope3 style guide.

What exceptions can int() raise?

On converting a preexisting value to an int:

ValueError, OverflowError, TypeError, AttributeError

(Any others?)

On converting an instance that implements __int__:

Anything at all.
It can even return a non-int value.

On evaluating the expression inside the int() brackets:

Anything at all.

I would suggest that only the four exceptions I listed first are worth catching. The other cases are programming errors.
Of those four exceptions, in this situation, I think you only need to catch ValueError. The other cases are application logic errors that I think it is counterproductive to catch. If you get one, there is a bug in some code, or some template, that should be fixed.

Here's how I produced the errors listed in the first category:

>>> int('xxx')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): xxx

>>> import sys
>>> int(sys.maxint+1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
OverflowError: long int too large to convert to int

>>> int(int)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: int() argument must be a string or a number

>>> int(AttributeError())
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: AttributeError instance has no attribute '__int__'

Steve Alexander

Zope-Dev maillist - [EMAIL PROTECTED]
** No cross posts or HTML encoding! **
(Related lists - )

Reply via email to