Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/_pydecimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@ def sin(x):
for key, value in kwargs.items():
if key not in _context_attributes:
raise TypeError(f"'{key}' is an invalid keyword argument for this function")
if key in ('flags', 'traps') and not isinstance(value, dict):
# Accept a list of signals, like the Context constructor.
value = dict((s, int(s in value)) for s in _signals + value)
setattr(ctx_manager.new_context, key, value)
return ctx_manager

Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3851,6 +3851,19 @@ def test_localcontext_kwargs(self):
self.assertEqual(ctx.capitals, 0)
self.assertEqual(ctx.clamp, 1)

# gh-162491: flags and traps also accept a list of signals.
Overflow = self.decimal.Overflow
with self.decimal.localcontext(
flags=[self.decimal.Inexact], traps=[Overflow]
) as ctx:
self.assertTrue(ctx.flags[self.decimal.Inexact])
self.assertFalse(ctx.flags[Overflow])
self.assertTrue(ctx.traps[Overflow])
self.assertFalse(ctx.traps[self.decimal.DivisionByZero])
with self.decimal.localcontext(traps=[]) as ctx:
self.assertFalse(any(ctx.traps.values()))
self.assertRaises(KeyError, self.decimal.localcontext, traps=[ValueError])

self.assertRaises(TypeError, self.decimal.localcontext, precision=10)

self.assertRaises(ValueError, self.decimal.localcontext, Emin=1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The pure-Python :func:`decimal.localcontext` now accepts a list of signals
for its *flags* and *traps* arguments, like the C implementation.
Loading