Discussion:
[FE-discuss] Possible bug with Number validator
Aston Motes
2009-04-08 00:56:35 UTC
Permalink
I generally expect the Number validator to either return a number
(float or int) or raise an Invalid exception. However, if I do the
following:

from formencode.validators import Number
Number.to_python('inf')

I get an OverflowError related to 'inf' being interpreted as the float
infinity. The same thing happens with 'infinity' as the input string.

Is this a for reals bug with FormEncode, or just a corner case
exception I should be aware of and catch?

Thanks,
- Aston
Ian Bicking
2009-04-08 01:55:06 UTC
Permalink
Yes, probably it would be reasonable behavior for 'inf' to be invalid,
except if some settings was on the Number validator to allow it (like
Number(allow_infinity=True)). But at the moment infinity is allowed and
actually tested for. I guess at some point I was convinced it would be a
good idea to just accept it.
Post by Aston Motes
I generally expect the Number validator to either return a number
(float or int) or raise an Invalid exception. However, if I do the
from formencode.validators import Number
Number.to_python('inf')
I get an OverflowError related to 'inf' being interpreted as the float
infinity. The same thing happens with 'infinity' as the input string.
Is this a for reals bug with FormEncode, or just a corner case
exception I should be aware of and catch?
Thanks,
- Aston
------------------------------------------------------------------------------
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
FormEncode-discuss mailing list
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
--
Ian Bicking | http://blog.ianbicking.org
Aston Motes
2009-04-08 02:09:17 UTC
Permalink
I don't (really) mind infinity being accepted; it's just weird that
the validator ends up raising an exception no part of the stack is
expecting. It pretty much makes using the validate decorator
impossible without wrapping it up with my own thing.

Or maybe you're saying infinity works correctly (no exception raised)
in the new version of formencode? I'm still on 0.7.1.

- Aston
Post by Ian Bicking
Yes, probably it would be reasonable behavior for 'inf' to be invalid,
except if some settings was on the Number validator to allow it (like
Number(allow_infinity=True)).  But at the moment infinity is allowed and
actually tested for.  I guess at some point I was convinced it would be a
good idea to just accept it.
Post by Aston Motes
I generally expect the Number validator to either return a number
(float or int) or raise an Invalid exception. However, if I do the
from formencode.validators import Number
Number.to_python('inf')
I get an OverflowError related to 'inf' being interpreted as the float
infinity. The same thing happens with 'infinity' as the input string.
Is this a for reals bug with FormEncode, or just a corner case
exception I should be aware of and catch?
Thanks,
  - Aston
------------------------------------------------------------------------------
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
FormEncode-discuss mailing list
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
--
Ian Bicking  |  http://blog.ianbicking.org
Ian Bicking
2009-04-08 02:41:51 UTC
Permalink
Post by Aston Motes
I don't (really) mind infinity being accepted; it's just weird that
the validator ends up raising an exception no part of the stack is
expecting. It pretty much makes using the validate decorator
impossible without wrapping it up with my own thing.
Or maybe you're saying infinity works correctly (no exception raised)
in the new version of formencode? I'm still on 0.7.1.
I didn't realize it would raise an exception... where is the exception
coming from?
--
Ian Bicking | http://blog.ianbicking.org
Aston Motes
2009-04-08 02:55:10 UTC
Permalink
int(float('inf')) raises an OverflowException. The problematic lines
are something like

value = float(value)
if value == int(value):

They're lines 956 and 957 in my formencode/validators.py, but I've got
old stuff.

- Aston
Post by Ian Bicking
Post by Aston Motes
I don't (really) mind infinity being accepted; it's just weird that
the validator ends up raising an exception no part of the stack is
expecting. It pretty much makes using the validate decorator
impossible without wrapping it up with my own thing.
Or maybe you're saying infinity works correctly (no exception raised)
in the new version of formencode? I'm still on 0.7.1.
I didn't realize it would raise an exception... where is the exception
coming from?
--
Ian Bicking  |  http://blog.ianbicking.org
Christoph Zwerschke
2009-04-09 17:35:24 UTC
Permalink
Post by Aston Motes
int(float('inf')) raises an OverflowException. The problematic lines
are something like
value = float(value)
That has been fixed already 6 months ago in r3657 by catching the
possible OverflowError here:

value = float(value)
try:
int_value = int(value)
except OverflowError:
int_value = None
if value == int_value:
return int_value
return value

However, I think this comparison also does not work when you have really
Post by Aston Motes
print Number().to_python(1e33)
999999999999999945575230987042816

This is probably not what we want. Maybe the following is better:

if value == int_value and '%.f' % value == str(int_value):
return int_value

-- Christoph

Loading...