Commit f2fd034c authored by Stefan Behnel's avatar Stefan Behnel

prevent "2**intsubtype" from being incorrectly optimised if intsubtype overrides __rpow__()

parent c8724307
...@@ -438,13 +438,13 @@ static PyObject* __Pyx__PyNumber_PowerOf2(PyObject *two, PyObject *exp, PyObject ...@@ -438,13 +438,13 @@ static PyObject* __Pyx__PyNumber_PowerOf2(PyObject *two, PyObject *exp, PyObject
static PyObject* __Pyx__PyNumber_PowerOf2(PyObject *two, PyObject *exp, PyObject *none, int inplace) { static PyObject* __Pyx__PyNumber_PowerOf2(PyObject *two, PyObject *exp, PyObject *none, int inplace) {
// in CPython, 1<<N is substantially faster than 2**N // in CPython, 1<<N is substantially faster than 2**N
// TODO: disable this in Py3.5 if http://bugs.python.org/issue21420 gets accepted // see http://bugs.python.org/issue21420
#if CYTHON_COMPILING_IN_CPYTHON #if CYTHON_COMPILING_IN_CPYTHON
Py_ssize_t shiftby; Py_ssize_t shiftby;
if (likely(PyLong_Check(exp))) { if (likely(PyLong_CheckExact(exp))) {
shiftby = PyLong_AsSsize_t(exp); shiftby = PyLong_AsSsize_t(exp);
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
} else if (likely(PyInt_Check(exp))) { } else if (likely(PyInt_CheckExact(exp))) {
shiftby = PyInt_AsLong(exp); shiftby = PyInt_AsLong(exp);
#endif #endif
} else { } else {
......
...@@ -67,6 +67,34 @@ def int_pow(short a, short b): ...@@ -67,6 +67,34 @@ def int_pow(short a, short b):
return a**b return a**b
class I(int):
"""
Copied from CPython's test_descr.py
>>> I(2) ** I(3)
I(8)
>>> 2 ** I(3)
I(8)
>>> I(3).pow2()
I(8)
"""
def __repr__(self):
return 'I(%s)' % int(self)
def __pow__(self, other, mod=None):
if mod is None:
return I(pow(int(self), int(other)))
else:
return I(pow(int(self), int(other), int(mod)))
def __rpow__(self, other, mod=None):
if mod is None:
return I(pow(int(other), int(self), mod))
else:
return I(pow(int(other), int(self), int(mod)))
def pow2(self):
return 2 ** self
def optimised_pow2(n): def optimised_pow2(n):
""" """
>>> optimised_pow2(0) >>> optimised_pow2(0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment