@@ -1650,6 +1650,119 @@ divrem1(PyLongObject *a, digit n, digit *prem)
16501650 return long_normalize (z );
16511651}
16521652
1653+ /* Convert a long integer to a base 10 string. Returns a new non-shared
1654+ string. (Return value is non-shared so that callers can modify the
1655+ returned value if necessary.) */
1656+
1657+ static PyObject *
1658+ long_to_decimal_string (PyObject * aa )
1659+ {
1660+ PyLongObject * scratch , * a ;
1661+ PyObject * str ;
1662+ Py_ssize_t size , strlen , size_a , i , j ;
1663+ digit * pout , * pin , rem , tenpow ;
1664+ Py_UNICODE * p ;
1665+ int negative ;
1666+
1667+ a = (PyLongObject * )aa ;
1668+ if (a == NULL || !PyLong_Check (a )) {
1669+ PyErr_BadInternalCall ();
1670+ return NULL ;
1671+ }
1672+ size_a = ABS (Py_SIZE (a ));
1673+ negative = Py_SIZE (a ) < 0 ;
1674+
1675+ /* quick and dirty upper bound for the number of digits
1676+ required to express a in base _PyLong_DECIMAL_BASE:
1677+
1678+ #digits = 1 + floor(log2(a) / log2(_PyLong_DECIMAL_BASE))
1679+
1680+ But log2(a) < size_a * PyLong_SHIFT, and
1681+ log2(_PyLong_DECIMAL_BASE) = log2(10) * _PyLong_DECIMAL_SHIFT
1682+ > 3 * _PyLong_DECIMAL_SHIFT
1683+ */
1684+ if (size_a > PY_SSIZE_T_MAX / PyLong_SHIFT ) {
1685+ PyErr_SetString (PyExc_OverflowError ,
1686+ "long is too large to format" );
1687+ return NULL ;
1688+ }
1689+ /* the expression size_a * PyLong_SHIFT is now safe from overflow */
1690+ size = 1 + size_a * PyLong_SHIFT / (3 * _PyLong_DECIMAL_SHIFT );
1691+ scratch = _PyLong_New (size );
1692+ if (scratch == NULL )
1693+ return NULL ;
1694+
1695+ /* convert array of base _PyLong_BASE digits in pin to an array of
1696+ base _PyLong_DECIMAL_BASE digits in pout, following Knuth (TAOCP,
1697+ Volume 2 (3rd edn), section 4.4, Method 1b). */
1698+ pin = a -> ob_digit ;
1699+ pout = scratch -> ob_digit ;
1700+ size = 0 ;
1701+ for (i = size_a ; -- i >= 0 ; ) {
1702+ digit hi = pin [i ];
1703+ for (j = 0 ; j < size ; j ++ ) {
1704+ twodigits z = (twodigits )pout [j ] << PyLong_SHIFT | hi ;
1705+ hi = z / _PyLong_DECIMAL_BASE ;
1706+ pout [j ] = z - (twodigits )hi * _PyLong_DECIMAL_BASE ;
1707+ }
1708+ while (hi ) {
1709+ pout [size ++ ] = hi % _PyLong_DECIMAL_BASE ;
1710+ hi /= _PyLong_DECIMAL_BASE ;
1711+ }
1712+ /* check for keyboard interrupt */
1713+ SIGCHECK ({
1714+ Py_DECREF (scratch );
1715+ return NULL ;
1716+ })
1717+ }
1718+ /* pout should have at least one digit, so that the case when a = 0
1719+ works correctly */
1720+ if (size == 0 )
1721+ pout [size ++ ] = 0 ;
1722+
1723+ /* calculate exact length of output string, and allocate */
1724+ strlen = negative + 1 + (size - 1 ) * _PyLong_DECIMAL_SHIFT ;
1725+ tenpow = 10 ;
1726+ rem = pout [size - 1 ];
1727+ while (rem >= tenpow ) {
1728+ tenpow *= 10 ;
1729+ strlen ++ ;
1730+ }
1731+ str = PyUnicode_FromUnicode (NULL , strlen );
1732+ if (str == NULL ) {
1733+ Py_DECREF (scratch );
1734+ return NULL ;
1735+ }
1736+
1737+ /* fill the string right-to-left */
1738+ p = PyUnicode_AS_UNICODE (str ) + strlen ;
1739+ * p = '\0' ;
1740+ /* pout[0] through pout[size-2] contribute exactly
1741+ _PyLong_DECIMAL_SHIFT digits each */
1742+ for (i = 0 ; i < size - 1 ; i ++ ) {
1743+ rem = pout [i ];
1744+ for (j = 0 ; j < _PyLong_DECIMAL_SHIFT ; j ++ ) {
1745+ * -- p = '0' + rem % 10 ;
1746+ rem /= 10 ;
1747+ }
1748+ }
1749+ /* pout[size-1]: always produce at least one decimal digit */
1750+ rem = pout [i ];
1751+ do {
1752+ * -- p = '0' + rem % 10 ;
1753+ rem /= 10 ;
1754+ } while (rem != 0 );
1755+
1756+ /* and sign */
1757+ if (negative )
1758+ * -- p = '-' ;
1759+
1760+ /* check we've counted correctly */
1761+ assert (p == PyUnicode_AS_UNICODE (str ));
1762+ Py_DECREF (scratch );
1763+ return (PyObject * )str ;
1764+ }
1765+
16531766/* Convert a long int object to a string, using a given conversion base.
16541767 Return a string object.
16551768 If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'. */
@@ -1665,6 +1778,9 @@ _PyLong_Format(PyObject *aa, int base)
16651778 int bits ;
16661779 char sign = '\0' ;
16671780
1781+ if (base == 10 )
1782+ return long_to_decimal_string ((PyObject * )a );
1783+
16681784 if (a == NULL || !PyLong_Check (a )) {
16691785 PyErr_BadInternalCall ();
16701786 return NULL ;
0 commit comments