From c21a2eca265fa59852d5cf369250ca684d49c8e9 Mon Sep 17 00:00:00 2001
From: Victor Stinner <vstinner@python.org>
Date: Tue, 4 Oct 2022 19:05:45 +0200
Subject: [PATCH] [3.9] gh-95778: Mention sys.set_int_max_str_digits() in error
 message (GH-96874) (GH-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 e841ffc915e82e5ea6e3b473205417d63494808d)

Co-authored-by: Ned Deily <nad@python.org>
(cherry picked from commit 41188134bd2120f0cedd681ed88743c11c7f3742)

Co-authored-by: Victor Stinner <vstinner@python.org>
---
 Doc/library/stdtypes.rst                                      | 4 ++--
 .../2022-09-16-19-02-40.gh-issue-95778.cJmnst.rst             | 3 +++
 Objects/longobject.c                                          | 4 ++--
 3 files changed, 7 insertions(+), 4 deletions(-)
 create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-09-16-19-02-40.gh-issue-95778.cJmnst.rst

diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
index 14d48e6b07c029..45d648bca0c5a7 100644
--- a/Doc/library/stdtypes.rst
+++ b/Doc/library/stdtypes.rst
@@ -4903,7 +4903,7 @@ When an operation would exceed the limit, a :exc:`ValueError` is raised:
    >>> _ = int('2' * 5432)
    Traceback (most recent call last):
    ...
-   ValueError: Exceeds the limit (4300) for integer string conversion: value has 5432 digits.
+   ValueError: Exceeds the limit (4300) for integer string conversion: value has 5432 digits; use sys.set_int_max_str_digits() to increase the limit.
    >>> i = int('2' * 4300)
    >>> len(str(i))
    4300
@@ -4911,7 +4911,7 @@ When an operation would exceed the limit, a :exc:`ValueError` is raised:
    >>> len(str(i_squared))
    Traceback (most recent call last):
    ...
-   ValueError: Exceeds the limit (4300) for integer string conversion: value has 8599 digits.
+   ValueError: Exceeds the limit (4300) for integer string conversion: value has 8599 digits; use sys.set_int_max_str_digits() to increase the limit.
    >>> len(hex(i_squared))
    7144
    >>> assert int(hex(i_squared), base=16) == i*i  # Hexadecimal is unlimited.
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-09-16-19-02-40.gh-issue-95778.cJmnst.rst b/Misc/NEWS.d/next/Core and Builtins/2022-09-16-19-02-40.gh-issue-95778.cJmnst.rst
new file mode 100644
index 00000000000000..ebf63778a605d7
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-09-16-19-02-40.gh-issue-95778.cJmnst.rst	
@@ -0,0 +1,3 @@
+When :exc:`ValueError` is raised if an integer is larger than the limit,
+mention the :func:`sys.set_int_max_str_digits` function in the error message.
+Patch by Victor Stinner.
diff --git a/Objects/longobject.c b/Objects/longobject.c
index a58a2e1e78b1fc..26850519767249 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -48,8 +48,8 @@ static PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
 Py_ssize_t _Py_quick_int_allocs, _Py_quick_neg_int_allocs;
 #endif
 
-#define _MAX_STR_DIGITS_ERROR_FMT_TO_INT "Exceeds the limit (%d) for integer string conversion: value has %zd digits"
-#define _MAX_STR_DIGITS_ERROR_FMT_TO_STR "Exceeds the limit (%d) for integer string conversion"
+#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"
+#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"
 
 static PyObject *
 get_small_int(sdigit ival)