[New-bugs-announce] [issue4940] decimal.Decimal.__init__ should raise an instance of ValueError

rech report at bugs.python.org
Wed Jan 14 01:29:57 CET 2009


New submission from rech <r.rechkemmer at web.de>:

A common pattern is to check numeric input like this:

a = input()
try:
   int(a) # or float(a)
except ValueError:
   print("Bad input")

One would expect this to work with decimal.Decimal(a), too. But, as
Decimal.__init__ raises decimal.InvalidOperation instead of a
ValueError, it fails.

Making Decimal.__init__ raise a ValueError in this context would be an
incompatible change. Also, making decimal.InvalidOperation a subclass of
ValueError might not be correct.
It would be best to create a new an more specific exception for this
case, a subclass of both decimal.InvalidOperation and ValueError. This
would keep all functionality and enable this kind of intuitive use.

----------
components: Library (Lib)
messages: 79814
nosy: lemburg, rech
severity: normal
status: open
title: decimal.Decimal.__init__ should raise an instance of ValueError
type: behavior
versions: Python 2.4, Python 2.5, Python 2.6, Python 2.7, Python 3.0, Python 3.1

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4940>
_______________________________________


More information about the New-bugs-announce mailing list