rand.c: Cope with large `range' arguments.
authorMark Wooding <mdw@distorted.org.uk>
Fri, 15 Nov 2019 18:26:57 +0000 (18:26 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 25 Nov 2019 17:43:08 +0000 (17:43 +0000)
Python 3 will report large numbers as being `int' values, and
`PyInt_AS_LONG' will return nonsense in this case.

rand.c

diff --git a/rand.c b/rand.c
index 6c6abdb..22a0b3c 100644 (file)
--- a/rand.c
+++ b/rand.c
@@ -102,8 +102,9 @@ static PyObject *grmeth_range(PyObject *me, PyObject *arg)
   if (!PyArg_ParseTuple(arg, "O:range", &m)) return (0);
   if (grand_check(me)) return (0);
   if (PyInt_Check(m)) {
-    long mm = PyInt_AS_LONG(m);
-    if (mm <= 0) goto notpos;
+    long mm = PyInt_AsLong(m);
+    if (mm == -1 && PyErr_Occurred()) PyErr_Clear();
+    else if (mm <= 0) goto notpos;
     else if (mm <= 0xffffffff)
       return (PyInt_FromLong(grand_range(GRAND_R(me), mm)));
   }