Skip to content

_pydecimal.localcontext doesn't properly accommodate flags/traps #162491

Open

Description

@pressler-vsc

Bug report

The behavior from _pydecimal.localcontext doesn't seem to match the documentation or _decimal.localcontext behavior:

import _decimal
import _pydecimal

for impl in (_decimal, _pydecimal):
    print(impl)
    print()
    with impl.localcontext(traps=[impl.Overflow]) as ctx:
        print(ctx)
        print()

_decimal

<module '_decimal' from '/home/vscuser/miniconda3/envs/jbpy/lib/python3.15/lib-dynload/_decimal.cpython-315-x86_64-linux-gnu.so'>

Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[Overflow])

_pydecimal

<module '_pydecimal' from '/home/vscuser/miniconda3/envs/jbpy/lib/python3.15/_pydecimal.py'>

Traceback (most recent call last):
  File "/home/vscuser/git/glweb/software/third-party/jbpy/tmp_repro.py", line 7, in <module>
    with impl.localcontext(traps=[impl.Overflow]) as ctx:
         ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscuser/miniconda3/envs/jbpy/lib/python3.15/_pydecimal.py", line 423, in localcontext
    setattr(ctx_manager.new_context, key, value)
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscuser/miniconda3/envs/jbpy/lib/python3.15/_pydecimal.py", line 4048, in __setattr__
    return self._set_signal_dict(name, value)
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/home/vscuser/miniconda3/envs/jbpy/lib/python3.15/_pydecimal.py", line 4021, in _set_signal_dict
    raise TypeError("%s must be a signal dict" % d)
TypeError: [<class 'decimal.Overflow'>] must be a signal dict

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bug
    on Nov 23, 2026
  2. added
    stdlib
    on Nov 23, 2026
  3. skirpichev commented on Nov 23, 2026

    @skirpichev
    Member

    I think it's a valid issue. Strictly speaking, I don't see where this behavior is documented for the _decimal.localcontext(). But on another hand, we have similar examples for Context:

    >>> import _decimal, _pydecimal
    >>> _decimal.Context(traps=[_decimal.Inexact])
    Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[Inexact])
    >>> _pydecimal.Context(traps=[_pydecimal.Inexact])
    Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[Inexact])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions