]> granicus.if.org Git - python/commitdiff
Issue #16761: Raise TypeError when int() called with base argument only.
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 28 Dec 2012 08:09:54 +0000 (10:09 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Fri, 28 Dec 2012 08:09:54 +0000 (10:09 +0200)
1  2 
Lib/test/test_int.py
Misc/NEWS
Objects/longobject.c

index db79926f8fa9c3821857371aa5a76be7ac6a16bb,c35a42f59673d05990a19f0d491b1dbf187c69a6..09b9a7785b4f71071d120dcc3f67cff1e9973e63
@@@ -233,41 -233,9 +233,33 @@@ class IntTestCases(unittest.TestCase)
          self.assertEqual(int(x=1.2), 1)
          self.assertEqual(int('100', base=2), 4)
          self.assertEqual(int(x='100', base=2), 4)
-     # For example, PyPy 1.9.0 raised TypeError for these cases because it
-     # expects x to be a string if base is given.
-     @support.cpython_only
-     def test_base_arg_with_no_x_arg(self):
-         self.assertEqual(int(base=6), 0)
-         # Even invalid bases don't raise an exception.
-         self.assertEqual(int(base=1), 0)
-         self.assertEqual(int(base=1000), 0)
-         self.assertEqual(int(base='foo'), 0)
+         self.assertRaises(TypeError, int, base=10)
+         self.assertRaises(TypeError, int, base=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.
diff --cc Misc/NEWS
Simple merge
index 49e9d5d9159295d7791c084902d03cca8bd24b9c,9535830658b587f79f10fa5c28f11f5c7d19b171..5a50f24330f03b0194f14ac96349639ee088b266
@@@ -4255,22 -4267,23 +4255,28 @@@ long_new(PyTypeObject *type, PyObject *
      if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:int", kwlist,
                                       &x, &obase))
          return NULL;
-     if (x == NULL)
+     if (x == NULL) {
+         if (obase != NULL) {
+             PyErr_SetString(PyExc_TypeError,
+                             "int() missing string argument");
+             return NULL;
+         }
          return PyLong_FromLong(0L);
+     }
      if (obase == NULL)
          return PyNumber_Long(x);
-                         "int() arg 2 must be an integer.");
 +    if (!PyLong_Check(obase)) {
 +        PyErr_SetString(PyExc_TypeError,
++                        "int() base must be an integer.");
 +        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");
+                         "int() base must be >= 2 and <= 36");
          return NULL;
      }