]> granicus.if.org Git - python/commitdiff
Test for issue16772 and redoes the previous fix to accept __index__-aware
authorGregory P. Smith <greg@krypto.org>
Wed, 26 Dec 2012 06:38:32 +0000 (22:38 -0800)
committerGregory P. Smith <greg@krypto.org>
Wed, 26 Dec 2012 06:38:32 +0000 (22:38 -0800)
objects as the base by using PyNumber_AsSsize_t similar to round().

Lib/test/test_int.py
Objects/longobject.c

index e0406dd4f9d819656480969540f9767838785a1f..fdb84a0be8a7f93ade719395dd49a55878f31230 100644 (file)
@@ -240,6 +240,30 @@ class IntTestCases(unittest.TestCase):
         self.assertEquals(int(base=1000), 0)
         self.assertEquals(int(base='foo'), 0)
 
+    def test_int_base_limits(self):
+        """Testing the supported limits of the int() base parameter."""
+        self.assertEqual(int('0', 5), 0)
+        with self.assertRaises(ValueError):
+            int('0', 1)
+        with self.assertRaises(ValueError):
+            int('0', 37)
+        with self.assertRaises(ValueError):
+            int('0', -909)  # An old magic value base from Python 2.
+        with self.assertRaises(ValueError):
+            int('0', base=0-(2**234))
+        with self.assertRaises(ValueError):
+            int('0', base=2**234)
+        # Bases 2 through 36 are supported.
+        for base in range(2,37):
+            self.assertEqual(int('0', base=base), 0)
+
+    def test_int_base_bad_types(self):
+        """Not integer types are not valid bases; issue16772."""
+        with self.assertRaises(TypeError):
+            int('0', 5.5)
+        with self.assertRaises(TypeError):
+            int('0', 5.0)
+
     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 e4d4df46f3a2885a4f96a287d3d4da7a36e1917d..cea2f730a4148c297ef26e49976e37050c4db10e 100644 (file)
@@ -4247,8 +4247,7 @@ static PyObject *
 long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
     PyObject *obase = NULL, *x = NULL;
-    long base;
-    int overflow;
+    Py_ssize_t base;
     static char *kwlist[] = {"x", "base", 0};
 
     if (type != &PyLong_Type)
@@ -4266,10 +4265,10 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
         return NULL;
     }
 
-    base = PyLong_AsLongAndOverflow(obase, &overflow);
+    base = PyNumber_AsSsize_t(obase, NULL);
     if (base == -1 && PyErr_Occurred())
         return NULL;
-    if (overflow || (base != 0 && base < 2) || base > 36) {
+    if ((base != 0 && base < 2) || base > 36) {
         PyErr_SetString(PyExc_ValueError,
                         "int() arg 2 must be >= 2 and <= 36");
         return NULL;