[Python-checkins] cpython (3.2): Issue #16761: Raise TypeError when int() called with base argument only.

serhiy.storchaka python-checkins at python.org
Fri Dec 28 09:21:01 CET 2012


http://hg.python.org/cpython/rev/e4ea38a92c4d
changeset:   81124:e4ea38a92c4d
branch:      3.2
parent:      81118:b2cd12690a51
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Fri Dec 28 09:42:11 2012 +0200
summary:
  Issue #16761: Raise TypeError when int() called with base argument only.

files:
  Lib/test/test_int.py |   8 ++++++++
  Misc/NEWS            |   3 +++
  Objects/longobject.c |  10 ++++++++--
  3 files changed, 19 insertions(+), 2 deletions(-)


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
@@ -226,6 +226,14 @@
         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:
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 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.
diff --git a/Objects/longobject.c b/Objects/longobject.c
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -4130,8 +4130,14 @@
     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 @@
         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;
     }
 

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


More information about the Python-checkins mailing list