diff -r 62438d1b11c7 Objects/longobject.c --- a/Objects/longobject.c Fri May 02 23:26:03 2014 +0200 +++ b/Objects/longobject.c Fri May 02 23:50:54 2014 +0200 @@ -34,6 +34,9 @@ static PyLongObject small_ints[NSMALLNEG Py_ssize_t quick_int_allocs, quick_neg_int_allocs; #endif +/* Forward declaration */ +static PyObject* long_lshift(PyObject *v, PyObject *w); + static PyObject * get_small_int(sdigit ival) { @@ -3855,6 +3858,21 @@ long_pow(PyObject *v, PyObject *w, PyObj } } + if (c == NULL && Py_SIZE(a) == 1 && a->ob_digit[0] == 2) { + PyObject *one, *pow2; + one = PyLong_FromLong(1L); + if (one == NULL) { + Py_DECREF(a); + Py_DECREF(b); + return NULL; + } + pow2 = long_lshift(one, w); + Py_DECREF(a); + Py_DECREF(b); + Py_DECREF(one); + return pow2; + } + if (c) { /* if modulus == 0: raise ValueError() */ @@ -4118,6 +4136,12 @@ long_lshift(PyObject *v, PyObject *w) PyErr_SetString(PyExc_ValueError, "negative shift count"); return NULL; } + if (shiftby == 0) { + /* x << 0 = x */ + Py_INCREF(v); + return v; + } + /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ wordshift = shiftby / PyLong_SHIFT; remshift = shiftby - wordshift * PyLong_SHIFT;