Skip to content

Decimal to int conversion with engineering notation costly despite failing #139957

@roharvey

Description

@roharvey

Bug report

Bug description:

Adding many digits to engineering notation causes cpython to work very hard trying to do the conversion, only for it to fail anyway.

>>> x = decimal.Decimal('1.0E+10000')
>>> int(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Exceeds the limit (4300 digits) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit

But the time it takes goes up when you add extra digits. It appears to be unbounded.

>>> timeit.timeit(setup="import decimal", stmt="x = decimal.Decimal('1.0E+10000'); i = int(x)", number=10)
0.027457250020233914
>>> timeit.timeit(setup="import decimal", stmt="x = decimal.Decimal('1.0E+100000'); i = int(x)", number=10)
1.8902547920006327

The decimal to int conversion should shortcut to a failure when there is no possibility of generating a real number, e.g. by examining the length of the substring after + or - (in conjunction with sys.set_int_max_str_digits or maybe some other setting).

CPython versions tested on:

3.11, 3.12

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirperformancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions