[Zope-dev] BUG: ValueError while changing height of the template edit window

Guido van Rossum guido@python.org
Wed, 11 Dec 2002 10:51:55 -0500


> > Anything can raise MemoryError.
> 
> Ok. But I don't think regular application code should catch these.

Correct.

> > On converting an 8bit string to an int:
> > 
> >      ValueError *only*
> 
> Ok.
> 
> > On converting a Unicode string to an int:
> > 
> >      ValueError
> >      UnicodeError (or UnicodeEncodeError, which is a subclass of it)
> 
> Can you provide an example of raising a unicode error like this:
> 
>    u = makeUnicodeString()  # your choice of function
>    int(u)

In Python 2.3, I get this:

  >>> int(u"\u1234")
  Traceback (most recent call last):
    File "<stdin>", line 1, in ?
  UnicodeEncodeError: 'decimal' codec can't encode character '\u1234' in position 0: invalid decimal Unicode string
  >>> 

In Python 2.2, this raises ValueError.

I think I may have to report this as a bug in Python 2.3 though.

> My point is that once you have a valid unicode object, I don't see how 
> calling int(valid_unicode_object) will raise a UnicodeError.
> 
> If this is so, then the style should be:
> 
>    value = expression_to_compute_value
>    try:
>        i = int(value)
>    except ValueError:
>        # take corrective action
> 
> rather than:
> 
>    try:
>        i = int(expression_to_compute_value)
>    except:  # Note: calling 'int()' can raise just about anything
>        # take corrective action

Even if we decide that we have to use a bare except after all, the
expression_to_compute_value should still be moved outside the
try/except.

--Guido van Rossum (home page: http://www.python.org/~guido/)