Index: Objects/intobject.c =================================================================== --- Objects/intobject.c (revision 63206) +++ Objects/intobject.c (working copy) @@ -952,18 +952,31 @@ static PyObject * int_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - PyObject *x = NULL; - int base = -909; + PyObject *x = NULL, *baseo = NULL; + long base; static char *kwlist[] = {"x", "base", 0}; if (type != &PyInt_Type) return int_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, &baseo)) return NULL; + if (baseo) { + if (PyInt_Check(baseo)) + base = PyInt_AS_LONG(baseo); + else if (PyLong_Check(baseo)) { + base = PyLong_AsLong(baseo); + if (base == -1 && PyErr_Occurred()) + return NULL; + } + else { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return NULL; + } + } if (x == NULL) return PyInt_FromLong(0L); - if (base == -909) + if (baseo == NULL) return PyNumber_Int(x); if (PyString_Check(x)) { /* Since PyInt_FromString doesn't have a length parameter, Index: Objects/longobject.c =================================================================== --- Objects/longobject.c (revision 63206) +++ Objects/longobject.c (working copy) @@ -3319,18 +3319,31 @@ static PyObject * long_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - PyObject *x = NULL; - int base = -909; /* unlikely! */ + PyObject *x = NULL, *baseo = NULL; + long base; 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:long", kwlist, - &x, &base)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:long", kwlist, + &x, &baseo)) return NULL; + if (baseo) { + if (PyInt_Check(baseo)) + base = PyInt_AS_LONG(baseo); + else if (PyLong_Check(baseo)) { + base = PyLong_AsLong(baseo); + if (base == -1 && PyErr_Occurred()) + return NULL; + } + else { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return NULL; + } + } if (x == NULL) return PyLong_FromLong(0L); - if (base == -909) + if (baseo == NULL) return PyNumber_Long(x); else if (PyString_Check(x)) { /* Since PyLong_FromString doesn't have a length parameter, Index: Lib/test/test_long.py =================================================================== --- Lib/test/test_long.py (revision 63206) +++ Lib/test/test_long.py (working copy) @@ -398,7 +398,11 @@ self.assertEqual(long('2br45qc', 35), 4294967297) self.assertEqual(long('1z141z5', 36), 4294967297) + # issue2844 + self.assertRaises(ValueError, long, '42', -909) + self.assertRaises(TypeError, long, 42, 10) + def test_conversion(self): # Test __long__() class ClassicMissingMethods: