[pypy-svn] r5117 - in pypy/trunk/src/pypy: interpreter/test objspace/std

arigo at codespeak.net arigo at codespeak.net
Wed Jun 16 16:56:40 CEST 2004


Author: arigo
Date: Wed Jun 16 16:56:39 2004
New Revision: 5117

Modified:
   pypy/trunk/src/pypy/interpreter/test/test_class.py
   pypy/trunk/src/pypy/objspace/std/intobject.py
Log:
int(x) should not return the object x if it is of a subclass of int.
Added a test and fixed.


Modified: pypy/trunk/src/pypy/interpreter/test/test_class.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/test/test_class.py	(original)
+++ pypy/trunk/src/pypy/interpreter/test/test_class.py	Wed Jun 16 16:56:39 2004
@@ -86,5 +86,14 @@
         self.assert_(isinstance(object.__new__(A), A))
         self.assert_(isinstance(A.__new__(A), A))
 
+    def test_int_subclass(self):
+        class R(int):
+            pass
+        x = R(5)
+        self.assertEquals(type(x), R)
+        self.assertEquals(x, 5)
+        self.assertEquals(type(int(x)), int)
+        self.assertEquals(int(x), 5)
+
 if __name__ == '__main__':
     testit.main()

Modified: pypy/trunk/src/pypy/objspace/std/intobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/intobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/intobject.py	Wed Jun 16 16:56:39 2004
@@ -287,10 +287,7 @@
 # a derived integer object, where it should return
 # an exact one.
 def pos__Int(space, w_int1):
-    if space.is_true(space.is_(space.type(w_int1), space.w_int)):
-        return w_int1
-    a = w_int1.intval
-    return W_IntObject(space, a)
+    return int__Int(space, w_int1)
 
 def abs__Int(space, w_int1):
     if w_int1.intval >= 0:
@@ -313,7 +310,7 @@
         raise OperationError(space.w_ValueError,
                              space.wrap("negative shift count"))
     if a == 0 or b == 0:
-        return pos__Int(space, w_int1)
+        return int__Int(space, w_int1)
     if b >= LONG_BIT:
         raise FailedToImplement(space.w_OverflowError,
                                 space.wrap("integer left shift"))
@@ -343,7 +340,7 @@
         raise OperationError(space.w_ValueError,
                              space.wrap("negative shift count"))
     if a == 0 or b == 0:
-        return pos__Int(space, w_int1)
+        return int__Int(space, w_int1)
     if b >= LONG_BIT:
         if a < 0:
             a = -1
@@ -386,8 +383,14 @@
 ##    return 1; /* Can't do it */
 ##}
 
+# int__Int is supposed to do nothing, unless it has
+# a derived integer object, where it should return
+# an exact one.
 def int__Int(space, w_int1):
-    return w_int1
+    if space.is_true(space.is_(space.type(w_int1), space.w_int)):
+        return w_int1
+    a = w_int1.intval
+    return W_IntObject(space, a)
 
 """
 # Not registered



More information about the Pypy-commit mailing list