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

Skip to content

Commit 0dd8f78

Browse files
committed
Issue #9136: Profiling Decimal gave 'dictionary changed size during iteration'.
Remove the use of locals() that caused this error.
1 parent e42f1bb commit 0dd8f78

2 files changed

Lines changed: 35 additions & 13 deletions

File tree

Lib/decimal.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3813,20 +3813,38 @@ def __init__(self, prec=None, rounding=None,
38133813
Emin=None, Emax=None,
38143814
capitals=None, clamp=None,
38153815
_ignored_flags=None):
3816-
if flags is None:
3817-
flags = []
3816+
# Set defaults; for everything except flags and _ignored_flags,
3817+
# inherit from DefaultContext.
3818+
try:
3819+
dc = DefaultContext
3820+
except NameError:
3821+
pass
3822+
3823+
self.prec = prec if prec is not None else dc.prec
3824+
self.rounding = rounding if rounding is not None else dc.rounding
3825+
self.Emin = Emin if Emin is not None else dc.Emin
3826+
self.Emax = Emax if Emax is not None else dc.Emax
3827+
self.capitals = capitals if capitals is not None else dc.capitals
3828+
self.clamp = clamp if clamp is not None else dc.clamp
3829+
38183830
if _ignored_flags is None:
3819-
_ignored_flags = []
3820-
if not isinstance(flags, dict):
3821-
flags = dict([(s, int(s in flags)) for s in _signals])
3822-
if traps is not None and not isinstance(traps, dict):
3823-
traps = dict([(s, int(s in traps)) for s in _signals])
3824-
for name, val in locals().items():
3825-
if val is None:
3826-
setattr(self, name, _copy.copy(getattr(DefaultContext, name)))
3827-
else:
3828-
setattr(self, name, val)
3829-
del self.self
3831+
self._ignored_flags = []
3832+
else:
3833+
self._ignored_flags = _ignored_flags
3834+
3835+
if traps is None:
3836+
self.traps = dc.traps.copy()
3837+
elif not isinstance(traps, dict):
3838+
self.traps = dict((s, int(s in traps)) for s in _signals)
3839+
else:
3840+
self.traps = traps
3841+
3842+
if flags is None:
3843+
self.flags = dict.fromkeys(_signals, 0)
3844+
elif not isinstance(flags, dict):
3845+
self.flags = dict((s, int(s in flags)) for s in _signals)
3846+
else:
3847+
self.flags = flags
38303848

38313849
def __repr__(self):
38323850
"""Show the current context."""

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,10 @@ C-API
470470
Library
471471
-------
472472

473+
- Issue #9136: Fix 'dictionary changed size during iteration'
474+
RuntimeError produced when profiling the decimal module. This was
475+
due to a dangerous iteration over 'locals()' in Context.__init__.
476+
473477
- Fix extreme speed issue in Decimal.pow when the base is an exact
474478
power of 10 and the exponent is tiny (for example,
475479
Decimal(10) ** Decimal('1e-999999999')).

0 commit comments

Comments
 (0)