From cddd2f2d0636689cb86cbbfe40668ab2ec6c7588 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Mon, 8 Apr 2019 14:32:07 +1000 Subject: [PATCH 1/2] Add a catch to `range_length()` to change the OverflowError to a ValueError with a different error message --- Objects/rangeobject.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index 4b8e5ed4cfd4cd..e0d1459d540208 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -212,7 +212,13 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step) static Py_ssize_t range_length(rangeobject *r) { - return PyLong_AsSsize_t(r->length); + Py_ssize_t size = PyLong_AsSsize_t(r->length); + if (size < 0 && PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_OverflowError)) { + PyErr_Clear(); + PyErr_Format(PyExc_ValueError, "Range object too large to calculate length (Overflow Error)"); + return -1; + } + return size; } static PyObject * From ca18ca7c0204dd81e58a5a579c218fb433773c60 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" Date: Mon, 8 Apr 2019 04:45:04 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Core and Builtins/2019-04-08-04-45-02.bpo-36552.X5p6-M.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-04-08-04-45-02.bpo-36552.X5p6-M.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-04-08-04-45-02.bpo-36552.X5p6-M.rst b/Misc/NEWS.d/next/Core and Builtins/2019-04-08-04-45-02.bpo-36552.X5p6-M.rst new file mode 100644 index 00000000000000..2db8e8ec2347ff --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-04-08-04-45-02.bpo-36552.X5p6-M.rst @@ -0,0 +1 @@ +Calculating the length of range objects whose length is larger than PY_SIZE_MAX will now raise a ValueError instead of an OverflowError. \ No newline at end of file