[pypy-commit] pypy py3k: make sure that bin() calls __index__

antocuni noreply at buildbot.pypy.org
Wed Feb 29 12:10:53 CET 2012


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: py3k
Changeset: r53016:9a0f43982dd5
Date: 2012-02-29 11:50 +0100
http://bitbucket.org/pypy/pypy/changeset/9a0f43982dd5/

Log:	make sure that bin() calls __index__

diff --git a/pypy/module/__builtin__/app_operation.py b/pypy/module/__builtin__/app_operation.py
--- a/pypy/module/__builtin__/app_operation.py
+++ b/pypy/module/__builtin__/app_operation.py
@@ -1,4 +1,5 @@
+import operator
+
 def bin(x):
-    if not isinstance(x, int):
-        raise TypeError("%s object cannot be interpreted as an integer" % type(x))
+    x = operator.index(x)
     return x.__format__("#b")
diff --git a/pypy/module/__builtin__/test/test_builtin.py b/pypy/module/__builtin__/test/test_builtin.py
--- a/pypy/module/__builtin__/test/test_builtin.py
+++ b/pypy/module/__builtin__/test/test_builtin.py
@@ -77,10 +77,14 @@
             r"""'\'\x00"\n\r\t abcd\x85\xe9\U00012fff\ud800\U0001d121xxx.'"""
 
     def test_bin(self):
+        class Foo:
+            def __index__(self):
+                return 4
         assert bin(0) == "0b0"
         assert bin(-1) == "-0b1"
         assert bin(2) == "0b10"
         assert bin(-2) == "-0b10"
+        assert bin(Foo()) == "0b100"
         raises(TypeError, bin, 0.)
 
     def test_chr(self):


More information about the pypy-commit mailing list