Author: Brian Kearns <[email protected]>
Branch: 
Changeset: r68126:6f5e3c868dc6
Date: 2013-11-14 20:12 -0500
http://bitbucket.org/pypy/pypy/changeset/6f5e3c868dc6/

Log:    test/fix int/trunc behavior

diff --git a/pypy/objspace/std/inttype.py b/pypy/objspace/std/inttype.py
--- a/pypy/objspace/std/inttype.py
+++ b/pypy/objspace/std/inttype.py
@@ -119,14 +119,14 @@
         if not ok:
             # otherwise, use the __int__() or the __trunc__() methods
             w_obj = w_value
-            if space.lookup(w_obj, '__int__') is not None:
-                w_obj = space.int(w_obj)
-            elif space.lookup(w_obj, '__trunc__') is not None:
-                w_obj = space.trunc(w_obj)
-            else:
-                raise operationerrfmt(space.w_TypeError,
-                    "int() argument must be a string or a number, not '%T'",
-                    w_obj)
+            if space.lookup(w_obj, '__int__') is None:
+                if space.lookup(w_obj, '__trunc__') is not None:
+                    w_obj = space.trunc(w_obj)
+                else:
+                    raise operationerrfmt(space.w_TypeError,
+                        "int() argument must be a string or a number, not 
'%T'",
+                        w_obj)
+            w_obj = space.int(w_obj)
             # 'int(x)' should return what x.__int__() returned, which should
             # be an int or long or a subclass thereof.
             if space.is_w(w_inttype, space.w_int):
diff --git a/pypy/objspace/std/test/test_intobject.py 
b/pypy/objspace/std/test/test_intobject.py
--- a/pypy/objspace/std/test/test_intobject.py
+++ b/pypy/objspace/std/test/test_intobject.py
@@ -7,7 +7,6 @@
 
 
 class TestW_IntObject:
-
     def _longshiftresult(self, x):
         """ calculate an overflowing shift """
         n = 1
@@ -40,7 +39,7 @@
         space = self.space
         assert isinstance(space.bigint_w(space.wrap(42)), rbigint)
         assert space.bigint_w(space.wrap(42)).eq(rbigint.fromint(42))
-        
+
     def test_repr(self):
         x = 1
         f1 = iobj.W_IntObject(x)
@@ -71,7 +70,7 @@
                     method = getattr(iobj, '%s__Int_Int' % op)
                     myres = method(self.space, wx, wy)
                     assert self.space.unwrap(myres) == res
-                    
+
     def test_add(self):
         x = 1
         y = 2
@@ -283,8 +282,8 @@
         result = iobj.hex__Int(self.space, f1)
         assert self.space.unwrap(result) == hex(x)
 
+
 class AppTestInt:
-
     def test_conjugate(self):
         assert (1).conjugate() == 1
         assert (-1).conjugate() == -1
@@ -326,7 +325,7 @@
         assert "42" == str(42)
         assert "42" == repr(42)
         raises(ValueError, int, '0x2A')
-        
+
     def test_int_two_param(self):
         assert 42 == int('0x2A', 0)
         assert 42 == int('2A', 16)
@@ -431,28 +430,28 @@
 
     def test_special_int(self):
         class a(object):
-            def __int__(self): 
-                self.ar = True 
+            def __int__(self):
+                self.ar = True
                 return None
         inst = a()
-        raises(TypeError, int, inst) 
+        raises(TypeError, int, inst)
         assert inst.ar == True 
 
-        class b(object): 
+        class b(object):
             pass 
         raises((AttributeError,TypeError), int, b()) 
 
     def test_special_long(self):
         class a(object):
-            def __long__(self): 
-                self.ar = True 
+            def __long__(self):
+                self.ar = True
                 return None
         inst = a()
-        raises(TypeError, long, inst) 
-        assert inst.ar == True 
+        raises(TypeError, long, inst)
+        assert inst.ar == True
 
-        class b(object): 
-            pass 
+        class b(object):
+            pass
         raises((AttributeError,TypeError), long, b())
 
     def test_just_trunc(self):
@@ -470,6 +469,15 @@
             pass
         assert int(myotherint(21)) == 21
 
+    def test_trunc_returns_non_int(self):
+        class Integral(object):
+            def __int__(self):
+                return 42
+        class TruncReturnsNonInt(object):
+            def __trunc__(self):
+                return Integral()
+        assert int(TruncReturnsNonInt()) == 42
+
     def test_getnewargs(self):
         assert  0 .__getnewargs__() == (0,)
 
diff --git a/pypy/objspace/std/test/test_longobject.py 
b/pypy/objspace/std/test/test_longobject.py
--- a/pypy/objspace/std/test/test_longobject.py
+++ b/pypy/objspace/std/test/test_longobject.py
@@ -41,7 +41,6 @@
 
 
 class AppTestLong:
-
     def test_trunc(self):
         import math
         assert math.trunc(1L) == 1L
@@ -311,7 +310,6 @@
         assert (-1<<40).bit_length() == 41
         assert ((2**31)-1).bit_length() == 31
 
-
     def test_negative_zero(self):
         x = eval("-0L")
         assert x == 0L
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to