From 60c20cf83a6bc295b2234e898ef175f9874580a7 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 15 Mar 2017 22:22:19 +0200 Subject: [PATCH 1/5] bpo-29816: Shift operation now has less opportunity to raise OverflowError. ValueError always is raised rather than OverflowError for negative counts. Shifting zero with non-negative count always returns zero. --- Lib/test/test_long.py | 31 +++++++++++++++++++++++++++++-- Misc/NEWS | 4 ++++ Objects/longobject.c | 29 ++++++++++++++++++++--------- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index fd15f04aceca8f..0152b657e2e985 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -906,20 +906,47 @@ def test_correctly_rounded_true_division(self): self.check_truediv(-x, y) self.check_truediv(-x, -y) + def test_negative_shift_count(self): + with self.assertRaises(ValueError): + 42 << -3 + with self.assertRaises(ValueError): + 42 << -(1 << 1000) + with self.assertRaises(ValueError): + 42 >> -3 + with self.assertRaises(ValueError): + 42 >> -(1 << 1000) + def test_lshift_of_zero(self): self.assertEqual(0 << 0, 0) self.assertEqual(0 << 10, 0) with self.assertRaises(ValueError): 0 << -1 + self.assertEqual(0 << (1 << 1000), 0) + with self.assertRaises(ValueError): + 0 << -(1 << 1000) @support.cpython_only - def test_huge_lshift_of_zero(self): + def test_huge_lshift(self): # Shouldn't try to allocate memory for a huge shift. See issue #27870. # Other implementations may have a different boundary for overflow, # or not raise at all. self.assertEqual(0 << sys.maxsize, 0) + self.assertEqual(0 << (sys.maxsize + 1), 0) + with self.assertRaises(OverflowError): + 1 << (sys.maxsize + 1) + + def test_huge_rshift(self): + self.assertEqual(42 >> (1 << 1000), 0) + self.assertEqual((-42) >> (1 << 1000), -1) + + @support.cpython_only + @support.bigmemtest(sys.maxsize + 500, memuse=2/15, dry_run=False) + def test_huge_rshift_of_huge(self, size): + huge = 1 << 500 << sys.maxsize + with self.assertRaises(OverflowError): + huge >> (sys.maxsize + 1) with self.assertRaises(OverflowError): - 0 << (sys.maxsize + 1) + huge >> (sys.maxsize + 1000) def test_small_ints(self): for i in range(-5, 257): diff --git a/Misc/NEWS b/Misc/NEWS index b7990c62e4f744..7e184ca37ffd73 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,10 @@ What's New in Python 3.7.0 alpha 1? Core and Builtins ----------------- +- bpo-29816: Shift operation now has less opportunity to raise OverflowError. + ValueError always is raised rather than OverflowError for negative counts. + Shifting zero with non-negative count always returns zero. + - bpo-28856: Fix an oversight that %b format for bytes should support objects follow the buffer protocol. diff --git a/Objects/longobject.c b/Objects/longobject.c index 95661a4022018f..bc72a3b957ef3b 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -4308,14 +4308,21 @@ long_rshift(PyLongObject *a, PyLongObject *b) Py_DECREF(a2); } else { - shiftby = PyLong_AsSsize_t((PyObject *)b); - if (shiftby == -1L && PyErr_Occurred()) - return NULL; - if (shiftby < 0) { + if (Py_SIZE(b) < 0) { PyErr_SetString(PyExc_ValueError, "negative shift count"); return NULL; } + shiftby = PyLong_AsSsize_t((PyObject *)b); + if (shiftby < 0) { + /* PyLong_Check(b) is true, so it must be that + PyLong_AsSsize_t raised an OverflowError. */ + assert(PyErr_ExceptionMatches(PyExc_OverflowError)); + if (Py_ABS(Py_SIZE(a)) > PY_SSIZE_T_MAX / PyLong_SHIFT) + return NULL; + PyErr_Clear(); + return PyLong_FromLong(0); + } wordshift = shiftby / PyLong_SHIFT; newsize = Py_ABS(Py_SIZE(a)) - wordshift; if (newsize <= 0) @@ -4349,18 +4356,22 @@ long_lshift(PyObject *v, PyObject *w) CHECK_BINOP(a, b); - shiftby = PyLong_AsSsize_t((PyObject *)b); - if (shiftby == -1L && PyErr_Occurred()) - return NULL; - if (shiftby < 0) { + if (Py_SIZE(b) < 0) { PyErr_SetString(PyExc_ValueError, "negative shift count"); return NULL; } - if (Py_SIZE(a) == 0) { return PyLong_FromLong(0); } + shiftby = PyLong_AsSsize_t((PyObject *)b); + if (shiftby < 0) { + /* PyLong_Check(b) is true, so it must be that + PyLong_AsSsize_t raised an OverflowError. */ + assert(PyErr_ExceptionMatches(PyExc_OverflowError)); + return NULL; + } + /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ wordshift = shiftby / PyLong_SHIFT; remshift = shiftby - wordshift * PyLong_SHIFT; From d0fcb3db3ebe74563cb2915cca13d8aac88fcb97 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 17 Mar 2017 18:02:17 +0200 Subject: [PATCH 2/5] Don't raise OverflowError in shift operations if the result is representable. --- Lib/test/test_long.py | 17 ++++++----- Objects/longobject.c | 69 ++++++++++++++++++++++++++++++------------- 2 files changed, 58 insertions(+), 28 deletions(-) diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index 0152b657e2e985..cc48259e35fb2c 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -926,14 +926,17 @@ def test_lshift_of_zero(self): 0 << -(1 << 1000) @support.cpython_only - def test_huge_lshift(self): + def test_huge_lshift_of_zero(self): # Shouldn't try to allocate memory for a huge shift. See issue #27870. # Other implementations may have a different boundary for overflow, # or not raise at all. self.assertEqual(0 << sys.maxsize, 0) self.assertEqual(0 << (sys.maxsize + 1), 0) - with self.assertRaises(OverflowError): - 1 << (sys.maxsize + 1) + + @support.cpython_only + @support.bigmemtest(sys.maxsize + 1000, memuse=2/15 * 2, dry_run=False) + def test_huge_lshift(self, size): + self.assertEqual(1 << (sys.maxsize + 1000), 1 << 1000 << sys.maxsize) def test_huge_rshift(self): self.assertEqual(42 >> (1 << 1000), 0) @@ -942,11 +945,9 @@ def test_huge_rshift(self): @support.cpython_only @support.bigmemtest(sys.maxsize + 500, memuse=2/15, dry_run=False) def test_huge_rshift_of_huge(self, size): - huge = 1 << 500 << sys.maxsize - with self.assertRaises(OverflowError): - huge >> (sys.maxsize + 1) - with self.assertRaises(OverflowError): - huge >> (sys.maxsize + 1000) + huge = ((1 << 500) + 11) << sys.maxsize + self.assertEqual(huge >> (sys.maxsize + 1), (1 << 499) + 5) + self.assertEqual(huge >> (sys.maxsize + 1000), 0) def test_small_ints(self): for i in range(-5, 257): diff --git a/Objects/longobject.c b/Objects/longobject.c index bc72a3b957ef3b..6e460fec1b6274 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -4294,6 +4294,12 @@ long_rshift(PyLongObject *a, PyLongObject *b) CHECK_BINOP(a, b); + if (Py_SIZE(b) < 0) { + PyErr_SetString(PyExc_ValueError, + "negative shift count"); + return NULL; + } + if (Py_SIZE(a) < 0) { /* Right shifting negative numbers is harder */ PyLongObject *a1, *a2; @@ -4308,26 +4314,35 @@ long_rshift(PyLongObject *a, PyLongObject *b) Py_DECREF(a2); } else { - if (Py_SIZE(b) < 0) { - PyErr_SetString(PyExc_ValueError, - "negative shift count"); - return NULL; - } shiftby = PyLong_AsSsize_t((PyObject *)b); - if (shiftby < 0) { - /* PyLong_Check(b) is true, so it must be that + if (shiftby >= 0) { + wordshift = shiftby / PyLong_SHIFT; + loshift = shiftby - wordshift * PyLong_SHIFT; + } + else { + /* PyLong_Check(b) is true and Py_SIZE(b) >= 0, so it must be that PyLong_AsSsize_t raised an OverflowError. */ assert(PyErr_ExceptionMatches(PyExc_OverflowError)); - if (Py_ABS(Py_SIZE(a)) > PY_SSIZE_T_MAX / PyLong_SHIFT) - return NULL; PyErr_Clear(); - return PyLong_FromLong(0); + /* With such large wordshift return PyLong_FromLong(0) below. */ + wordshift = PY_SSIZE_T_MAX / sizeof(digit); + loshift = 0; +#if SIZE_MAX / 2 < ULLONG_MAX / 15 + if (Py_SIZE(a) > PY_SSIZE_T_MAX / PyLong_SHIFT) { + int overflow; + long long lshiftby = PyLong_AsLongLongAndOverflow((PyObject *)b, + &overflow); + assert(!PyErr_Occurred()); + if (lshiftby >= 0 && lshiftby / PyLong_SHIFT < wordshift) { + wordshift = lshiftby / PyLong_SHIFT; + loshift = lshiftby - wordshift * PyLong_SHIFT; + } + } +#endif } - wordshift = shiftby / PyLong_SHIFT; - newsize = Py_ABS(Py_SIZE(a)) - wordshift; + newsize = Py_SIZE(a) - wordshift; if (newsize <= 0) return PyLong_FromLong(0); - loshift = shiftby % PyLong_SHIFT; hishift = PyLong_SHIFT - loshift; lomask = ((digit)1 << hishift) - 1; himask = PyLong_MASK ^ lomask; @@ -4365,17 +4380,31 @@ long_lshift(PyObject *v, PyObject *w) } shiftby = PyLong_AsSsize_t((PyObject *)b); - if (shiftby < 0) { - /* PyLong_Check(b) is true, so it must be that + /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ + if (shiftby >= 0) { + wordshift = shiftby / PyLong_SHIFT; + remshift = shiftby - wordshift * PyLong_SHIFT; + } + else { + /* PyLong_Check(b) is true and Py_SIZE(b) >= 0, so it must be that PyLong_AsSsize_t raised an OverflowError. */ assert(PyErr_ExceptionMatches(PyExc_OverflowError)); - return NULL; + PyErr_Clear(); + /* With such large wordshift raise error in _PyLong_New() below. */ + wordshift = PY_SSIZE_T_MAX / sizeof(digit); + remshift = 0; +#if SIZE_MAX / 2 < ULLONG_MAX / 15 + int overflow; + long long lshiftby = PyLong_AsLongLongAndOverflow((PyObject *)b, + &overflow); + assert(!PyErr_Occurred()); + if (lshiftby >= 0 && lshiftby / PyLong_SHIFT < wordshift) { + wordshift = lshiftby / PyLong_SHIFT; + remshift = lshiftby - wordshift * PyLong_SHIFT; + } +#endif } - /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ - wordshift = shiftby / PyLong_SHIFT; - remshift = shiftby - wordshift * PyLong_SHIFT; - oldsize = Py_ABS(Py_SIZE(a)); newsize = oldsize + wordshift; if (remshift) From 7edc7f8d9133860a8bc67fc320b30731fcf39eea Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 20 Mar 2017 19:41:29 +0200 Subject: [PATCH 3/5] Use "%" rather than "*" and "-". --- Objects/longobject.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Objects/longobject.c b/Objects/longobject.c index 6e460fec1b6274..7c0acf9ef3ece2 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -4317,7 +4317,7 @@ long_rshift(PyLongObject *a, PyLongObject *b) shiftby = PyLong_AsSsize_t((PyObject *)b); if (shiftby >= 0) { wordshift = shiftby / PyLong_SHIFT; - loshift = shiftby - wordshift * PyLong_SHIFT; + loshift = shiftby % PyLong_SHIFT; } else { /* PyLong_Check(b) is true and Py_SIZE(b) >= 0, so it must be that @@ -4335,7 +4335,7 @@ long_rshift(PyLongObject *a, PyLongObject *b) assert(!PyErr_Occurred()); if (lshiftby >= 0 && lshiftby / PyLong_SHIFT < wordshift) { wordshift = lshiftby / PyLong_SHIFT; - loshift = lshiftby - wordshift * PyLong_SHIFT; + loshift = lshiftby % PyLong_SHIFT; } } #endif @@ -4383,7 +4383,7 @@ long_lshift(PyObject *v, PyObject *w) /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ if (shiftby >= 0) { wordshift = shiftby / PyLong_SHIFT; - remshift = shiftby - wordshift * PyLong_SHIFT; + remshift = shiftby % PyLong_SHIFT; } else { /* PyLong_Check(b) is true and Py_SIZE(b) >= 0, so it must be that @@ -4400,7 +4400,7 @@ long_lshift(PyObject *v, PyObject *w) assert(!PyErr_Occurred()); if (lshiftby >= 0 && lshiftby / PyLong_SHIFT < wordshift) { wordshift = lshiftby / PyLong_SHIFT; - remshift = lshiftby - wordshift * PyLong_SHIFT; + remshift = lshiftby % PyLong_SHIFT; } #endif } From a3d415d707ff3ab7f170e98d8251be3e4c3f2dc9 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 20 Mar 2017 19:55:56 +0200 Subject: [PATCH 4/5] Share code between left and rigth shifts. --- Objects/longobject.c | 91 ++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 54 deletions(-) diff --git a/Objects/longobject.c b/Objects/longobject.c index 7c0acf9ef3ece2..2517e46339139f 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -4285,11 +4285,44 @@ long_bool(PyLongObject *v) return Py_SIZE(v) != 0; } +/* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ +static void +divmod_shift(PyObject *shiftby, Py_ssize_t *wordshift, Py_ssize_t *remshift) +{ + assert(PyLong_Check(shiftby)); + assert(Py_SIZE(shiftby) >= 0); + Py_ssize_t lshiftby = PyLong_AsSsize_t(shiftby); + if (lshiftby >= 0) { + *wordshift = lshiftby / PyLong_SHIFT; + *remshift = lshiftby % PyLong_SHIFT; + } + else { + /* PyLong_Check(shiftby) is true and Py_SIZE(shiftby) >= 0, so it must + be that PyLong_AsSsize_t raised an OverflowError. */ + assert(PyErr_ExceptionMatches(PyExc_OverflowError)); + PyErr_Clear(); + /* Clip the value. With such large wordshift the right shift + returns 0 and the left shift raises an error in _PyLong_New(). */ + *wordshift = PY_SSIZE_T_MAX / sizeof(digit); + *remshift = 0; +#if SIZE_MAX / 2 < ULLONG_MAX / 15 + int overflow; + long long llshiftby = PyLong_AsLongLongAndOverflow(shiftby, &overflow); + assert(!PyErr_Occurred()); + if (llshiftby >= 0 /* no overflow */ && + llshiftby / PyLong_SHIFT < PY_SSIZE_T_MAX / sizeof(digit)) { + *wordshift = llshiftby / PyLong_SHIFT; + *remshift = llshiftby % PyLong_SHIFT; + } +#endif + } +} + static PyObject * long_rshift(PyLongObject *a, PyLongObject *b) { PyLongObject *z = NULL; - Py_ssize_t shiftby, newsize, wordshift, loshift, hishift, i, j; + Py_ssize_t newsize, wordshift, loshift, hishift, i, j; digit lomask, himask; CHECK_BINOP(a, b); @@ -4314,32 +4347,7 @@ long_rshift(PyLongObject *a, PyLongObject *b) Py_DECREF(a2); } else { - shiftby = PyLong_AsSsize_t((PyObject *)b); - if (shiftby >= 0) { - wordshift = shiftby / PyLong_SHIFT; - loshift = shiftby % PyLong_SHIFT; - } - else { - /* PyLong_Check(b) is true and Py_SIZE(b) >= 0, so it must be that - PyLong_AsSsize_t raised an OverflowError. */ - assert(PyErr_ExceptionMatches(PyExc_OverflowError)); - PyErr_Clear(); - /* With such large wordshift return PyLong_FromLong(0) below. */ - wordshift = PY_SSIZE_T_MAX / sizeof(digit); - loshift = 0; -#if SIZE_MAX / 2 < ULLONG_MAX / 15 - if (Py_SIZE(a) > PY_SSIZE_T_MAX / PyLong_SHIFT) { - int overflow; - long long lshiftby = PyLong_AsLongLongAndOverflow((PyObject *)b, - &overflow); - assert(!PyErr_Occurred()); - if (lshiftby >= 0 && lshiftby / PyLong_SHIFT < wordshift) { - wordshift = lshiftby / PyLong_SHIFT; - loshift = lshiftby % PyLong_SHIFT; - } - } -#endif - } + divmod_shift((PyObject *)b, &wordshift, &loshift); newsize = Py_SIZE(a) - wordshift; if (newsize <= 0) return PyLong_FromLong(0); @@ -4366,7 +4374,7 @@ long_lshift(PyObject *v, PyObject *w) PyLongObject *a = (PyLongObject*)v; PyLongObject *b = (PyLongObject*)w; PyLongObject *z = NULL; - Py_ssize_t shiftby, oldsize, newsize, wordshift, remshift, i, j; + Py_ssize_t oldsize, newsize, wordshift, remshift, i, j; twodigits accum; CHECK_BINOP(a, b); @@ -4379,32 +4387,7 @@ long_lshift(PyObject *v, PyObject *w) return PyLong_FromLong(0); } - shiftby = PyLong_AsSsize_t((PyObject *)b); - /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ - if (shiftby >= 0) { - wordshift = shiftby / PyLong_SHIFT; - remshift = shiftby % PyLong_SHIFT; - } - else { - /* PyLong_Check(b) is true and Py_SIZE(b) >= 0, so it must be that - PyLong_AsSsize_t raised an OverflowError. */ - assert(PyErr_ExceptionMatches(PyExc_OverflowError)); - PyErr_Clear(); - /* With such large wordshift raise error in _PyLong_New() below. */ - wordshift = PY_SSIZE_T_MAX / sizeof(digit); - remshift = 0; -#if SIZE_MAX / 2 < ULLONG_MAX / 15 - int overflow; - long long lshiftby = PyLong_AsLongLongAndOverflow((PyObject *)b, - &overflow); - assert(!PyErr_Occurred()); - if (lshiftby >= 0 && lshiftby / PyLong_SHIFT < wordshift) { - wordshift = lshiftby / PyLong_SHIFT; - remshift = lshiftby % PyLong_SHIFT; - } -#endif - } - + divmod_shift((PyObject *)b, &wordshift, &remshift); oldsize = Py_ABS(Py_SIZE(a)); newsize = oldsize + wordshift; if (remshift) From 0c22c824933a4f7a43355f6ccb9d069886a0f40b Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 22 Mar 2017 21:28:19 +0200 Subject: [PATCH 5/5] Use Python int arithmetic rather than C long long arithmetic. --- Objects/longobject.c | 59 +++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/Objects/longobject.c b/Objects/longobject.c index 2517e46339139f..e9e309f0a50461 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -4286,44 +4286,44 @@ long_bool(PyLongObject *v) } /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ -static void -divmod_shift(PyObject *shiftby, Py_ssize_t *wordshift, Py_ssize_t *remshift) +static int +divmod_shift(PyLongObject *shiftby, Py_ssize_t *wordshift, digit *remshift) { - assert(PyLong_Check(shiftby)); + assert(PyLong_Check((PyObject *)shiftby)); assert(Py_SIZE(shiftby) >= 0); - Py_ssize_t lshiftby = PyLong_AsSsize_t(shiftby); + Py_ssize_t lshiftby = PyLong_AsSsize_t((PyObject *)shiftby); if (lshiftby >= 0) { *wordshift = lshiftby / PyLong_SHIFT; *remshift = lshiftby % PyLong_SHIFT; + return 0; } - else { - /* PyLong_Check(shiftby) is true and Py_SIZE(shiftby) >= 0, so it must - be that PyLong_AsSsize_t raised an OverflowError. */ - assert(PyErr_ExceptionMatches(PyExc_OverflowError)); - PyErr_Clear(); - /* Clip the value. With such large wordshift the right shift - returns 0 and the left shift raises an error in _PyLong_New(). */ - *wordshift = PY_SSIZE_T_MAX / sizeof(digit); - *remshift = 0; -#if SIZE_MAX / 2 < ULLONG_MAX / 15 - int overflow; - long long llshiftby = PyLong_AsLongLongAndOverflow(shiftby, &overflow); - assert(!PyErr_Occurred()); - if (llshiftby >= 0 /* no overflow */ && - llshiftby / PyLong_SHIFT < PY_SSIZE_T_MAX / sizeof(digit)) { - *wordshift = llshiftby / PyLong_SHIFT; - *remshift = llshiftby % PyLong_SHIFT; - } -#endif + /* PyLong_Check(shiftby) is true and Py_SIZE(shiftby) >= 0, so it must + be that PyLong_AsSsize_t raised an OverflowError. */ + assert(PyErr_ExceptionMatches(PyExc_OverflowError)); + PyErr_Clear(); + PyLongObject *wordshift_obj = divrem1(shiftby, PyLong_SHIFT, remshift); + if (wordshift_obj == NULL) { + return -1; + } + *wordshift = PyLong_AsSsize_t((PyObject *)wordshift_obj); + Py_DECREF(wordshift_obj); + if (*wordshift >= 0 && *wordshift < PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(digit)) { + return 0; } + PyErr_Clear(); + /* Clip the value. With such large wordshift the right shift + returns 0 and the left shift raises an error in _PyLong_New(). */ + *wordshift = PY_SSIZE_T_MAX / sizeof(digit); + *remshift = 0; + return 0; } static PyObject * long_rshift(PyLongObject *a, PyLongObject *b) { PyLongObject *z = NULL; - Py_ssize_t newsize, wordshift, loshift, hishift, i, j; - digit lomask, himask; + Py_ssize_t newsize, wordshift, hishift, i, j; + digit loshift, lomask, himask; CHECK_BINOP(a, b); @@ -4347,7 +4347,8 @@ long_rshift(PyLongObject *a, PyLongObject *b) Py_DECREF(a2); } else { - divmod_shift((PyObject *)b, &wordshift, &loshift); + if (divmod_shift(b, &wordshift, &loshift) < 0) + return NULL; newsize = Py_SIZE(a) - wordshift; if (newsize <= 0) return PyLong_FromLong(0); @@ -4374,7 +4375,8 @@ long_lshift(PyObject *v, PyObject *w) PyLongObject *a = (PyLongObject*)v; PyLongObject *b = (PyLongObject*)w; PyLongObject *z = NULL; - Py_ssize_t oldsize, newsize, wordshift, remshift, i, j; + Py_ssize_t oldsize, newsize, wordshift, i, j; + digit remshift; twodigits accum; CHECK_BINOP(a, b); @@ -4387,7 +4389,8 @@ long_lshift(PyObject *v, PyObject *w) return PyLong_FromLong(0); } - divmod_shift((PyObject *)b, &wordshift, &remshift); + if (divmod_shift(b, &wordshift, &remshift) < 0) + return NULL; oldsize = Py_ABS(Py_SIZE(a)); newsize = oldsize + wordshift; if (remshift)