Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 73a1dfe

Browse files
committed
More bug 460020. When I is a subclass of int, disable the +I(whatever),
I(0) << whatever, I(0) >> whatever, I(whatever) << 0 and I(whatever) >> 0 optimizations.
1 parent 95fefc7 commit 73a1dfe

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

Lib/test/test_descr.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,11 @@ def __add__(self, other):
13661366
a = hexint(12345)
13671367
verify(int(a) == 12345)
13681368
verify(int(a).__class__ is int)
1369+
verify((+a).__class__ is int)
1370+
verify((a >> 0).__class__ is int)
1371+
verify((a << 0).__class__ is int)
1372+
verify((hexint(0) << 12).__class__ is int)
1373+
verify((hexint(0) >> 12).__class__ is int)
13691374

13701375
class octlong(long):
13711376
__slots__ = []

Objects/intobject.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,12 @@ int_neg(PyIntObject *v)
681681
static PyObject *
682682
int_pos(PyIntObject *v)
683683
{
684-
Py_INCREF(v);
685-
return (PyObject *)v;
684+
if (PyInt_CheckExact(v)) {
685+
Py_INCREF(v);
686+
return (PyObject *)v;
687+
}
688+
else
689+
return PyInt_FromLong(v->ob_ival);
686690
}
687691

688692
static PyObject *
@@ -716,10 +720,8 @@ int_lshift(PyIntObject *v, PyIntObject *w)
716720
PyErr_SetString(PyExc_ValueError, "negative shift count");
717721
return NULL;
718722
}
719-
if (a == 0 || b == 0) {
720-
Py_INCREF(v);
721-
return (PyObject *) v;
722-
}
723+
if (a == 0 || b == 0)
724+
return int_pos(v);
723725
if (b >= LONG_BIT) {
724726
return PyInt_FromLong(0L);
725727
}
@@ -737,10 +739,8 @@ int_rshift(PyIntObject *v, PyIntObject *w)
737739
PyErr_SetString(PyExc_ValueError, "negative shift count");
738740
return NULL;
739741
}
740-
if (a == 0 || b == 0) {
741-
Py_INCREF(v);
742-
return (PyObject *) v;
743-
}
742+
if (a == 0 || b == 0)
743+
return int_pos(v);
744744
if (b >= LONG_BIT) {
745745
if (a < 0)
746746
a = -1;

0 commit comments

Comments
 (0)