This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Decimal should allow leading or trailing spaces.
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 2.4
Status: closed Resolution: fixed
Dependencies: Superseder: Decimal constructor accepts newline terminated strings
View: 1780
Assigned To: Nosy List: mark.dickinson, romaia
Priority: normal Keywords:

Created on 2006-07-03 20:27 by romaia, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
decimal.diff romaia, 2006-07-03 20:27 simple patch
Messages (3)
msg29029 - (view) Author: Ronaldo Francisco Maia (romaia) Date: 2006-07-03 20:27
When creating a Decimal from a string, leading and
trailing spaces should be allowed, as it happens with
int and float:

>>> int(' 1 ')
>>> Decimal(' 1 ')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/", line 571, in
    self._sign, self._int, self._exp =
  File "/usr/lib/python2.4/", line 2267, in
    raise error, explanation

If we look at, the code (regex) is already
there, but commented.
msg57824 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2007-11-25 03:17
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

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".
msg59780 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-01-12 03:31
I spoke too soon.  This came up again in the discussion for issue #1780 
and there was general support for allowing trailing and leading 

Fixed for Python 2.6, revision 59929
Date User Action Args
2022-04-11 14:56:18adminsetgithub: 43606
2008-01-12 03:31:57mark.dickinsonsetstatus: open -> closed
resolution: fixed
superseder: Decimal constructor accepts newline terminated strings
messages: + msg59780
2007-11-25 03:17:08mark.dickinsonsettype: enhancement
messages: + msg57824
nosy: + mark.dickinson
2006-07-03 20:27:42romaiacreate