@@ -2005,6 +2005,14 @@ long_from_binary_base(char **str, int base)
20052005 return long_normalize (z );
20062006}
20072007
2008+ /* Parses a long from a bytestring. Leading and trailing whitespace will be
2009+ * ignored.
2010+ *
2011+ * If successful, a PyLong object will be returned and 'pend' will be pointing
2012+ * to the first unused byte unless it's NULL.
2013+ *
2014+ * If unsuccessful, NULL will be returned.
2015+ */
20082016PyObject *
20092017PyLong_FromString (char * str , char * * pend , int base )
20102018{
@@ -2267,12 +2275,17 @@ digit beyond the first.
22672275 str ++ ;
22682276 if (* str != '\0' )
22692277 goto onError ;
2270- if (pend )
2271- * pend = str ;
22722278 long_normalize (z );
2273- return (PyObject * ) maybe_small_long (z );
2279+ z = maybe_small_long (z );
2280+ if (z == NULL )
2281+ return NULL ;
2282+ if (pend != NULL )
2283+ * pend = str ;
2284+ return (PyObject * ) z ;
22742285
22752286 onError :
2287+ if (pend != NULL )
2288+ * pend = str ;
22762289 Py_XDECREF (z );
22772290 slen = strlen (orig_str ) < 200 ? strlen (orig_str ) : 200 ;
22782291 strobj = PyUnicode_FromStringAndSize (orig_str , slen );
@@ -2285,6 +2298,31 @@ digit beyond the first.
22852298 return NULL ;
22862299}
22872300
2301+ /* Since PyLong_FromString doesn't have a length parameter,
2302+ * check here for possible NULs in the string.
2303+ *
2304+ * Reports an invalid literal as a bytes object.
2305+ */
2306+ PyObject *
2307+ _PyLong_FromBytes (const char * s , Py_ssize_t len , int base )
2308+ {
2309+ PyObject * result , * strobj ;
2310+ char * end = NULL ;
2311+
2312+ result = PyLong_FromString ((char * )s , & end , base );
2313+ if (end == NULL || (result != NULL && end == s + len ))
2314+ return result ;
2315+ Py_XDECREF (result );
2316+ strobj = PyBytes_FromStringAndSize (s , Py_MIN (len , 200 ));
2317+ if (strobj != NULL ) {
2318+ PyErr_Format (PyExc_ValueError ,
2319+ "invalid literal for int() with base %d: %R" ,
2320+ base , strobj );
2321+ Py_DECREF (strobj );
2322+ }
2323+ return NULL ;
2324+ }
2325+
22882326PyObject *
22892327PyLong_FromUnicode (Py_UNICODE * u , Py_ssize_t length , int base )
22902328{
@@ -2299,9 +2337,8 @@ PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
22992337PyObject *
23002338PyLong_FromUnicodeObject (PyObject * u , int base )
23012339{
2302- PyObject * result ;
2303- PyObject * asciidig ;
2304- char * buffer , * end ;
2340+ PyObject * result , * asciidig , * strobj ;
2341+ char * buffer , * end = NULL ;
23052342 Py_ssize_t buflen ;
23062343
23072344 asciidig = _PyUnicode_TransformDecimalAndSpaceToASCII (u );
@@ -2310,17 +2347,26 @@ PyLong_FromUnicodeObject(PyObject *u, int base)
23102347 buffer = PyUnicode_AsUTF8AndSize (asciidig , & buflen );
23112348 if (buffer == NULL ) {
23122349 Py_DECREF (asciidig );
2313- return NULL ;
2350+ if (!PyErr_ExceptionMatches (PyExc_UnicodeEncodeError ))
2351+ return NULL ;
23142352 }
2315- result = PyLong_FromString (buffer , & end , base );
2316- if (result != NULL && end != buffer + buflen ) {
2317- PyErr_SetString (PyExc_ValueError ,
2318- "null byte in argument for int()" );
2319- Py_DECREF (result );
2320- result = NULL ;
2353+ else {
2354+ result = PyLong_FromString (buffer , & end , base );
2355+ if (end == NULL || (result != NULL && end == buffer + buflen )) {
2356+ Py_DECREF (asciidig );
2357+ return result ;
2358+ }
2359+ Py_DECREF (asciidig );
2360+ Py_XDECREF (result );
23212361 }
2322- Py_DECREF (asciidig );
2323- return result ;
2362+ strobj = PySequence_GetSlice (u , 0 , 200 );
2363+ if (strobj != NULL ) {
2364+ PyErr_Format (PyExc_ValueError ,
2365+ "invalid literal for int() with base %d: %R" ,
2366+ base , strobj );
2367+ Py_DECREF (strobj );
2368+ }
2369+ return NULL ;
23242370}
23252371
23262372/* forward */
@@ -4308,23 +4354,12 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
43084354 if (PyUnicode_Check (x ))
43094355 return PyLong_FromUnicodeObject (x , (int )base );
43104356 else if (PyByteArray_Check (x ) || PyBytes_Check (x )) {
4311- /* Since PyLong_FromString doesn't have a length parameter,
4312- * check here for possible NULs in the string. */
43134357 char * string ;
4314- Py_ssize_t size = Py_SIZE (x );
43154358 if (PyByteArray_Check (x ))
43164359 string = PyByteArray_AS_STRING (x );
43174360 else
43184361 string = PyBytes_AS_STRING (x );
4319- if (strlen (string ) != (size_t )size || !size ) {
4320- /* We only see this if there's a null byte in x or x is empty,
4321- x is a bytes or buffer, *and* a base is given. */
4322- PyErr_Format (PyExc_ValueError ,
4323- "invalid literal for int() with base %d: %R" ,
4324- (int )base , x );
4325- return NULL ;
4326- }
4327- return PyLong_FromString (string , NULL , (int )base );
4362+ return _PyLong_FromBytes (string , Py_SIZE (x ), (int )base );
43284363 }
43294364 else {
43304365 PyErr_SetString (PyExc_TypeError ,
0 commit comments