[Python-checkins] cpython (merge 2.7 -> 2.7): Merge heads

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


http://hg.python.org/cpython/rev/75eac2794deb
changeset:   81127:75eac2794deb
branch:      2.7
parent:      81122:14f99221d937
parent:      81123:c0266ba8e4c6
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Fri Dec 28 10:16:47 2012 +0200
summary:
  Merge heads

files:
  Lib/test/test_int.py |  14 ++------------
  Misc/NEWS            |   3 +++
  Objects/intobject.c  |   8 +++++++-
  Objects/longobject.c |   8 +++++++-
  4 files changed, 19 insertions(+), 14 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
@@ -60,6 +60,8 @@
         self.assertEqual(self.ntype(x=1.2), 1)
         self.assertEqual(self.ntype('100', base=2), 4)
         self.assertEqual(self.ntype(x='100', base=2), 4)
+        self.assertRaises(TypeError, self.ntype, base=10)
+        self.assertRaises(TypeError, self.ntype, base=0)
 
 class IntTestCases(IntLongCommonTests, unittest.TestCase):
 
@@ -365,18 +367,6 @@
 
     def test_error_on_string_base(self):
         self.assertRaises(TypeError, int, 100, base='foo')
-        # Include the following because in contrast CPython raises no error
-        # for bad integer bases when x is not given.
-        self.assertRaises(TypeError, int, base='foo')
-
-    # For example, PyPy 1.9.0 raised TypeError for these cases because it
-    # expects x to be a string if base is given.
-    @test_support.cpython_only
-    def test_int_base_without_x_returns_0(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)
 
     @test_support.cpython_only
     def test_small_ints(self):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@
 Core and Builtins
 -----------------
 
+- Issue #16761: Calling ``int()`` and ``long()`` 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/intobject.c b/Objects/intobject.c
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -1059,8 +1059,14 @@
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
                                      &x, &base))
         return NULL;
-    if (x == NULL)
+    if (x == NULL) {
+        if (base != -909) {
+            PyErr_SetString(PyExc_TypeError,
+                            "int() missing string argument");
+            return NULL;
+        }
         return PyInt_FromLong(0L);
+    }
     if (base == -909)
         return PyNumber_Int(x);
     if (PyString_Check(x)) {
diff --git a/Objects/longobject.c b/Objects/longobject.c
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -3987,8 +3987,14 @@
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
                                      &x, &base))
         return NULL;
-    if (x == NULL)
+    if (x == NULL) {
+        if (base != -909) {
+            PyErr_SetString(PyExc_TypeError,
+                            "long() missing string argument");
+            return NULL;
+        }
         return PyLong_FromLong(0L);
+    }
     if (base == -909)
         return PyNumber_Long(x);
     else if (PyString_Check(x)) {

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


More information about the Python-checkins mailing list