[pypy-svn] pypy fast-forward: (lac, arigo)

arigo commits-noreply at bitbucket.org
Sun Jan 16 19:23:29 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: fast-forward
Changeset: r40732:72d84754c044
Date: 2011-01-16 19:22 +0100
http://bitbucket.org/pypy/pypy/changeset/72d84754c044/

Log:	(lac, arigo)

	Fix longs in the same way that we fixed ints.

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
@@ -273,6 +273,24 @@
             pass
         assert long(myotherlong(21)) == 21L
 
+    def test___long__(self):
+        class A(object):
+            def __long__(self):
+                return 42
+        assert long(A()) == 42L
+        class B(object):
+            def __int__(self):
+                return 42
+        raises(TypeError, long, B())
+        # but!: (blame CPython 2.7)
+        class Integral(object):
+            def __int__(self):
+                return 42
+        class TruncReturnsNonLong(object):
+            def __trunc__(self):
+                return Integral()
+        assert long(TruncReturnsNonLong()) == 42
+
     def test_conjugate(self):
         assert (7L).conjugate() == 7L
         assert (-7L).conjugate() == -7L

diff --git a/lib-python/2.7.0/test/test_long.py b/lib-python/modified-2.7.0/test/test_long.py
copy from lib-python/2.7.0/test/test_long.py
copy to lib-python/modified-2.7.0/test/test_long.py
--- a/lib-python/2.7.0/test/test_long.py
+++ b/lib-python/modified-2.7.0/test/test_long.py
@@ -530,9 +530,10 @@
                 try:
                     long(TruncReturnsNonIntegral())
                 except TypeError as e:
-                    self.assertEquals(str(e),
-                                      "__trunc__ returned non-Integral"
-                                      " (type NonIntegral)")
+                    if test_support.check_impl_detail(cpython=True):
+                        self.assertEquals(str(e),
+                                          "__trunc__ returned non-Integral"
+                                          " (type NonIntegral)")
                 else:
                     self.fail("Failed to raise TypeError with %s" %
                               ((base, trunc_result_base),))

diff --git a/pypy/objspace/std/longtype.py b/pypy/objspace/std/longtype.py
--- a/pypy/objspace/std/longtype.py
+++ b/pypy/objspace/std/longtype.py
@@ -36,13 +36,18 @@
                 raise OperationError(space.w_ValueError,
                                      space.wrap(e.msg))
         else:
-            # otherwise, use the __long__(), then the __trunc__ methods
-            try:
-                w_obj = space.long(w_value)
-            except OperationError, e:
-                if not e.match(space,space.w_TypeError):
-                    raise
-                w_obj = space.trunc(w_value)
+            # otherwise, use the __long__() or the __trunc__ methods
+            w_obj = w_value
+            if (space.lookup(w_obj, '__long__') is not None or
+                space.lookup(w_obj, '__int__') is not None):
+                w_obj = space.long(w_obj)
+            else:
+                w_obj = space.trunc(w_obj)
+                # :-(  blame CPython 2.7
+                if space.lookup(w_obj, '__long__') is not None:
+                    w_obj = space.long(w_obj)
+                else:
+                    w_obj = space.int(w_obj)
             # 'long(x)' should return whatever x.__long__() returned
             if space.is_w(w_longtype, space.w_long):
                 return w_obj


More information about the Pypy-commit mailing list