]> 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 07:42:11 +0000 (09:42 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Fri, 28 Dec 2012 07:42:11 +0000 (09:42 +0200)
Lib/test/test_int.py
Misc/NEWS
Objects/longobject.c

index 4f2224b9323d7f5d480a743300ae79ffdd029603..671b20a5841111502307a638e08480ba6491a5c5 100644 (file)
@@ -226,6 +226,14 @@ class IntTestCases(unittest.TestCase):
         self.assertIs(int(b'10'), 10)
         self.assertIs(int(b'-1'), -1)
 
+    def test_keyword_args(self):
+        # Test invoking int() using keyword arguments.
+        self.assertEqual(int(x=1.2), 1)
+        self.assertEqual(int('100', base=2), 4)
+        self.assertEqual(int(x='100', base=2), 4)
+        self.assertRaises(TypeError, int, base=10)
+        self.assertRaises(TypeError, int, base=0)
+
     def test_intconversion(self):
         # Test __int__()
         class ClassicMissingMethods:
index 3efa292408c85c07c7a1c85dc61270b2bec2d87b..356e58681197d16e546e36220a4eb4ef68d3b07c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.2.4
 Core and Builtins
 -----------------
 
+- Issue #16761: Calling ``int()`` with *base* argument only now raises
+  TypeError.
+
 - Issue #16759: Support the full DWORD (unsigned long) range in Reg2Py
   when retreiving a REG_DWORD value. This corrects functions like
   winreg.QueryValueEx that may have been returning truncated values.
index 3a675c4331924054643fe984fca8adfb8a7f915a..51da32955e60db1da50fa8fc357a5f03a1f40a67 100644 (file)
@@ -4130,8 +4130,14 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
     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);
 
@@ -4140,7 +4146,7 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
         return NULL;
     if (overflow || (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;
     }