@@ -2000,6 +2000,14 @@ long_from_binary_base(char **str, int base)
20002000 return long_normalize (z );
20012001}
20022002
2003+ /* Parses a long from a bytestring. Leading and trailing whitespace will be
2004+ * ignored.
2005+ *
2006+ * If successful, a PyLong object will be returned and 'pend' will be pointing
2007+ * to the first unused byte unless it's NULL.
2008+ *
2009+ * If unsuccessful, NULL will be returned.
2010+ */
20032011PyObject *
20042012PyLong_FromString (char * str , char * * pend , int base )
20052013{
@@ -2262,24 +2270,54 @@ digit beyond the first.
22622270 str ++ ;
22632271 if (* str != '\0' )
22642272 goto onError ;
2265- if (pend )
2266- * pend = str ;
22672273 long_normalize (z );
2268- return (PyObject * ) maybe_small_long (z );
2274+ z = maybe_small_long (z );
2275+ if (z == NULL )
2276+ return NULL ;
2277+ if (pend != NULL )
2278+ * pend = str ;
2279+ return (PyObject * ) z ;
22692280
22702281 onError :
2282+ if (pend != NULL )
2283+ * pend = str ;
22712284 Py_XDECREF (z );
22722285 slen = strlen (orig_str ) < 200 ? strlen (orig_str ) : 200 ;
22732286 strobj = PyUnicode_FromStringAndSize (orig_str , slen );
22742287 if (strobj == NULL )
22752288 return NULL ;
22762289 PyErr_Format (PyExc_ValueError ,
2277- "invalid literal for int() with base %d: %R " ,
2290+ "invalid literal for int() with base %d: %.200R " ,
22782291 base , strobj );
22792292 Py_DECREF (strobj );
22802293 return NULL ;
22812294}
22822295
2296+ /* Since PyLong_FromString doesn't have a length parameter,
2297+ * check here for possible NULs in the string.
2298+ *
2299+ * Reports an invalid literal as a bytes object.
2300+ */
2301+ PyObject *
2302+ _PyLong_FromBytes (const char * s , Py_ssize_t len , int base )
2303+ {
2304+ PyObject * result , * strobj ;
2305+ char * end = NULL ;
2306+
2307+ result = PyLong_FromString ((char * )s , & end , base );
2308+ if (end == NULL || (result != NULL && end == s + len ))
2309+ return result ;
2310+ Py_XDECREF (result );
2311+ strobj = PyBytes_FromStringAndSize (s , Py_MIN (len , 200 ));
2312+ if (strobj != NULL ) {
2313+ PyErr_Format (PyExc_ValueError ,
2314+ "invalid literal for int() with base %d: %.200R" ,
2315+ base , strobj );
2316+ Py_DECREF (strobj );
2317+ }
2318+ return NULL ;
2319+ }
2320+
22832321PyObject *
22842322PyLong_FromUnicode (Py_UNICODE * u , Py_ssize_t length , int base )
22852323{
@@ -2294,9 +2332,8 @@ PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
22942332PyObject *
22952333PyLong_FromUnicodeObject (PyObject * u , int base )
22962334{
2297- PyObject * result ;
2298- PyObject * asciidig ;
2299- char * buffer , * end ;
2335+ PyObject * result , * asciidig ;
2336+ char * buffer , * end = NULL ;
23002337 Py_ssize_t buflen ;
23012338
23022339 asciidig = _PyUnicode_TransformDecimalAndSpaceToASCII (u );
@@ -2305,17 +2342,22 @@ PyLong_FromUnicodeObject(PyObject *u, int base)
23052342 buffer = PyUnicode_AsUTF8AndSize (asciidig , & buflen );
23062343 if (buffer == NULL ) {
23072344 Py_DECREF (asciidig );
2308- return NULL ;
2345+ if (!PyErr_ExceptionMatches (PyExc_UnicodeEncodeError ))
2346+ return NULL ;
23092347 }
2310- result = PyLong_FromString (buffer , & end , base );
2311- if (result != NULL && end != buffer + buflen ) {
2312- PyErr_SetString (PyExc_ValueError ,
2313- "null byte in argument for int()" );
2314- Py_DECREF (result );
2315- result = NULL ;
2348+ else {
2349+ result = PyLong_FromString (buffer , & end , base );
2350+ if (end == NULL || (result != NULL && end == buffer + buflen )) {
2351+ Py_DECREF (asciidig );
2352+ return result ;
2353+ }
2354+ Py_DECREF (asciidig );
2355+ Py_XDECREF (result );
23162356 }
2317- Py_DECREF (asciidig );
2318- return result ;
2357+ PyErr_Format (PyExc_ValueError ,
2358+ "invalid literal for int() with base %d: %.200R" ,
2359+ base , u );
2360+ return NULL ;
23192361}
23202362
23212363/* forward */
@@ -4319,23 +4361,12 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
43194361 if (PyUnicode_Check (x ))
43204362 return PyLong_FromUnicodeObject (x , (int )base );
43214363 else if (PyByteArray_Check (x ) || PyBytes_Check (x )) {
4322- /* Since PyLong_FromString doesn't have a length parameter,
4323- * check here for possible NULs in the string. */
43244364 char * string ;
4325- Py_ssize_t size = Py_SIZE (x );
43264365 if (PyByteArray_Check (x ))
43274366 string = PyByteArray_AS_STRING (x );
43284367 else
43294368 string = PyBytes_AS_STRING (x );
4330- if (strlen (string ) != (size_t )size || !size ) {
4331- /* We only see this if there's a null byte in x or x is empty,
4332- x is a bytes or buffer, *and* a base is given. */
4333- PyErr_Format (PyExc_ValueError ,
4334- "invalid literal for int() with base %d: %R" ,
4335- (int )base , x );
4336- return NULL ;
4337- }
4338- return PyLong_FromString (string , NULL , (int )base );
4369+ return _PyLong_FromBytes (string , Py_SIZE (x ), (int )base );
43394370 }
43404371 else {
43414372 PyErr_SetString (PyExc_TypeError ,
0 commit comments