From 34e3dbc6ba03a3c9ce7d2238b7ec98aea4a939ee Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Sun, 24 Nov 2019 00:21:44 +0000 Subject: [PATCH] mp.c: Convert shift amounts via `index' to prevent `float' and `str'. It obviously makes no sense to calculate `MP(4) << "4"' or `MP(9) >> 2.8'. --- mp.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/mp.c b/mp.c index 96e3e86..c47b011 100644 --- 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) \ -- 2.11.0