Issue 1516613: Decimal should allow leading or trailing spaces. (original) (raw)

When creating a Decimal from a string, leading and trailing spaces should be allowed, as it happens with int and float:

int(' 1 ') 1 Decimal(' 1 ') Traceback (most recent call last): File "", line 1, in ? File "/usr/lib/python2.4/decimal.py", line 571, in new self._sign, self._int, self._exp = context._raise_error(ConversionSyntax) File "/usr/lib/python2.4/decimal.py", line 2267, in _raise_error raise error, explanation decimal.InvalidOperation

If we look at decimal.py, the code (regex) is already there, but commented.

This is a feature request rather than a bug. There's a least one good reason not to do this, namely that the specification on which the decimal module is based specifically disallows this: At

http://www2.hursley.ibm.com/decimal/daconvs.html

it says: "No blanks or other white space characters are permitted in a numeric string.", and the "to-number" operation of the specification, which converts strings to Decimal instances, is expected only to accept numeric strings.

Of course, all the specification requires is that the functionality of to- number is present in the Decimal module somewhere: currently it's implemented by Decimal.new, but it would be possible to alter Decimal.new to allow leading and trailing spaces, and have a strictly conforming to-number implementation elsewhere in the Decimal module. I don't think there's any real benefit to this. It's easy enough to add a call to strip() to the Decimal argument.

I recommend closing this issue as a "won't fix".