summaryrefslogtreecommitdiffstats
path: root/Lib/decimal.py
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2007-11-30 19:27:20 (GMT)
committerChristian Heimes <christian@cheimes.de>2007-11-30 19:27:20 (GMT)
commitd59c64c49fe66c8cccd22e9bda570f71f70e6014 (patch)
treeb3749e1c47338d9c3dc4424933e0d9f438e58d4b /Lib/decimal.py
parent45031dfd1c37fca6361e881d56f4e62f800af91a (diff)
downloadcpython-d59c64c49fe66c8cccd22e9bda570f71f70e6014.zip
cpython-d59c64c49fe66c8cccd22e9bda570f71f70e6014.tar.gz
cpython-d59c64c49fe66c8cccd22e9bda570f71f70e6014.tar.bz2
Merged revisions 59234-59238 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r59237 | facundo.batista | 2007-11-30 18:15:25 +0100 (Fri, 30 Nov 2007) | 4 lines Reordering of __new__ to minimize isinstance() calls to most used types. Thanks Mark Dickinson. ........ r59238 | christian.heimes | 2007-11-30 20:18:08 +0100 (Fri, 30 Nov 2007) | 6 lines Removed or replaced some more deprecated preprocessor macros. Moved the _DEBUG and NDEBUG macros to two new property files. Fixed #1527 Problem with static libs on Windows Updated README.txt ........
Diffstat (limited to 'Lib/decimal.py')
-rw-r--r--Lib/decimal.py111
1 files changed, 56 insertions, 55 deletions
diff --git a/Lib/decimal.py b/Lib/decimal.py
index 7842cb2..2c97dbd 100644
--- a/Lib/decimal.py
+++ b/Lib/decimal.py
@@ -540,21 +540,47 @@ class Decimal(object):
# digits.
self = object.__new__(cls)
- self._is_special = False
- # From an internal working value
- if isinstance(value, _WorkRep):
- self._sign = value.sign
- self._int = str(value.int)
- self._exp = int(value.exp)
- return self
+ # From a string
+ # REs insist on real strings, so we can too.
+ if isinstance(value, str):
+ m = _parser(value)
+ if m is None:
+ if context is None:
+ context = getcontext()
+ return context._raise_error(ConversionSyntax,
+ "Invalid literal for Decimal: %r" % value)
- # From another decimal
- if isinstance(value, Decimal):
- self._exp = value._exp
- self._sign = value._sign
- self._int = value._int
- self._is_special = value._is_special
+ if m.group('sign') == "-":
+ self._sign = 1
+ else:
+ self._sign = 0
+ intpart = m.group('int')
+ if intpart is not None:
+ # finite number
+ fracpart = m.group('frac')
+ exp = int(m.group('exp') or '0')
+ if fracpart is not None:
+ self._int = (intpart+fracpart).lstrip('0') or '0'
+ self._exp = exp - len(fracpart)
+ else:
+ self._int = intpart.lstrip('0') or '0'
+ self._exp = exp
+ self._is_special = False
+ else:
+ diag = m.group('diag')
+ if diag is not None:
+ # NaN
+ self._int = diag.lstrip('0')
+ if m.group('signal'):
+ self._exp = 'N'
+ else:
+ self._exp = 'n'
+ else:
+ # infinity
+ self._int = '0'
+ self._exp = 'F'
+ self._is_special = True
return self
# From an integer
@@ -565,6 +591,23 @@ class Decimal(object):
self._sign = 1
self._exp = 0
self._int = str(abs(value))
+ self._is_special = False
+ return self
+
+ # From another decimal
+ if isinstance(value, Decimal):
+ self._exp = value._exp
+ self._sign = value._sign
+ self._int = value._int
+ self._is_special = value._is_special
+ return self
+
+ # From an internal working value
+ if isinstance(value, _WorkRep):
+ self._sign = value.sign
+ self._int = str(value.int)
+ self._exp = int(value.exp)
+ self._is_special = False
return self
# tuple/list conversion (possibly from as_tuple())
@@ -616,48 +659,6 @@ class Decimal(object):
raise TypeError("Cannot convert float to Decimal. " +
"First convert the float to a string")
- # From a string
- # REs insist on real strings, so we can too.
- if isinstance(value, str):
- m = _parser(value)
- if m is None:
- if context is None:
- context = getcontext()
- return context._raise_error(ConversionSyntax,
- "Invalid literal for Decimal: %r" % value)
-
- if m.group('sign') == "-":
- self._sign = 1
- else:
- self._sign = 0
- intpart = m.group('int')
- if intpart is not None:
- # finite number
- fracpart = m.group('frac')
- exp = int(m.group('exp') or '0')
- if fracpart is not None:
- self._int = (intpart+fracpart).lstrip('0') or '0'
- self._exp = exp - len(fracpart)
- else:
- self._int = intpart.lstrip('0') or '0'
- self._exp = exp
- self._is_special = False
- else:
- diag = m.group('diag')
- if diag is not None:
- # NaN
- self._int = diag.lstrip('0')
- if m.group('signal'):
- self._exp = 'N'
- else:
- self._exp = 'n'
- else:
- # infinity
- self._int = '0'
- self._exp = 'F'
- self._is_special = True
- return self
-
raise TypeError("Cannot convert %r to Decimal" % value)
def _isnan(self):