diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/zope/schema/_bootstrapfields.py | 11 | ||||
| -rw-r--r-- | src/zope/schema/_field.py | 9 |
2 files changed, 17 insertions, 3 deletions
diff --git a/src/zope/schema/_bootstrapfields.py b/src/zope/schema/_bootstrapfields.py index c96c7d8..6501310 100644 --- a/src/zope/schema/_bootstrapfields.py +++ b/src/zope/schema/_bootstrapfields.py @@ -19,6 +19,7 @@ from zope.interface import Attribute from zope.interface import providedBy from zope.interface import implementer +from zope.schema._bootstrapinterfaces import ValidationError from zope.schema._bootstrapinterfaces import ConstraintNotSatisfied from zope.schema._bootstrapinterfaces import IContextAwareDefaultFactory from zope.schema._bootstrapinterfaces import IFromUnicode @@ -440,6 +441,9 @@ class Bool(Field): self.validate(v) return v +class InvalidIntLiteral(ValueError, ValidationError): + """Invalid int literal.""" + @implementer(IFromUnicode) class Int(Orderable, Field): @@ -458,8 +462,11 @@ class Int(Orderable, Field): >>> f.fromUnicode("125.6") #doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... - ValueError: invalid literal for int(): 125.6 + InvalidIntLiteral: invalid literal for int(): 125.6 """ - v = int(str) + try: + v = int(str) + except ValueError as v: + raise InvalidIntLiteral(*v.args).with_field_and_value(self, str) self.validate(v) return v diff --git a/src/zope/schema/_field.py b/src/zope/schema/_field.py index 21edeb6..807ca48 100644 --- a/src/zope/schema/_field.py +++ b/src/zope/schema/_field.py @@ -185,6 +185,10 @@ class ASCIILine(ASCII): return '\n' not in value +class InvalidFloatLiteral(ValueError, ValidationError): + """Raised by Float fields.""" + + @implementer(IFloat, IFromUnicode) class Float(Orderable, Field): __doc__ = IFloat.__doc__ @@ -196,7 +200,10 @@ class Float(Orderable, Field): def fromUnicode(self, uc): """ See IFromUnicode. """ - v = float(uc) + try: + v = float(uc) + except ValueError as v: + raise InvalidFloatLiteral(*v.args).with_field_and_value(self, uc) self.validate(v) return v |
