]> granicus.if.org Git - python/commitdiff
Issue #16772: in int(x, base), non-integer bases must have an __index__ method.
authorMark Dickinson <dickinsm@gmail.com>
Sun, 27 Jan 2013 10:17:52 +0000 (10:17 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Sun, 27 Jan 2013 10:17:52 +0000 (10:17 +0000)
Doc/library/functions.rst
Lib/test/test_int.py
Misc/NEWS
Objects/longobject.c

index c52dc06b30f1b1832909a7b18dc0cd98fe161196..64b46a3a5bcbb1188b092124cd8d6b1ac2a1fe12 100644 (file)
@@ -665,6 +665,12 @@ are always available.  They are listed here in alphabetical order.
 
    The integer type is described in :ref:`typesnumeric`.
 
+   .. versionchanged:: 3.4
+      If *base* is not an instance of :class:`int` and the *base* object has a
+      :meth:`base.__index__ <object.__index__>` method, that method is called
+      to obtain an integer for the base.  Previous versions used
+      :meth:`base.__int__ <object.__int__>` instead of :meth:`base.__index__
+      <object.__index__>`.
 
 .. function:: isinstance(object, classinfo)
 
index 09b9a7785b4f71071d120dcc3f67cff1e9973e63..afc91699a9198221187ea8d68bb2dfb222df59d3 100644 (file)
@@ -260,6 +260,23 @@ class IntTestCases(unittest.TestCase):
         with self.assertRaises(TypeError):
             int('0', 5.0)
 
+    def test_int_base_indexable(self):
+        class MyIndexable(object):
+            def __init__(self, value):
+                self.value = value
+            def __index__(self):
+                return self.value
+
+        # Check out of range bases.
+        for base in 2**100, -2**100, 1, 37:
+            with self.assertRaises(ValueError):
+                int('43', base)
+
+        # Check in-range bases.
+        self.assertEqual(int('101', base=MyIndexable(2)), 5)
+        self.assertEqual(int('101', base=MyIndexable(10)), 101)
+        self.assertEqual(int('101', base=MyIndexable(36)), 1 + 36**2)
+
     def test_non_numeric_input_types(self):
         # Test possible non-numeric types for the argument x, including
         # subclasses of the explicitly documented accepted types.
index 01842ce5c2fb566c83a76e69071b6a2625d9db5d..d2f15b383dc270e261aa4e18cc2be8f562479c9a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ What's New in Python 3.4.0 Alpha 1?
 Core and Builtins
 -----------------
 
+- Issue #16772: The base argument to the int constructor no longer accepts
+  floats, or other non-integer objects with an __int__ method.  Objects
+  with an __index__ method are now accepted.
+
 - Issue #10156: In the interpreter's initialization phase, unicode globals
   are now initialized dynamically as needed.
 
index 1a82b1c67ad44540dc4fcea244dbb751165bfa98..bec0a780084402ecf29559c9a83bded01b3e776a 100644 (file)
@@ -4283,11 +4283,6 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
     }
     if (obase == NULL)
         return PyNumber_Long(x);
-    if (!PyLong_Check(obase)) {
-        PyErr_SetString(PyExc_TypeError,
-                        "int() base must be an integer.");
-        return NULL;
-    }
 
     base = PyNumber_AsSsize_t(obase, NULL);
     if (base == -1 && PyErr_Occurred())