Skip to content

Commit c21a2ec

Browse files
vstinnermiss-islington
authored andcommitted
[3.9] pythongh-95778: Mention sys.set_int_max_str_digits() in error message (pythonGH-96874) (pythonGH-96877)
When ValueError is raised if an integer is larger than the limit, mention sys.set_int_max_str_digits() in the error message. (cherry picked from commit e841ffc) Co-authored-by: Ned Deily <nad@python.org> (cherry picked from commit 4118813) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 246a044 commit c21a2ec

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

Doc/library/stdtypes.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -4903,15 +4903,15 @@ When an operation would exceed the limit, a :exc:`ValueError` is raised:
49034903
>>> _ = int('2' * 5432)
49044904
Traceback (most recent call last):
49054905
...
4906-
ValueError: Exceeds the limit (4300) for integer string conversion: value has 5432 digits.
4906+
ValueError: Exceeds the limit (4300) for integer string conversion: value has 5432 digits; use sys.set_int_max_str_digits() to increase the limit.
49074907
>>> i = int('2' * 4300)
49084908
>>> len(str(i))
49094909
4300
49104910
>>> i_squared = i*i
49114911
>>> len(str(i_squared))
49124912
Traceback (most recent call last):
49134913
...
4914-
ValueError: Exceeds the limit (4300) for integer string conversion: value has 8599 digits.
4914+
ValueError: Exceeds the limit (4300) for integer string conversion: value has 8599 digits; use sys.set_int_max_str_digits() to increase the limit.
49154915
>>> len(hex(i_squared))
49164916
7144
49174917
>>> assert int(hex(i_squared), base=16) == i*i # Hexadecimal is unlimited.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
When :exc:`ValueError` is raised if an integer is larger than the limit,
2+
mention the :func:`sys.set_int_max_str_digits` function in the error message.
3+
Patch by Victor Stinner.

Objects/longobject.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ static PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
4848
Py_ssize_t _Py_quick_int_allocs, _Py_quick_neg_int_allocs;
4949
#endif
5050

51-
#define _MAX_STR_DIGITS_ERROR_FMT_TO_INT "Exceeds the limit (%d) for integer string conversion: value has %zd digits"
52-
#define _MAX_STR_DIGITS_ERROR_FMT_TO_STR "Exceeds the limit (%d) for integer string conversion"
51+
#define _MAX_STR_DIGITS_ERROR_FMT_TO_INT "Exceeds the limit (%d) for integer string conversion: value has %zd digits; use sys.set_int_max_str_digits() to increase the limit"
52+
#define _MAX_STR_DIGITS_ERROR_FMT_TO_STR "Exceeds the limit (%d) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit"
5353

5454
static PyObject *
5555
get_small_int(sdigit ival)

0 commit comments

Comments
 (0)