From ccb6fd20d865df51a096c892ded27e37dab4f1fe Mon Sep 17 00:00:00 2001 From: Sumit Kumar Das Date: Mon, 23 Nov 2026 19:37:50 +0000 Subject: [PATCH 1/2] gh-162491: Accept a list of signals in _pydecimal.localcontext() localcontext() set the flags and traps keyword arguments directly on the new context, which only accepts a signal dict, so passing a list of signals raised TypeError. Convert a list the same way the Context constructor does, matching the C implementation. Co-Authored-By: Claude Opus 5.5 --- Lib/_pydecimal.py | 3 +++ Lib/test/test_decimal.py | 13 +++++++++++++ .../2026-11-23-20-00-00.gh-issue-162491.jyVmsQ.rst | 2 ++ 3 files changed, 18 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-23-20-00-00.gh-issue-162491.jyVmsQ.rst diff --git a/Lib/_pydecimal.py b/Lib/_pydecimal.py index 4b7bfd07887bd3..d3769bb1f49e46 100644 --- a/Lib/_pydecimal.py +++ b/Lib/_pydecimal.py @@ -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 isinstance(value, list): + # 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 diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index ef7aa5358b5cc6..c70bedda1fb687 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -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) diff --git a/Misc/NEWS.d/next/Library/2026-11-23-20-00-00.gh-issue-162491.jyVmsQ.rst b/Misc/NEWS.d/next/Library/2026-11-23-20-00-00.gh-issue-162491.jyVmsQ.rst new file mode 100644 index 00000000000000..91cce7bf465005 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-23-20-00-00.gh-issue-162491.jyVmsQ.rst @@ -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. From a619c64a8a73163305d19ea51cc2cf9d83fdbb05 Mon Sep 17 00:00:00 2001 From: Sumit Kumar Das Date: Fri, 27 Nov 2026 19:27:21 +0000 Subject: [PATCH 2/2] Check for a dict, like the Context constructor Co-authored-by: Sergey B Kirpichev --- Lib/_pydecimal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/_pydecimal.py b/Lib/_pydecimal.py index d3769bb1f49e46..8b02f19c4e9d99 100644 --- a/Lib/_pydecimal.py +++ b/Lib/_pydecimal.py @@ -420,7 +420,7 @@ 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 isinstance(value, list): + 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)