[Python-checkins] cpython: Issue #16772: in int(x, base), non-integer bases must have an __index__ method.

mark.dickinson python-checkins at python.org
Sun Jan 27 11:18:06 CET 2013


http://hg.python.org/cpython/rev/79db70bd3188
changeset:   81774:79db70bd3188
user:        Mark Dickinson <dickinsm at gmail.com>
date:        Sun Jan 27 10:17:52 2013 +0000
summary:
  Issue #16772: in int(x, base), non-integer bases must have an __index__ method.

files:
  Doc/library/functions.rst |   6 ++++++
  Lib/test/test_int.py      |  17 +++++++++++++++++
  Misc/NEWS                 |   4 ++++
  Objects/longobject.c      |   5 -----
  4 files changed, 27 insertions(+), 5 deletions(-)


diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst
--- a/Doc/library/functions.rst
+++ b/Doc/library/functions.rst
@@ -665,6 +665,12 @@
 
    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)
 
diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py
--- a/Lib/test/test_int.py
+++ b/Lib/test/test_int.py
@@ -260,6 +260,23 @@
         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.
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@
 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.
 
diff --git a/Objects/longobject.c b/Objects/longobject.c
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -4283,11 +4283,6 @@
     }
     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())

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list