summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2012-12-28 08:09:54 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2012-12-28 08:09:54 (GMT)
commitc819b077bb9125db303651a383fb03e68c822084 (patch)
treef037dec4edc5e92224eaf1a6b6ec5c06eff72e75
parentd6c18dcd20ec4cc94c2c99aff165780d16a6f13b (diff)
parent00e284311565c2caeadd10faa75cbe261b71bdaf (diff)
downloadcpython-c819b077bb9125db303651a383fb03e68c822084.zip
cpython-c819b077bb9125db303651a383fb03e68c822084.tar.gz
cpython-c819b077bb9125db303651a383fb03e68c822084.tar.bz2
Issue #16761: Raise TypeError when int() called with base argument only.
-rw-r--r--Lib/test/test_int.py12
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/longobject.c12
3 files changed, 14 insertions, 13 deletions
diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py
index db79926..09b9a77 100644
--- a/Lib/test/test_int.py
+++ b/Lib/test/test_int.py
@@ -233,16 +233,8 @@ 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."""
diff --git a/Misc/NEWS b/Misc/NEWS
index 334d439..e2a08dd 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.4.0 Alpha 1?
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
index 49e9d5d..5a50f24 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -4255,13 +4255,19 @@ 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);
if (!PyLong_Check(obase)) {
PyErr_SetString(PyExc_TypeError,
- "int() arg 2 must be an integer.");
+ "int() base must be an integer.");
return NULL;
}
@@ -4270,7 +4276,7 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
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;
}