Skip to content

Issues with Decimal and three-argument pow() #130230

Closed

Description

@serhiy-storchaka

Crash report

  1. Three-argument pow() with non-Decimal first argument does not work in the Python implementation.
>>> from decimal import Decimal
>>> pow(10, Decimal(2), 7)
Decimal('2')
>>> from _pydecimal import Decimal
>>> pow(10, Decimal(2), 7)
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    pow(10, Decimal(2), 7)
    ~~~^^^^^^^^^^^^^^^^^^^
TypeError: unsupported operand type(s) for ** or pow(): 'int', 'Decimal', 'int'

But it works in the C implementation.

  1. Three-argument pow() with only Decimal third argument crashes in the C implementation.
>>> from decimal import Decimal
>>> pow(10, 2, Decimal(7))
python: ./Modules/_decimal/_decimal.c:139: find_state_left_or_right: Assertion `mod != NULL' failed.
Aborted (core dumped)

In the Python implementation it only raises TypeError, which is expected.

Linked PRs

Activity

  1. added
    3.12
    3.13
    3.14
    extension-modules
    stdlib
    type-crash
    on Feb 17, 2025
  2. added a commit that references this issue on Feb 17, 2025
  3. serhiy-storchaka commented on Feb 17, 2025

    @serhiy-storchaka
    MemberAuthor

    #130237 fixes the second issue. It was a regression in 3.13 (most likely introduced in #106078). In 3.12 and older, pow(10, 2, Decimal(7)) evaluates to Decimal('2').

    The first issue is older (it exists from the beginning) and much more complex.

  4. skirpichev commented on Feb 18, 2025

    @skirpichev
    Member

    The first issue is older (it exists from the beginning) and much more complex.

    It's just an instance of #130104. I doubt we can do something on side of the pure-Python module.

    BTW, 100% compatibility is not a goal here.

  5. removed
    3.12
    on Feb 18, 2025
  6. skirpichev commented on Feb 18, 2025

    @skirpichev
    Member

    Second issue works for me (no crash) for <3.13, removing label.

  7. added a commit that references this issue on Feb 18, 2025
  8. added a commit that references this issue on Feb 18, 2025
  9. added 3 commits that reference this issue on Feb 18, 2025
  10. serhiy-storchaka commented on Feb 18, 2025

    @serhiy-storchaka
    MemberAuthor

    I found these issues when writing tests for #130104, but I was not sure that it was the only cause.

  11. added a commit that references this issue on Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

3.133.14extension-modulesstdlibtype-crash

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions