[pypy-commit] pypy py3k: bytes(obj) should call obj.__index__() instead of obj.__int__().

mjacob noreply at buildbot.pypy.org
Fri Jun 5 19:57:38 CEST 2015


Author: Manuel Jacob <me at manueljacob.de>
Branch: py3k
Changeset: r77922:b84f64795e2b
Date: 2015-06-05 19:38 +0200
http://bitbucket.org/pypy/pypy/changeset/b84f64795e2b/

Log:	bytes(obj) should call obj.__index__() instead of obj.__int__().

	Fixes #1957.

diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -701,9 +701,10 @@
             raise OperationError(space.w_TypeError, space.wrap(
                     "encoding or errors without string argument"))
         return []
-    # Is it an int?
+    # Is it an integer?
+    # Note that we're calling space.getindex_w() instead of space.int_w().
     try:
-        count = space.int_w(w_source)
+        count = space.getindex_w(w_source, space.w_OverflowError)
     except OperationError, e:
         if not e.match(space, space.w_TypeError):
             raise
diff --git a/pypy/objspace/std/test/test_bytesobject.py b/pypy/objspace/std/test/test_bytesobject.py
--- a/pypy/objspace/std/test/test_bytesobject.py
+++ b/pypy/objspace/std/test/test_bytesobject.py
@@ -732,6 +732,18 @@
                 return [3, 4]
         raises(TypeError, bytes, Z())
 
+    def test_fromobject___index__(self):
+        class WithIndex:
+            def __index__(self):
+                return 3
+        assert bytes(WithIndex()) == b'\x00\x00\x00'
+
+    def test_fromobject___int__(self):
+        class WithInt:
+            def __int__(self):
+                return 3
+        raises(TypeError, bytes, WithInt())
+
     def test_getnewargs(self):
         assert  b"foo".__getnewargs__() == (b"foo",)
 


More information about the pypy-commit mailing list