summaryrefslogtreecommitdiffstats
path: root/Lib/decimal.py
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2008-01-12 01:56:00 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2008-01-12 01:56:00 (GMT)
commit59bc20bb273055e2cd48a467524d21340c1771cc (patch)
tree5c56af6a822827d080e136a8f9ee819ee589d4af /Lib/decimal.py
parentbed4dd459ddebec5bf43ef8c658ed4a194b518cb (diff)
downloadcpython-59bc20bb273055e2cd48a467524d21340c1771cc.zip
cpython-59bc20bb273055e2cd48a467524d21340c1771cc.tar.gz
cpython-59bc20bb273055e2cd48a467524d21340c1771cc.tar.bz2
Issue 1780: Allow leading and trailing whitespace in Decimal constructor,
when constructing from a string. Disallow trailing newlines in Context.create_decimal.
Diffstat (limited to 'Lib/decimal.py')
-rw-r--r--Lib/decimal.py17
1 files changed, 14 insertions, 3 deletions
diff --git a/Lib/decimal.py b/Lib/decimal.py
index 8b54821..7e24516 100644
--- a/Lib/decimal.py
+++ b/Lib/decimal.py
@@ -523,6 +523,8 @@ class Decimal(object):
Decimal("314")
>>> Decimal(Decimal(314)) # another decimal instance
Decimal("314")
+ >>> Decimal(' 3.14 \\n') # leading and trailing whitespace okay
+ Decimal("3.14")
"""
# Note that the coefficient, self._int, is actually stored as
@@ -538,7 +540,7 @@ class Decimal(object):
# From a string
# REs insist on real strings, so we can too.
if isinstance(value, basestring):
- m = _parser(value)
+ m = _parser(value.strip())
if m is None:
if context is None:
context = getcontext()
@@ -3533,7 +3535,16 @@ class Context(object):
return rounding
def create_decimal(self, num='0'):
- """Creates a new Decimal instance but using self as context."""
+ """Creates a new Decimal instance but using self as context.
+
+ This method implements the to-number operation of the
+ IBM Decimal specification."""
+
+ if isinstance(num, basestring) and num != num.strip():
+ return self._raise_error(ConversionSyntax,
+ "no trailing or leading whitespace is "
+ "permitted.")
+
d = Decimal(num, context=self)
if d._isnan() and len(d._int) > self.prec - self._clamp:
return self._raise_error(ConversionSyntax,
@@ -5148,7 +5159,7 @@ _parser = re.compile(r""" # A numeric string consists of:
(?P<diag>\d*) # with (possibly empty) diagnostic information.
)
# \s*
- $
+ \Z
""", re.VERBOSE | re.IGNORECASE).match
_all_zeros = re.compile('0*$').match