mp.c: Convert shift amounts via `index' to prevent `float' and `str'.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 24 Nov 2019 00:21:44 +0000 (00:21 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 11 Apr 2020 11:49:31 +0000 (12:49 +0100)
It obviously makes no sense to calculate `MP(4) << "4"' or `MP(9) >>
2.8'.

mp.c

diff --git a/mp.c b/mp.c
index 96e3e86..c47b011 100644 (file)
--- a/mp.c
+++ b/mp.c
@@ -393,25 +393,24 @@ UNOP(mp, not2c)
 static PyObject *mp_pyid(PyObject *x) { RETURN_OBJ(x); }
 
 #define gf_lsr mp_lsr
-#define SHIFTOP(pre, name, rname)                                      \
+#define SHIFTOP(pre, PRE, name, rname)                                 \
   static PyObject *pre##_py##name(PyObject *x, PyObject *y) {          \
-    mp *xx, *yy;                                                       \
+    PyObject *yix = 0;                                                 \
     PyObject *z = 0;                                                   \
     long n;                                                            \
-    if (pre##binop(x, y, &xx, &yy)) RETURN_NOTIMPL;                    \
-    if (mp_tolong_checked(yy, &n, 1)) goto end;                                \
-    if (n < 0)                                                         \
-      z = pre##_pywrap(mp_##rname(MP_NEW, xx, -n));                    \
-    else                                                               \
-      z = pre##_pywrap(mp_##name(MP_NEW, xx, n));                      \
-  end:                                                                 \
-    MP_DROP(xx); MP_DROP(yy);                                          \
+    if (!PRE##_PYCHECK(x)) RETURN_NOTIMPL;                             \
+    if (GF_PYCHECK(y) || FE_PYCHECK(y)) RETURN_NOTIMPL;                        \
+    yix = PyNumber_Index(y); if (!yix) { PyErr_Clear(); RETURN_NOTIMPL; } \
+    n = PyInt_AsLong(yix); Py_DECREF(yix);                             \
+    if (n == -1 && PyErr_Occurred())  { PyErr_Clear(); RETURN_NOTIMPL; } \
+    if (n < 0) z = pre##_pywrap(mp_##rname(MP_NEW, MP_X(x), -n));      \
+    else z = pre##_pywrap(mp_##name(MP_NEW, MP_X(x), n));              \
     return (z);                                                                \
   }
-SHIFTOP(mp, lsl2c, lsr2c)
-SHIFTOP(mp, lsr2c, lsl2c)
-SHIFTOP(gf, lsl, lsr)
-SHIFTOP(gf, lsr, lsl)
+SHIFTOP(mp, MP, lsl2c, lsr2c)
+SHIFTOP(mp, MP, lsr2c, lsl2c)
+SHIFTOP(gf, GF, lsl, lsr)
+SHIFTOP(gf, GF, lsr, lsl)
 #undef SHIFTOP
 
 #define DIVOP(pre, name, qq, rr, gather)                               \