Author: Daniel Patrick <[email protected]>
Branch: py3.5
Changeset: r87407:1fc357671011
Date: 2016-09-26 21:00 +0100
http://bitbucket.org/pypy/pypy/changeset/1fc357671011/
Log: Update int(x, base=10) behaviour for Python 3.4.
"Changed in version 3.4: If base is not an instance of int and the
base object has a base.__index__ method, that method is called to
obtain an integer for the base. Previous versions used base.__int__
instead of base.__index__." (from
https://docs.python.org/3/library/functions.html#int)
Note that in CPython an error is returned if base.__index__ does not
exist on the non-int object, even if base.__int__ exists. Test
amended to confirm this error is raised.
diff --git a/pypy/objspace/std/intobject.py b/pypy/objspace/std/intobject.py
--- a/pypy/objspace/std/intobject.py
+++ b/pypy/objspace/std/intobject.py
@@ -892,7 +892,7 @@
return _string_to_int_or_long(space, w_inttype, w_value, buf)
else:
try:
- base = space.int_w(w_base)
+ base = space.getindex_w(w_base, None)
except OperationError as e:
if not e.match(space, space.w_OverflowError):
raise
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
@@ -525,12 +525,23 @@
class MyInt(object):
def __init__(self, x):
self.x = x
- def __int__(self):
+ def __index__(self):
return self.x
base = MyInt(24)
assert int('10', base) == 24
+ class MyNonIndexable(object):
+ def __init__(self, x):
+ self.x = x
+ def __int__(self):
+ return self.x
+
+ base = MyNonIndexable(24)
+ e = raises(TypeError, int, '10', base)
+ assert str(e.value) == ("'MyNonIndexable' object cannot be interpreted
"
+ "as an integer")
+
def test_truediv(self):
import operator
x = 1000000
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit