diff -r 6ccb04c4cbae Lib/test/test_int.py --- a/Lib/test/test_int.py Fri Sep 28 01:15:39 2012 -0700 +++ b/Lib/test/test_int.py Sun Sep 30 11:56:07 2012 +0100 @@ -74,7 +74,6 @@ self.assertEqual(x >> 1, x//2) self.assertRaises(ValueError, int, '123\0') - self.assertRaises(ValueError, int, '53', 40) # SF bug 1545497: embedded NULs were not detected with # explicit base @@ -317,6 +316,14 @@ with self.assertRaises(TypeError): int(TruncReturnsBadInt()) + def test_bad_base(self): + self.assertRaises(ValueError, int, '53', 40) + + with self.assertRaises(ValueError) as e: + int('100', 1) + self.assertEquals(str(e.exception), + "int() base must be >= 2 and <= 36, or 0") + def test_error_message(self): testlist = ('\xbd', '123\xbd', ' 123 456 ') for s in testlist: diff -r 6ccb04c4cbae Modules/_testcapimodule.c --- a/Modules/_testcapimodule.c Fri Sep 28 01:15:39 2012 -0700 +++ b/Modules/_testcapimodule.c Sun Sep 30 11:56:07 2012 +0100 @@ -350,6 +350,40 @@ #undef F_U_TO_PY #undef F_PY_TO_U +/* + Test that correct error is raised when parsing a long from a string when + given an invalid base. +*/ + +static PyObject * +test_bad_base(PyObject *self) +{ + PyObject *num; + PyObject *exc_type, *exc_value, *exc_traceback; + char *msg; + +#define CHECK_INVALID_LONG(STR, BASE) \ + num = PyLong_FromString(STR, NULL, BASE); \ + if (num == NULL && PyErr_Occurred()) { \ + PyErr_Fetch(&exc_type, &exc_value, &exc_traceback); \ + if (!(PyUnicode_CheckExact(exc_value) && \ + PyUnicode_CompareWithASCIIString(exc_value, \ + "int() base must be >= 2 and <= 36, or 0") == 0)) { \ + msg = "Incorrect error string"; \ + goto fail; \ + } \ + } + + CHECK_INVALID_LONG("123", -1); + CHECK_INVALID_LONG("123", 1); + CHECK_INVALID_LONG("123", 37); + CHECK_INVALID_LONG("123", 456); + + Py_RETURN_NONE; + fail: + return raiseTestError("test_long_error", msg); +} + /* Test the PyLong_AsLongAndOverflow API. General conversion to PY_LONG is tested by test_long_api_inner. This test will concentrate on proper handling of overflow. @@ -2441,6 +2475,7 @@ {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, + {"test_bad_base", (PyCFunction)test_bad_base, METH_NOARGS}, {"test_long_and_overflow", (PyCFunction)test_long_and_overflow, METH_NOARGS}, {"test_long_as_double", (PyCFunction)test_long_as_double,METH_NOARGS}, diff -r 6ccb04c4cbae Objects/longobject.c --- a/Objects/longobject.c Fri Sep 28 01:15:39 2012 -0700 +++ b/Objects/longobject.c Sun Sep 30 11:56:07 2012 +0100 @@ -1991,7 +1991,7 @@ 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, or 0"); return NULL; } while (*str != '\0' && isspace(Py_CHARMASK(*str))) @@ -4270,7 +4270,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, or 0"); return NULL; }