Skip to content

_pydecimal: int to and from Decimal conversion fails for large ints #96589

Closed as not planned
@mdickinson

Description

@mdickinson

Bug report

Since the fix for #95778 was merged, conversions from large ints to Decimal instances fail, but only for the Python version of the decimal module. The C version is unaffected.

Here's the _pydecimal behaviour: conversion large-int -> Decimal raises; conversion from Decimal back to int does not:

mdickinson@lovelace cpython % ./python.exe
Python 3.12.0a0 (heads/main:a9d58feccf, Sep  5 2022, 17:17:03) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from _pydecimal import Decimal
>>> d = Decimal(10**10000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mdickinson/Repositories/python/cpython/Lib/_pydecimal.py", line 602, in __new__
    self._int = str(abs(value))
                ^^^^^^^^^^^^^^^
ValueError: Exceeds the limit (4300) for integer string conversion
>>> n = int(Decimal(10)**10000)  # works as expected

Given that almost no CPython users should be using _pydecimal, this probably doesn't matter much, but it seemed worth recording.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions