Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1a20c12

Browse files
committed
Issue #11830: Remove unnecessary introspection code in the decimal module.
Forward ported changesets b4b1f557d563 and f4adc2926bf5 by Raymond Hettinger in branch '2.7'.
1 parent 2df393c commit 1a20c12

2 files changed

Lines changed: 16 additions & 17 deletions

File tree

Lib/decimal.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,7 @@ def _fix(self, context):
16501650
self = _dec_from_triple(self._sign, '1', exp_min-1)
16511651
digits = 0
16521652
rounding_method = self._pick_rounding_function[context.rounding]
1653-
changed = getattr(self, rounding_method)(digits)
1653+
changed = rounding_method(self, digits)
16541654
coeff = self._int[:digits] or '0'
16551655
if changed > 0:
16561656
coeff = str(int(coeff)+1)
@@ -1690,8 +1690,6 @@ def _fix(self, context):
16901690
# here self was representable to begin with; return unchanged
16911691
return Decimal(self)
16921692

1693-
_pick_rounding_function = {}
1694-
16951693
# for each of the rounding functions below:
16961694
# self is a finite, nonzero Decimal
16971695
# prec is an integer satisfying 0 <= prec < len(self._int)
@@ -1758,6 +1756,17 @@ def _round_05up(self, prec):
17581756
else:
17591757
return -self._round_down(prec)
17601758

1759+
_pick_rounding_function = dict(
1760+
ROUND_DOWN = _round_down,
1761+
ROUND_UP = _round_up,
1762+
ROUND_HALF_UP = _round_half_up,
1763+
ROUND_HALF_DOWN = _round_half_down,
1764+
ROUND_HALF_EVEN = _round_half_even,
1765+
ROUND_CEILING = _round_ceiling,
1766+
ROUND_FLOOR = _round_floor,
1767+
ROUND_05UP = _round_05up,
1768+
)
1769+
17611770
def __round__(self, n=None):
17621771
"""Round self to the nearest integer, or to a given precision.
17631772
@@ -2554,8 +2563,8 @@ def _rescale(self, exp, rounding):
25542563
if digits < 0:
25552564
self = _dec_from_triple(self._sign, '1', exp-1)
25562565
digits = 0
2557-
this_function = getattr(self, self._pick_rounding_function[rounding])
2558-
changed = this_function(digits)
2566+
this_function = self._pick_rounding_function[rounding]
2567+
changed = this_function(self, digits)
25592568
coeff = self._int[:digits] or '0'
25602569
if changed == 1:
25612570
coeff = str(int(coeff)+1)
@@ -3767,18 +3776,6 @@ def _dec_from_triple(sign, coefficient, exponent, special=False):
37673776

37683777
##### Context class #######################################################
37693778

3770-
3771-
# get rounding method function:
3772-
rounding_functions = [name for name in Decimal.__dict__.keys()
3773-
if name.startswith('_round_')]
3774-
for name in rounding_functions:
3775-
# name is like _round_half_even, goes to the global ROUND_HALF_EVEN value.
3776-
globalname = name[1:].upper()
3777-
val = globals()[globalname]
3778-
Decimal._pick_rounding_function[val] = name
3779-
3780-
del name, val, globalname, rounding_functions
3781-
37823779
class _ContextManager(object):
37833780
"""Context manager class to support localcontext().
37843781

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Core and Builtins
5353
Library
5454
-------
5555

56+
- Issue #11830: Remove unnecessary introspection code in the decimal module.
57+
5658
- Issue #11703 - urllib2.geturl() does not return correct url when the original
5759
url contains #fragment.
5860

0 commit comments

Comments
 (0)