summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2010-05-26 20:07:58 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2010-05-26 20:07:58 (GMT)
commitf9a5a8e0aff67061583b31afa114a6d025b1680d (patch)
treeebfb85fbd8c7e1f794a0b8ab5010d49446a0f7f7 /Objects
parentd5442cd37a391052723113e10f61a51da48f749e (diff)
downloadcpython-f9a5a8e0aff67061583b31afa114a6d025b1680d.zip
cpython-f9a5a8e0aff67061583b31afa114a6d025b1680d.tar.gz
cpython-f9a5a8e0aff67061583b31afa114a6d025b1680d.tar.bz2
Issue #2844: Make int('42', n) consistently raise ValueError for
invalid integers n (including n = -909).
Diffstat (limited to 'Objects')
-rw-r--r--Objects/longobject.c29
1 files changed, 20 insertions, 9 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 3eb0c44..ed41010 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -4098,23 +4098,34 @@ long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static PyObject *
long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
- PyObject *x = NULL;
- int base = -909; /* unlikely! */
+ PyObject *obase = NULL, *x = NULL;
+ long base;
+ int overflow;
static char *kwlist[] = {"x", "base", 0};
if (type != &PyLong_Type)
return long_subtype_new(type, args, kwds); /* Wimp out */
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
- &x, &base))
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:int", kwlist,
+ &x, &obase))
return NULL;
if (x == NULL)
return PyLong_FromLong(0L);
- if (base == -909)
+ if (obase == NULL)
return PyNumber_Long(x);
- else if (PyUnicode_Check(x))
+
+ base = PyLong_AsLongAndOverflow(obase, &overflow);
+ if (base == -1 && PyErr_Occurred())
+ return NULL;
+ if (overflow || (base != 0 && base < 2) || base > 36) {
+ PyErr_SetString(PyExc_ValueError,
+ "int() arg 2 must be >= 2 and <= 36");
+ return NULL;
+ }
+
+ if (PyUnicode_Check(x))
return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
PyUnicode_GET_SIZE(x),
- base);
+ (int)base);
else if (PyByteArray_Check(x) || PyBytes_Check(x)) {
/* Since PyLong_FromString doesn't have a length parameter,
* check here for possible NULs in the string. */
@@ -4129,10 +4140,10 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
x is a bytes or buffer, *and* a base is given. */
PyErr_Format(PyExc_ValueError,
"invalid literal for int() with base %d: %R",
- base, x);
+ (int)base, x);
return NULL;
}
- return PyLong_FromString(string, NULL, base);
+ return PyLong_FromString(string, NULL, (int)base);
}
else {
PyErr_SetString(PyExc_TypeError,