]> granicus.if.org Git - python/commitdiff
More bug 460020. When I is a subclass of int, disable the +I(whatever),
authorTim Peters <tim.peters@gmail.com>
Tue, 11 Sep 2001 21:44:14 +0000 (21:44 +0000)
committerTim Peters <tim.peters@gmail.com>
Tue, 11 Sep 2001 21:44:14 +0000 (21:44 +0000)
I(0) << whatever, I(0) >> whatever, I(whatever) << 0 and I(whatever) >> 0
optimizations.

Lib/test/test_descr.py
Objects/intobject.c

index 5c4db303359f5dbab8d476b9d1cb3882c7449431..e89f6425b9c258d775faba4337c650120e6d84d2 100644 (file)
@@ -1366,6 +1366,11 @@ def inherits():
     a = hexint(12345)
     verify(int(a) == 12345)
     verify(int(a).__class__ is int)
+    verify((+a).__class__ is int)
+    verify((a >> 0).__class__ is int)
+    verify((a << 0).__class__ is int)
+    verify((hexint(0) << 12).__class__ is int)
+    verify((hexint(0) >> 12).__class__ is int)
 
     class octlong(long):
         __slots__ = []
index 261edb84433f2b0d22ac03349a1da740845542ac..c93b3840ab28ddb1df65c184f6d83bd605e58e7b 100644 (file)
@@ -681,8 +681,12 @@ int_neg(PyIntObject *v)
 static PyObject *
 int_pos(PyIntObject *v)
 {
-       Py_INCREF(v);
-       return (PyObject *)v;
+       if (PyInt_CheckExact(v)) {
+               Py_INCREF(v);
+               return (PyObject *)v;
+       }
+       else
+               return PyInt_FromLong(v->ob_ival);
 }
 
 static PyObject *
@@ -716,10 +720,8 @@ int_lshift(PyIntObject *v, PyIntObject *w)
                PyErr_SetString(PyExc_ValueError, "negative shift count");
                return NULL;
        }
-       if (a == 0 || b == 0) {
-               Py_INCREF(v);
-               return (PyObject *) v;
-       }
+       if (a == 0 || b == 0)
+               return int_pos(v);
        if (b >= LONG_BIT) {
                return PyInt_FromLong(0L);
        }
@@ -737,10 +739,8 @@ int_rshift(PyIntObject *v, PyIntObject *w)
                PyErr_SetString(PyExc_ValueError, "negative shift count");
                return NULL;
        }
-       if (a == 0 || b == 0) {
-               Py_INCREF(v);
-               return (PyObject *) v;
-       }
+       if (a == 0 || b == 0)
+               return int_pos(v);
        if (b >= LONG_BIT) {
                if (a < 0)
                        a = -1;