]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Python/Python-2.7.10/Python/pystrtod.c
edk2: Remove AppPkg, StdLib, StdLibPrivateInternalFiles
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / Python / pystrtod.c
diff --git a/AppPkg/Applications/Python/Python-2.7.10/Python/pystrtod.c b/AppPkg/Applications/Python/Python-2.7.10/Python/pystrtod.c
deleted file mode 100644 (file)
index 3d925a1..0000000
+++ /dev/null
@@ -1,1249 +0,0 @@
-/* -*- Mode: C; c-file-style: "python" -*- */\r
-\r
-#include <Python.h>\r
-#include <locale.h>\r
-\r
-/* Case-insensitive string match used for nan and inf detection; t should be\r
-   lower-case.  Returns 1 for a successful match, 0 otherwise. */\r
-\r
-static int\r
-case_insensitive_match(const char *s, const char *t)\r
-{\r
-    while(*t && Py_TOLOWER(*s) == *t) {\r
-        s++;\r
-        t++;\r
-    }\r
-    return *t ? 0 : 1;\r
-}\r
-\r
-/* _Py_parse_inf_or_nan: Attempt to parse a string of the form "nan", "inf" or\r
-   "infinity", with an optional leading sign of "+" or "-".  On success,\r
-   return the NaN or Infinity as a double and set *endptr to point just beyond\r
-   the successfully parsed portion of the string.  On failure, return -1.0 and\r
-   set *endptr to point to the start of the string. */\r
-\r
-double\r
-_Py_parse_inf_or_nan(const char *p, char **endptr)\r
-{\r
-    double retval;\r
-    const char *s;\r
-    int negate = 0;\r
-\r
-    s = p;\r
-    if (*s == '-') {\r
-        negate = 1;\r
-        s++;\r
-    }\r
-    else if (*s == '+') {\r
-        s++;\r
-    }\r
-    if (case_insensitive_match(s, "inf")) {\r
-        s += 3;\r
-        if (case_insensitive_match(s, "inity"))\r
-            s += 5;\r
-        retval = negate ? -Py_HUGE_VAL : Py_HUGE_VAL;\r
-    }\r
-#ifdef Py_NAN\r
-    else if (case_insensitive_match(s, "nan")) {\r
-        s += 3;\r
-        retval = negate ? -Py_NAN : Py_NAN;\r
-    }\r
-#endif\r
-    else {\r
-        s = p;\r
-        retval = -1.0;\r
-    }\r
-    *endptr = (char *)s;\r
-    return retval;\r
-}\r
-\r
-/**\r
- * PyOS_ascii_strtod:\r
- * @nptr:    the string to convert to a numeric value.\r
- * @endptr:  if non-%NULL, it returns the character after\r
- *           the last character used in the conversion.\r
- *\r
- * Converts a string to a #gdouble value.\r
- * This function behaves like the standard strtod() function\r
- * does in the C locale. It does this without actually\r
- * changing the current locale, since that would not be\r
- * thread-safe.\r
- *\r
- * This function is typically used when reading configuration\r
- * files or other non-user input that should be locale independent.\r
- * To handle input from the user you should normally use the\r
- * locale-sensitive system strtod() function.\r
- *\r
- * If the correct value would cause overflow, plus or minus %HUGE_VAL\r
- * is returned (according to the sign of the value), and %ERANGE is\r
- * stored in %errno. If the correct value would cause underflow,\r
- * zero is returned and %ERANGE is stored in %errno.\r
- * If memory allocation fails, %ENOMEM is stored in %errno.\r
- *\r
- * This function resets %errno before calling strtod() so that\r
- * you can reliably detect overflow and underflow.\r
- *\r
- * Return value: the #gdouble value.\r
- **/\r
-\r
-#ifndef PY_NO_SHORT_FLOAT_REPR\r
-\r
-double\r
-_PyOS_ascii_strtod(const char *nptr, char **endptr)\r
-{\r
-    double result;\r
-    _Py_SET_53BIT_PRECISION_HEADER;\r
-\r
-    assert(nptr != NULL);\r
-    /* Set errno to zero, so that we can distinguish zero results\r
-       and underflows */\r
-    errno = 0;\r
-\r
-    _Py_SET_53BIT_PRECISION_START;\r
-    result = _Py_dg_strtod(nptr, endptr);\r
-    _Py_SET_53BIT_PRECISION_END;\r
-\r
-    if (*endptr == nptr)\r
-        /* string might represent an inf or nan */\r
-        result = _Py_parse_inf_or_nan(nptr, endptr);\r
-\r
-    return result;\r
-\r
-}\r
-\r
-#else\r
-\r
-/*\r
-   Use system strtod;  since strtod is locale aware, we may\r
-   have to first fix the decimal separator.\r
-\r
-   Note that unlike _Py_dg_strtod, the system strtod may not always give\r
-   correctly rounded results.\r
-*/\r
-\r
-double\r
-_PyOS_ascii_strtod(const char *nptr, char **endptr)\r
-{\r
-    char *fail_pos;\r
-    double val = -1.0;\r
-    struct lconv *locale_data;\r
-    const char *decimal_point;\r
-    size_t decimal_point_len;\r
-    const char *p, *decimal_point_pos;\r
-    const char *end = NULL; /* Silence gcc */\r
-    const char *digits_pos = NULL;\r
-    int negate = 0;\r
-\r
-    assert(nptr != NULL);\r
-\r
-    fail_pos = NULL;\r
-\r
-    locale_data = localeconv();\r
-    decimal_point = locale_data->decimal_point;\r
-    decimal_point_len = strlen(decimal_point);\r
-\r
-    assert(decimal_point_len != 0);\r
-\r
-    decimal_point_pos = NULL;\r
-\r
-    /* Parse infinities and nans */\r
-    val = _Py_parse_inf_or_nan(nptr, endptr);\r
-    if (*endptr != nptr)\r
-        return val;\r
-\r
-    /* Set errno to zero, so that we can distinguish zero results\r
-       and underflows */\r
-    errno = 0;\r
-\r
-    /* We process the optional sign manually, then pass the remainder to\r
-       the system strtod.  This ensures that the result of an underflow\r
-       has the correct sign. (bug #1725)  */\r
-    p = nptr;\r
-    /* Process leading sign, if present */\r
-    if (*p == '-') {\r
-        negate = 1;\r
-        p++;\r
-    }\r
-    else if (*p == '+') {\r
-        p++;\r
-    }\r
-\r
-    /* Some platform strtods accept hex floats; Python shouldn't (at the\r
-       moment), so we check explicitly for strings starting with '0x'. */\r
-    if (*p == '0' && (*(p+1) == 'x' || *(p+1) == 'X'))\r
-        goto invalid_string;\r
-\r
-    /* Check that what's left begins with a digit or decimal point */\r
-    if (!Py_ISDIGIT(*p) && *p != '.')\r
-        goto invalid_string;\r
-\r
-    digits_pos = p;\r
-    if (decimal_point[0] != '.' ||\r
-        decimal_point[1] != 0)\r
-    {\r
-        /* Look for a '.' in the input; if present, it'll need to be\r
-           swapped for the current locale's decimal point before we\r
-           call strtod.  On the other hand, if we find the current\r
-           locale's decimal point then the input is invalid. */\r
-        while (Py_ISDIGIT(*p))\r
-            p++;\r
-\r
-        if (*p == '.')\r
-        {\r
-            decimal_point_pos = p++;\r
-\r
-            /* locate end of number */\r
-            while (Py_ISDIGIT(*p))\r
-                p++;\r
-\r
-            if (*p == 'e' || *p == 'E')\r
-                p++;\r
-            if (*p == '+' || *p == '-')\r
-                p++;\r
-            while (Py_ISDIGIT(*p))\r
-                p++;\r
-            end = p;\r
-        }\r
-        else if (strncmp(p, decimal_point, decimal_point_len) == 0)\r
-            /* Python bug #1417699 */\r
-            goto invalid_string;\r
-        /* For the other cases, we need not convert the decimal\r
-           point */\r
-    }\r
-\r
-    if (decimal_point_pos) {\r
-        char *copy, *c;\r
-        /* Create a copy of the input, with the '.' converted to the\r
-           locale-specific decimal point */\r
-        copy = (char *)PyMem_MALLOC(end - digits_pos +\r
-                                    1 + decimal_point_len);\r
-        if (copy == NULL) {\r
-            *endptr = (char *)nptr;\r
-            errno = ENOMEM;\r
-            return val;\r
-        }\r
-\r
-        c = copy;\r
-        memcpy(c, digits_pos, decimal_point_pos - digits_pos);\r
-        c += decimal_point_pos - digits_pos;\r
-        memcpy(c, decimal_point, decimal_point_len);\r
-        c += decimal_point_len;\r
-        memcpy(c, decimal_point_pos + 1,\r
-               end - (decimal_point_pos + 1));\r
-        c += end - (decimal_point_pos + 1);\r
-        *c = 0;\r
-\r
-        val = strtod(copy, &fail_pos);\r
-\r
-        if (fail_pos)\r
-        {\r
-            if (fail_pos > decimal_point_pos)\r
-                fail_pos = (char *)digits_pos +\r
-                    (fail_pos - copy) -\r
-                    (decimal_point_len - 1);\r
-            else\r
-                fail_pos = (char *)digits_pos +\r
-                    (fail_pos - copy);\r
-        }\r
-\r
-        PyMem_FREE(copy);\r
-\r
-    }\r
-    else {\r
-        val = strtod(digits_pos, &fail_pos);\r
-    }\r
-\r
-    if (fail_pos == digits_pos)\r
-        goto invalid_string;\r
-\r
-    if (negate && fail_pos != nptr)\r
-        val = -val;\r
-    *endptr = fail_pos;\r
-\r
-    return val;\r
-\r
-  invalid_string:\r
-    *endptr = (char*)nptr;\r
-    errno = EINVAL;\r
-    return -1.0;\r
-}\r
-\r
-#endif\r
-\r
-/* PyOS_ascii_strtod is DEPRECATED in Python 2.7 and 3.1 */\r
-\r
-double\r
-PyOS_ascii_strtod(const char *nptr, char **endptr)\r
-{\r
-    char *fail_pos;\r
-    const char *p;\r
-    double x;\r
-\r
-    if (PyErr_WarnEx(PyExc_DeprecationWarning,\r
-                     "PyOS_ascii_strtod and PyOS_ascii_atof are "\r
-                     "deprecated.  Use PyOS_string_to_double "\r
-                     "instead.", 1) < 0)\r
-        return -1.0;\r
-\r
-    /* _PyOS_ascii_strtod already does everything that we want,\r
-       except that it doesn't parse leading whitespace */\r
-    p = nptr;\r
-    while (Py_ISSPACE(*p))\r
-        p++;\r
-    x = _PyOS_ascii_strtod(p, &fail_pos);\r
-    if (fail_pos == p)\r
-        fail_pos = (char *)nptr;\r
-    if (endptr)\r
-        *endptr = (char *)fail_pos;\r
-    return x;\r
-}\r
-\r
-/* PyOS_ascii_strtod is DEPRECATED in Python 2.7 and 3.1 */\r
-\r
-double\r
-PyOS_ascii_atof(const char *nptr)\r
-{\r
-    return PyOS_ascii_strtod(nptr, NULL);\r
-}\r
-\r
-/* PyOS_string_to_double is the recommended replacement for the deprecated\r
-   PyOS_ascii_strtod and PyOS_ascii_atof functions.  It converts a\r
-   null-terminated byte string s (interpreted as a string of ASCII characters)\r
-   to a float.  The string should not have leading or trailing whitespace (in\r
-   contrast, PyOS_ascii_strtod allows leading whitespace but not trailing\r
-   whitespace).  The conversion is independent of the current locale.\r
-\r
-   If endptr is NULL, try to convert the whole string.  Raise ValueError and\r
-   return -1.0 if the string is not a valid representation of a floating-point\r
-   number.\r
-\r
-   If endptr is non-NULL, try to convert as much of the string as possible.\r
-   If no initial segment of the string is the valid representation of a\r
-   floating-point number then *endptr is set to point to the beginning of the\r
-   string, -1.0 is returned and again ValueError is raised.\r
-\r
-   On overflow (e.g., when trying to convert '1e500' on an IEEE 754 machine),\r
-   if overflow_exception is NULL then +-Py_HUGE_VAL is returned, and no Python\r
-   exception is raised.  Otherwise, overflow_exception should point to\r
-   a Python exception, this exception will be raised, -1.0 will be returned,\r
-   and *endptr will point just past the end of the converted value.\r
-\r
-   If any other failure occurs (for example lack of memory), -1.0 is returned\r
-   and the appropriate Python exception will have been set.\r
-*/\r
-\r
-double\r
-PyOS_string_to_double(const char *s,\r
-                      char **endptr,\r
-                      PyObject *overflow_exception)\r
-{\r
-    double x, result=-1.0;\r
-    char *fail_pos;\r
-\r
-    errno = 0;\r
-    PyFPE_START_PROTECT("PyOS_string_to_double", return -1.0)\r
-    x = _PyOS_ascii_strtod(s, &fail_pos);\r
-    PyFPE_END_PROTECT(x)\r
-\r
-    if (errno == ENOMEM) {\r
-        PyErr_NoMemory();\r
-        fail_pos = (char *)s;\r
-    }\r
-    else if (!endptr && (fail_pos == s || *fail_pos != '\0'))\r
-        PyErr_Format(PyExc_ValueError,\r
-                      "could not convert string to float: "\r
-                      "%.200s", s);\r
-    else if (fail_pos == s)\r
-        PyErr_Format(PyExc_ValueError,\r
-                      "could not convert string to float: "\r
-                      "%.200s", s);\r
-    else if (errno == ERANGE && fabs(x) >= 1.0 && overflow_exception)\r
-        PyErr_Format(overflow_exception,\r
-                      "value too large to convert to float: "\r
-                      "%.200s", s);\r
-    else\r
-        result = x;\r
-\r
-    if (endptr != NULL)\r
-        *endptr = fail_pos;\r
-    return result;\r
-}\r
-\r
-/* Given a string that may have a decimal point in the current\r
-   locale, change it back to a dot.  Since the string cannot get\r
-   longer, no need for a maximum buffer size parameter. */\r
-Py_LOCAL_INLINE(void)\r
-change_decimal_from_locale_to_dot(char* buffer)\r
-{\r
-    struct lconv *locale_data = localeconv();\r
-    const char *decimal_point = locale_data->decimal_point;\r
-\r
-    if (decimal_point[0] != '.' || decimal_point[1] != 0) {\r
-        size_t decimal_point_len = strlen(decimal_point);\r
-\r
-        if (*buffer == '+' || *buffer == '-')\r
-            buffer++;\r
-        while (Py_ISDIGIT(*buffer))\r
-            buffer++;\r
-        if (strncmp(buffer, decimal_point, decimal_point_len) == 0) {\r
-            *buffer = '.';\r
-            buffer++;\r
-            if (decimal_point_len > 1) {\r
-                /* buffer needs to get smaller */\r
-                size_t rest_len = strlen(buffer +\r
-                                     (decimal_point_len - 1));\r
-                memmove(buffer,\r
-                    buffer + (decimal_point_len - 1),\r
-                    rest_len);\r
-                buffer[rest_len] = 0;\r
-            }\r
-        }\r
-    }\r
-}\r
-\r
-\r
-/* From the C99 standard, section 7.19.6:\r
-The exponent always contains at least two digits, and only as many more digits\r
-as necessary to represent the exponent.\r
-*/\r
-#define MIN_EXPONENT_DIGITS 2\r
-\r
-/* Ensure that any exponent, if present, is at least MIN_EXPONENT_DIGITS\r
-   in length. */\r
-Py_LOCAL_INLINE(void)\r
-ensure_minimum_exponent_length(char* buffer, size_t buf_size)\r
-{\r
-    char *p = strpbrk(buffer, "eE");\r
-    if (p && (*(p + 1) == '-' || *(p + 1) == '+')) {\r
-        char *start = p + 2;\r
-        int exponent_digit_cnt = 0;\r
-        int leading_zero_cnt = 0;\r
-        int in_leading_zeros = 1;\r
-        int significant_digit_cnt;\r
-\r
-        /* Skip over the exponent and the sign. */\r
-        p += 2;\r
-\r
-        /* Find the end of the exponent, keeping track of leading\r
-           zeros. */\r
-        while (*p && Py_ISDIGIT(*p)) {\r
-            if (in_leading_zeros && *p == '0')\r
-                ++leading_zero_cnt;\r
-            if (*p != '0')\r
-                in_leading_zeros = 0;\r
-            ++p;\r
-            ++exponent_digit_cnt;\r
-        }\r
-\r
-        significant_digit_cnt = exponent_digit_cnt - leading_zero_cnt;\r
-        if (exponent_digit_cnt == MIN_EXPONENT_DIGITS) {\r
-            /* If there are 2 exactly digits, we're done,\r
-               regardless of what they contain */\r
-        }\r
-        else if (exponent_digit_cnt > MIN_EXPONENT_DIGITS) {\r
-            int extra_zeros_cnt;\r
-\r
-            /* There are more than 2 digits in the exponent.  See\r
-               if we can delete some of the leading zeros */\r
-            if (significant_digit_cnt < MIN_EXPONENT_DIGITS)\r
-                significant_digit_cnt = MIN_EXPONENT_DIGITS;\r
-            extra_zeros_cnt = exponent_digit_cnt -\r
-                significant_digit_cnt;\r
-\r
-            /* Delete extra_zeros_cnt worth of characters from the\r
-               front of the exponent */\r
-            assert(extra_zeros_cnt >= 0);\r
-\r
-            /* Add one to significant_digit_cnt to copy the\r
-               trailing 0 byte, thus setting the length */\r
-            memmove(start,\r
-                start + extra_zeros_cnt,\r
-                significant_digit_cnt + 1);\r
-        }\r
-        else {\r
-            /* If there are fewer than 2 digits, add zeros\r
-               until there are 2, if there's enough room */\r
-            int zeros = MIN_EXPONENT_DIGITS - exponent_digit_cnt;\r
-            if (start + zeros + exponent_digit_cnt + 1\r
-                  < buffer + buf_size) {\r
-                memmove(start + zeros, start,\r
-                    exponent_digit_cnt + 1);\r
-                memset(start, '0', zeros);\r
-            }\r
-        }\r
-    }\r
-}\r
-\r
-/* Remove trailing zeros after the decimal point from a numeric string; also\r
-   remove the decimal point if all digits following it are zero.  The numeric\r
-   string must end in '\0', and should not have any leading or trailing\r
-   whitespace.  Assumes that the decimal point is '.'. */\r
-Py_LOCAL_INLINE(void)\r
-remove_trailing_zeros(char *buffer)\r
-{\r
-    char *old_fraction_end, *new_fraction_end, *end, *p;\r
-\r
-    p = buffer;\r
-    if (*p == '-' || *p == '+')\r
-        /* Skip leading sign, if present */\r
-        ++p;\r
-    while (Py_ISDIGIT(*p))\r
-        ++p;\r
-\r
-    /* if there's no decimal point there's nothing to do */\r
-    if (*p++ != '.')\r
-        return;\r
-\r
-    /* scan any digits after the point */\r
-    while (Py_ISDIGIT(*p))\r
-        ++p;\r
-    old_fraction_end = p;\r
-\r
-    /* scan up to ending '\0' */\r
-    while (*p != '\0')\r
-        p++;\r
-    /* +1 to make sure that we move the null byte as well */\r
-    end = p+1;\r
-\r
-    /* scan back from fraction_end, looking for removable zeros */\r
-    p = old_fraction_end;\r
-    while (*(p-1) == '0')\r
-        --p;\r
-    /* and remove point if we've got that far */\r
-    if (*(p-1) == '.')\r
-        --p;\r
-    new_fraction_end = p;\r
-\r
-    memmove(new_fraction_end, old_fraction_end, end-old_fraction_end);\r
-}\r
-\r
-/* Ensure that buffer has a decimal point in it.  The decimal point will not\r
-   be in the current locale, it will always be '.'. Don't add a decimal point\r
-   if an exponent is present.  Also, convert to exponential notation where\r
-   adding a '.0' would produce too many significant digits (see issue 5864).\r
-\r
-   Returns a pointer to the fixed buffer, or NULL on failure.\r
-*/\r
-Py_LOCAL_INLINE(char *)\r
-ensure_decimal_point(char* buffer, size_t buf_size, int precision)\r
-{\r
-    int digit_count, insert_count = 0, convert_to_exp = 0;\r
-    char *chars_to_insert, *digits_start;\r
-\r
-    /* search for the first non-digit character */\r
-    char *p = buffer;\r
-    if (*p == '-' || *p == '+')\r
-        /* Skip leading sign, if present.  I think this could only\r
-           ever be '-', but it can't hurt to check for both. */\r
-        ++p;\r
-    digits_start = p;\r
-    while (*p && Py_ISDIGIT(*p))\r
-        ++p;\r
-    digit_count = Py_SAFE_DOWNCAST(p - digits_start, Py_ssize_t, int);\r
-\r
-    if (*p == '.') {\r
-        if (Py_ISDIGIT(*(p+1))) {\r
-            /* Nothing to do, we already have a decimal\r
-               point and a digit after it */\r
-        }\r
-        else {\r
-            /* We have a decimal point, but no following\r
-               digit.  Insert a zero after the decimal. */\r
-            /* can't ever get here via PyOS_double_to_string */\r
-            assert(precision == -1);\r
-            ++p;\r
-            chars_to_insert = "0";\r
-            insert_count = 1;\r
-        }\r
-    }\r
-    else if (!(*p == 'e' || *p == 'E')) {\r
-        /* Don't add ".0" if we have an exponent. */\r
-        if (digit_count == precision) {\r
-            /* issue 5864: don't add a trailing .0 in the case\r
-               where the '%g'-formatted result already has as many\r
-               significant digits as were requested.  Switch to\r
-               exponential notation instead. */\r
-            convert_to_exp = 1;\r
-            /* no exponent, no point, and we shouldn't land here\r
-               for infs and nans, so we must be at the end of the\r
-               string. */\r
-            assert(*p == '\0');\r
-        }\r
-        else {\r
-            assert(precision == -1 || digit_count < precision);\r
-            chars_to_insert = ".0";\r
-            insert_count = 2;\r
-        }\r
-    }\r
-    if (insert_count) {\r
-        size_t buf_len = strlen(buffer);\r
-        if (buf_len + insert_count + 1 >= buf_size) {\r
-            /* If there is not enough room in the buffer\r
-               for the additional text, just skip it.  It's\r
-               not worth generating an error over. */\r
-        }\r
-        else {\r
-            memmove(p + insert_count, p,\r
-                buffer + strlen(buffer) - p + 1);\r
-            memcpy(p, chars_to_insert, insert_count);\r
-        }\r
-    }\r
-    if (convert_to_exp) {\r
-        int written;\r
-        size_t buf_avail;\r
-        p = digits_start;\r
-        /* insert decimal point */\r
-        assert(digit_count >= 1);\r
-        memmove(p+2, p+1, digit_count); /* safe, but overwrites nul */\r
-        p[1] = '.';\r
-        p += digit_count+1;\r
-        assert(p <= buf_size+buffer);\r
-        buf_avail = buf_size+buffer-p;\r
-        if (buf_avail == 0)\r
-            return NULL;\r
-        /* Add exponent.  It's okay to use lower case 'e': we only\r
-           arrive here as a result of using the empty format code or\r
-           repr/str builtins and those never want an upper case 'E' */\r
-        written = PyOS_snprintf(p, buf_avail, "e%+.02d", digit_count-1);\r
-        if (!(0 <= written &&\r
-              written < Py_SAFE_DOWNCAST(buf_avail, size_t, int)))\r
-            /* output truncated, or something else bad happened */\r
-            return NULL;\r
-        remove_trailing_zeros(buffer);\r
-    }\r
-    return buffer;\r
-}\r
-\r
-/* see FORMATBUFLEN in unicodeobject.c */\r
-#define FLOAT_FORMATBUFLEN 120\r
-\r
-/**\r
- * PyOS_ascii_formatd:\r
- * @buffer: A buffer to place the resulting string in\r
- * @buf_size: The length of the buffer.\r
- * @format: The printf()-style format to use for the\r
- *          code to use for converting.\r
- * @d: The #gdouble to convert\r
- *\r
- * Converts a #gdouble to a string, using the '.' as\r
- * decimal point. To format the number you pass in\r
- * a printf()-style format string. Allowed conversion\r
- * specifiers are 'e', 'E', 'f', 'F', 'g', 'G', and 'Z'.\r
- *\r
- * 'Z' is the same as 'g', except it always has a decimal and\r
- *     at least one digit after the decimal.\r
- *\r
- * Return value: The pointer to the buffer with the converted string.\r
- * On failure returns NULL but does not set any Python exception.\r
- **/\r
-char *\r
-_PyOS_ascii_formatd(char       *buffer,\r
-                   size_t      buf_size,\r
-                   const char *format,\r
-                   double      d,\r
-                   int         precision)\r
-{\r
-    char format_char;\r
-    size_t format_len = strlen(format);\r
-\r
-    /* Issue 2264: code 'Z' requires copying the format.  'Z' is 'g', but\r
-       also with at least one character past the decimal. */\r
-    char tmp_format[FLOAT_FORMATBUFLEN];\r
-\r
-    /* The last character in the format string must be the format char */\r
-    format_char = format[format_len - 1];\r
-\r
-    if (format[0] != '%')\r
-        return NULL;\r
-\r
-    /* I'm not sure why this test is here.  It's ensuring that the format\r
-       string after the first character doesn't have a single quote, a\r
-       lowercase l, or a percent. This is the reverse of the commented-out\r
-       test about 10 lines ago. */\r
-    if (strpbrk(format + 1, "'l%"))\r
-        return NULL;\r
-\r
-    /* Also curious about this function is that it accepts format strings\r
-       like "%xg", which are invalid for floats.  In general, the\r
-       interface to this function is not very good, but changing it is\r
-       difficult because it's a public API. */\r
-\r
-    if (!(format_char == 'e' || format_char == 'E' ||\r
-          format_char == 'f' || format_char == 'F' ||\r
-          format_char == 'g' || format_char == 'G' ||\r
-          format_char == 'Z'))\r
-        return NULL;\r
-\r
-    /* Map 'Z' format_char to 'g', by copying the format string and\r
-       replacing the final char with a 'g' */\r
-    if (format_char == 'Z') {\r
-        if (format_len + 1 >= sizeof(tmp_format)) {\r
-            /* The format won't fit in our copy.  Error out.  In\r
-               practice, this will never happen and will be\r
-               detected by returning NULL */\r
-            return NULL;\r
-        }\r
-        strcpy(tmp_format, format);\r
-        tmp_format[format_len - 1] = 'g';\r
-        format = tmp_format;\r
-    }\r
-\r
-\r
-    /* Have PyOS_snprintf do the hard work */\r
-    PyOS_snprintf(buffer, buf_size, format, d);\r
-\r
-    /* Do various fixups on the return string */\r
-\r
-    /* Get the current locale, and find the decimal point string.\r
-       Convert that string back to a dot. */\r
-    change_decimal_from_locale_to_dot(buffer);\r
-\r
-    /* If an exponent exists, ensure that the exponent is at least\r
-       MIN_EXPONENT_DIGITS digits, providing the buffer is large enough\r
-       for the extra zeros.  Also, if there are more than\r
-       MIN_EXPONENT_DIGITS, remove as many zeros as possible until we get\r
-       back to MIN_EXPONENT_DIGITS */\r
-    ensure_minimum_exponent_length(buffer, buf_size);\r
-\r
-    /* If format_char is 'Z', make sure we have at least one character\r
-       after the decimal point (and make sure we have a decimal point);\r
-       also switch to exponential notation in some edge cases where the\r
-       extra character would produce more significant digits that we\r
-       really want. */\r
-    if (format_char == 'Z')\r
-        buffer = ensure_decimal_point(buffer, buf_size, precision);\r
-\r
-    return buffer;\r
-}\r
-\r
-char *\r
-PyOS_ascii_formatd(char       *buffer,\r
-                   size_t      buf_size,\r
-                   const char *format,\r
-                   double      d)\r
-{\r
-    if (PyErr_WarnEx(PyExc_DeprecationWarning,\r
-                     "PyOS_ascii_formatd is deprecated, "\r
-                     "use PyOS_double_to_string instead", 1) < 0)\r
-        return NULL;\r
-\r
-    return _PyOS_ascii_formatd(buffer, buf_size, format, d, -1);\r
-}\r
-\r
-#ifdef PY_NO_SHORT_FLOAT_REPR\r
-\r
-/* The fallback code to use if _Py_dg_dtoa is not available. */\r
-\r
-PyAPI_FUNC(char *) PyOS_double_to_string(double val,\r
-                                         char format_code,\r
-                                         int precision,\r
-                                         int flags,\r
-                                         int *type)\r
-{\r
-    char format[32];\r
-    Py_ssize_t bufsize;\r
-    char *buf;\r
-    int t, exp;\r
-    int upper = 0;\r
-\r
-    /* Validate format_code, and map upper and lower case */\r
-    switch (format_code) {\r
-    case 'e':          /* exponent */\r
-    case 'f':          /* fixed */\r
-    case 'g':          /* general */\r
-        break;\r
-    case 'E':\r
-        upper = 1;\r
-        format_code = 'e';\r
-        break;\r
-    case 'F':\r
-        upper = 1;\r
-        format_code = 'f';\r
-        break;\r
-    case 'G':\r
-        upper = 1;\r
-        format_code = 'g';\r
-        break;\r
-    case 'r':          /* repr format */\r
-        /* Supplied precision is unused, must be 0. */\r
-        if (precision != 0) {\r
-            PyErr_BadInternalCall();\r
-            return NULL;\r
-        }\r
-        /* The repr() precision (17 significant decimal digits) is the\r
-           minimal number that is guaranteed to have enough precision\r
-           so that if the number is read back in the exact same binary\r
-           value is recreated.  This is true for IEEE floating point\r
-           by design, and also happens to work for all other modern\r
-           hardware. */\r
-        precision = 17;\r
-        format_code = 'g';\r
-        break;\r
-    default:\r
-        PyErr_BadInternalCall();\r
-        return NULL;\r
-    }\r
-\r
-    /* Here's a quick-and-dirty calculation to figure out how big a buffer\r
-       we need.  In general, for a finite float we need:\r
-\r
-         1 byte for each digit of the decimal significand, and\r
-\r
-         1 for a possible sign\r
-         1 for a possible decimal point\r
-         2 for a possible [eE][+-]\r
-         1 for each digit of the exponent;  if we allow 19 digits\r
-           total then we're safe up to exponents of 2**63.\r
-         1 for the trailing nul byte\r
-\r
-       This gives a total of 24 + the number of digits in the significand,\r
-       and the number of digits in the significand is:\r
-\r
-         for 'g' format: at most precision, except possibly\r
-           when precision == 0, when it's 1.\r
-         for 'e' format: precision+1\r
-         for 'f' format: precision digits after the point, at least 1\r
-           before.  To figure out how many digits appear before the point\r
-           we have to examine the size of the number.  If fabs(val) < 1.0\r
-           then there will be only one digit before the point.  If\r
-           fabs(val) >= 1.0, then there are at most\r
-\r
-         1+floor(log10(ceiling(fabs(val))))\r
-\r
-           digits before the point (where the 'ceiling' allows for the\r
-           possibility that the rounding rounds the integer part of val\r
-           up).  A safe upper bound for the above quantity is\r
-           1+floor(exp/3), where exp is the unique integer such that 0.5\r
-           <= fabs(val)/2**exp < 1.0.  This exp can be obtained from\r
-           frexp.\r
-\r
-       So we allow room for precision+1 digits for all formats, plus an\r
-       extra floor(exp/3) digits for 'f' format.\r
-\r
-    */\r
-\r
-    if (Py_IS_NAN(val) || Py_IS_INFINITY(val))\r
-        /* 3 for 'inf'/'nan', 1 for sign, 1 for '\0' */\r
-        bufsize = 5;\r
-    else {\r
-        bufsize = 25 + precision;\r
-        if (format_code == 'f' && fabs(val) >= 1.0) {\r
-            frexp(val, &exp);\r
-            bufsize += exp/3;\r
-        }\r
-    }\r
-\r
-    buf = PyMem_Malloc(bufsize);\r
-    if (buf == NULL) {\r
-        PyErr_NoMemory();\r
-        return NULL;\r
-    }\r
-\r
-    /* Handle nan and inf. */\r
-    if (Py_IS_NAN(val)) {\r
-        strcpy(buf, "nan");\r
-        t = Py_DTST_NAN;\r
-    } else if (Py_IS_INFINITY(val)) {\r
-        if (copysign(1., val) == 1.)\r
-            strcpy(buf, "inf");\r
-        else\r
-            strcpy(buf, "-inf");\r
-        t = Py_DTST_INFINITE;\r
-    } else {\r
-        t = Py_DTST_FINITE;\r
-        if (flags & Py_DTSF_ADD_DOT_0)\r
-            format_code = 'Z';\r
-\r
-        PyOS_snprintf(format, sizeof(format), "%%%s.%i%c",\r
-                      (flags & Py_DTSF_ALT ? "#" : ""), precision,\r
-                      format_code);\r
-        _PyOS_ascii_formatd(buf, bufsize, format, val, precision);\r
-    }\r
-\r
-    /* Add sign when requested.  It's convenient (esp. when formatting\r
-     complex numbers) to include a sign even for inf and nan. */\r
-    if (flags & Py_DTSF_SIGN && buf[0] != '-') {\r
-        size_t len = strlen(buf);\r
-        /* the bufsize calculations above should ensure that we've got\r
-           space to add a sign */\r
-        assert((size_t)bufsize >= len+2);\r
-        memmove(buf+1, buf, len+1);\r
-        buf[0] = '+';\r
-    }\r
-    if (upper) {\r
-        /* Convert to upper case. */\r
-        char *p1;\r
-        for (p1 = buf; *p1; p1++)\r
-            *p1 = Py_TOUPPER(*p1);\r
-    }\r
-\r
-    if (type)\r
-        *type = t;\r
-    return buf;\r
-}\r
-\r
-#else\r
-\r
-/* _Py_dg_dtoa is available. */\r
-\r
-/* I'm using a lookup table here so that I don't have to invent a non-locale\r
-   specific way to convert to uppercase */\r
-#define OFS_INF 0\r
-#define OFS_NAN 1\r
-#define OFS_E 2\r
-\r
-/* The lengths of these are known to the code below, so don't change them */\r
-static char *lc_float_strings[] = {\r
-    "inf",\r
-    "nan",\r
-    "e",\r
-};\r
-static char *uc_float_strings[] = {\r
-    "INF",\r
-    "NAN",\r
-    "E",\r
-};\r
-\r
-\r
-/* Convert a double d to a string, and return a PyMem_Malloc'd block of\r
-   memory contain the resulting string.\r
-\r
-   Arguments:\r
-     d is the double to be converted\r
-     format_code is one of 'e', 'f', 'g', 'r'.  'e', 'f' and 'g'\r
-       correspond to '%e', '%f' and '%g';  'r' corresponds to repr.\r
-     mode is one of '0', '2' or '3', and is completely determined by\r
-       format_code: 'e' and 'g' use mode 2; 'f' mode 3, 'r' mode 0.\r
-     precision is the desired precision\r
-     always_add_sign is nonzero if a '+' sign should be included for positive\r
-       numbers\r
-     add_dot_0_if_integer is nonzero if integers in non-exponential form\r
-       should have ".0" added.  Only applies to format codes 'r' and 'g'.\r
-     use_alt_formatting is nonzero if alternative formatting should be\r
-       used.  Only applies to format codes 'e', 'f' and 'g'.  For code 'g',\r
-       at most one of use_alt_formatting and add_dot_0_if_integer should\r
-       be nonzero.\r
-     type, if non-NULL, will be set to one of these constants to identify\r
-       the type of the 'd' argument:\r
-     Py_DTST_FINITE\r
-     Py_DTST_INFINITE\r
-     Py_DTST_NAN\r
-\r
-   Returns a PyMem_Malloc'd block of memory containing the resulting string,\r
-    or NULL on error. If NULL is returned, the Python error has been set.\r
- */\r
-\r
-static char *\r
-format_float_short(double d, char format_code,\r
-                   int mode, Py_ssize_t precision,\r
-                   int always_add_sign, int add_dot_0_if_integer,\r
-                   int use_alt_formatting, char **float_strings, int *type)\r
-{\r
-    char *buf = NULL;\r
-    char *p = NULL;\r
-    Py_ssize_t bufsize = 0;\r
-    char *digits, *digits_end;\r
-    int decpt_as_int, sign, exp_len, exp = 0, use_exp = 0;\r
-    Py_ssize_t decpt, digits_len, vdigits_start, vdigits_end;\r
-    _Py_SET_53BIT_PRECISION_HEADER;\r
-\r
-    /* _Py_dg_dtoa returns a digit string (no decimal point or exponent).\r
-       Must be matched by a call to _Py_dg_freedtoa. */\r
-    _Py_SET_53BIT_PRECISION_START;\r
-    digits = _Py_dg_dtoa(d, mode, precision, &decpt_as_int, &sign,\r
-                         &digits_end);\r
-    _Py_SET_53BIT_PRECISION_END;\r
-\r
-    decpt = (Py_ssize_t)decpt_as_int;\r
-    if (digits == NULL) {\r
-        /* The only failure mode is no memory. */\r
-        PyErr_NoMemory();\r
-        goto exit;\r
-    }\r
-    assert(digits_end != NULL && digits_end >= digits);\r
-    digits_len = digits_end - digits;\r
-\r
-    if (digits_len && !Py_ISDIGIT(digits[0])) {\r
-        /* Infinities and nans here; adapt Gay's output,\r
-           so convert Infinity to inf and NaN to nan, and\r
-           ignore sign of nan. Then return. */\r
-\r
-        /* ignore the actual sign of a nan */\r
-        if (digits[0] == 'n' || digits[0] == 'N')\r
-            sign = 0;\r
-\r
-        /* We only need 5 bytes to hold the result "+inf\0" . */\r
-        bufsize = 5; /* Used later in an assert. */\r
-        buf = (char *)PyMem_Malloc(bufsize);\r
-        if (buf == NULL) {\r
-            PyErr_NoMemory();\r
-            goto exit;\r
-        }\r
-        p = buf;\r
-\r
-        if (sign == 1) {\r
-            *p++ = '-';\r
-        }\r
-        else if (always_add_sign) {\r
-            *p++ = '+';\r
-        }\r
-        if (digits[0] == 'i' || digits[0] == 'I') {\r
-            strncpy(p, float_strings[OFS_INF], 3);\r
-            p += 3;\r
-\r
-            if (type)\r
-                *type = Py_DTST_INFINITE;\r
-        }\r
-        else if (digits[0] == 'n' || digits[0] == 'N') {\r
-            strncpy(p, float_strings[OFS_NAN], 3);\r
-            p += 3;\r
-\r
-            if (type)\r
-                *type = Py_DTST_NAN;\r
-        }\r
-        else {\r
-            /* shouldn't get here: Gay's code should always return\r
-               something starting with a digit, an 'I',  or 'N' */\r
-            strncpy(p, "ERR", 3);\r
-            p += 3;\r
-            assert(0);\r
-        }\r
-        goto exit;\r
-    }\r
-\r
-    /* The result must be finite (not inf or nan). */\r
-    if (type)\r
-        *type = Py_DTST_FINITE;\r
-\r
-\r
-    /* We got digits back, format them.  We may need to pad 'digits'\r
-       either on the left or right (or both) with extra zeros, so in\r
-       general the resulting string has the form\r
-\r
-         [<sign>]<zeros><digits><zeros>[<exponent>]\r
-\r
-       where either of the <zeros> pieces could be empty, and there's a\r
-       decimal point that could appear either in <digits> or in the\r
-       leading or trailing <zeros>.\r
-\r
-       Imagine an infinite 'virtual' string vdigits, consisting of the\r
-       string 'digits' (starting at index 0) padded on both the left and\r
-       right with infinite strings of zeros.  We want to output a slice\r
-\r
-         vdigits[vdigits_start : vdigits_end]\r
-\r
-       of this virtual string.  Thus if vdigits_start < 0 then we'll end\r
-       up producing some leading zeros; if vdigits_end > digits_len there\r
-       will be trailing zeros in the output.  The next section of code\r
-       determines whether to use an exponent or not, figures out the\r
-       position 'decpt' of the decimal point, and computes 'vdigits_start'\r
-       and 'vdigits_end'. */\r
-    vdigits_end = digits_len;\r
-    switch (format_code) {\r
-    case 'e':\r
-        use_exp = 1;\r
-        vdigits_end = precision;\r
-        break;\r
-    case 'f':\r
-        vdigits_end = decpt + precision;\r
-        break;\r
-    case 'g':\r
-        if (decpt <= -4 || decpt >\r
-            (add_dot_0_if_integer ? precision-1 : precision))\r
-            use_exp = 1;\r
-        if (use_alt_formatting)\r
-            vdigits_end = precision;\r
-        break;\r
-    case 'r':\r
-        /* convert to exponential format at 1e16.  We used to convert\r
-           at 1e17, but that gives odd-looking results for some values\r
-           when a 16-digit 'shortest' repr is padded with bogus zeros.\r
-           For example, repr(2e16+8) would give 20000000000000010.0;\r
-           the true value is 20000000000000008.0. */\r
-        if (decpt <= -4 || decpt > 16)\r
-            use_exp = 1;\r
-        break;\r
-    default:\r
-        PyErr_BadInternalCall();\r
-        goto exit;\r
-    }\r
-\r
-    /* if using an exponent, reset decimal point position to 1 and adjust\r
-       exponent accordingly.*/\r
-    if (use_exp) {\r
-        exp = decpt - 1;\r
-        decpt = 1;\r
-    }\r
-    /* ensure vdigits_start < decpt <= vdigits_end, or vdigits_start <\r
-       decpt < vdigits_end if add_dot_0_if_integer and no exponent */\r
-    vdigits_start = decpt <= 0 ? decpt-1 : 0;\r
-    if (!use_exp && add_dot_0_if_integer)\r
-        vdigits_end = vdigits_end > decpt ? vdigits_end : decpt + 1;\r
-    else\r
-        vdigits_end = vdigits_end > decpt ? vdigits_end : decpt;\r
-\r
-    /* double check inequalities */\r
-    assert(vdigits_start <= 0 &&\r
-           0 <= digits_len &&\r
-           digits_len <= vdigits_end);\r
-    /* decimal point should be in (vdigits_start, vdigits_end] */\r
-    assert(vdigits_start < decpt && decpt <= vdigits_end);\r
-\r
-    /* Compute an upper bound how much memory we need. This might be a few\r
-       chars too long, but no big deal. */\r
-    bufsize =\r
-        /* sign, decimal point and trailing 0 byte */\r
-        3 +\r
-\r
-        /* total digit count (including zero padding on both sides) */\r
-        (vdigits_end - vdigits_start) +\r
-\r
-        /* exponent "e+100", max 3 numerical digits */\r
-        (use_exp ? 5 : 0);\r
-\r
-    /* Now allocate the memory and initialize p to point to the start of\r
-       it. */\r
-    buf = (char *)PyMem_Malloc(bufsize);\r
-    if (buf == NULL) {\r
-        PyErr_NoMemory();\r
-        goto exit;\r
-    }\r
-    p = buf;\r
-\r
-    /* Add a negative sign if negative, and a plus sign if non-negative\r
-       and always_add_sign is true. */\r
-    if (sign == 1)\r
-        *p++ = '-';\r
-    else if (always_add_sign)\r
-        *p++ = '+';\r
-\r
-    /* note that exactly one of the three 'if' conditions is true,\r
-       so we include exactly one decimal point */\r
-    /* Zero padding on left of digit string */\r
-    if (decpt <= 0) {\r
-        memset(p, '0', decpt-vdigits_start);\r
-        p += decpt - vdigits_start;\r
-        *p++ = '.';\r
-        memset(p, '0', 0-decpt);\r
-        p += 0-decpt;\r
-    }\r
-    else {\r
-        memset(p, '0', 0-vdigits_start);\r
-        p += 0 - vdigits_start;\r
-    }\r
-\r
-    /* Digits, with included decimal point */\r
-    if (0 < decpt && decpt <= digits_len) {\r
-        strncpy(p, digits, decpt-0);\r
-        p += decpt-0;\r
-        *p++ = '.';\r
-        strncpy(p, digits+decpt, digits_len-decpt);\r
-        p += digits_len-decpt;\r
-    }\r
-    else {\r
-        strncpy(p, digits, digits_len);\r
-        p += digits_len;\r
-    }\r
-\r
-    /* And zeros on the right */\r
-    if (digits_len < decpt) {\r
-        memset(p, '0', decpt-digits_len);\r
-        p += decpt-digits_len;\r
-        *p++ = '.';\r
-        memset(p, '0', vdigits_end-decpt);\r
-        p += vdigits_end-decpt;\r
-    }\r
-    else {\r
-        memset(p, '0', vdigits_end-digits_len);\r
-        p += vdigits_end-digits_len;\r
-    }\r
-\r
-    /* Delete a trailing decimal pt unless using alternative formatting. */\r
-    if (p[-1] == '.' && !use_alt_formatting)\r
-        p--;\r
-\r
-    /* Now that we've done zero padding, add an exponent if needed. */\r
-    if (use_exp) {\r
-        *p++ = float_strings[OFS_E][0];\r
-        exp_len = sprintf(p, "%+.02d", exp);\r
-        p += exp_len;\r
-    }\r
-  exit:\r
-    if (buf) {\r
-        *p = '\0';\r
-        /* It's too late if this fails, as we've already stepped on\r
-           memory that isn't ours. But it's an okay debugging test. */\r
-        assert(p-buf < bufsize);\r
-    }\r
-    if (digits)\r
-        _Py_dg_freedtoa(digits);\r
-\r
-    return buf;\r
-}\r
-\r
-\r
-PyAPI_FUNC(char *) PyOS_double_to_string(double val,\r
-                                         char format_code,\r
-                                         int precision,\r
-                                         int flags,\r
-                                         int *type)\r
-{\r
-    char **float_strings = lc_float_strings;\r
-    int mode;\r
-\r
-    /* Validate format_code, and map upper and lower case. Compute the\r
-       mode and make any adjustments as needed. */\r
-    switch (format_code) {\r
-    /* exponent */\r
-    case 'E':\r
-        float_strings = uc_float_strings;\r
-        format_code = 'e';\r
-        /* Fall through. */\r
-    case 'e':\r
-        mode = 2;\r
-        precision++;\r
-        break;\r
-\r
-    /* fixed */\r
-    case 'F':\r
-        float_strings = uc_float_strings;\r
-        format_code = 'f';\r
-        /* Fall through. */\r
-    case 'f':\r
-        mode = 3;\r
-        break;\r
-\r
-    /* general */\r
-    case 'G':\r
-        float_strings = uc_float_strings;\r
-        format_code = 'g';\r
-        /* Fall through. */\r
-    case 'g':\r
-        mode = 2;\r
-        /* precision 0 makes no sense for 'g' format; interpret as 1 */\r
-        if (precision == 0)\r
-            precision = 1;\r
-        break;\r
-\r
-    /* repr format */\r
-    case 'r':\r
-        mode = 0;\r
-        /* Supplied precision is unused, must be 0. */\r
-        if (precision != 0) {\r
-            PyErr_BadInternalCall();\r
-            return NULL;\r
-        }\r
-        break;\r
-\r
-    default:\r
-        PyErr_BadInternalCall();\r
-        return NULL;\r
-    }\r
-\r
-    return format_float_short(val, format_code, mode, precision,\r
-                              flags & Py_DTSF_SIGN,\r
-                              flags & Py_DTSF_ADD_DOT_0,\r
-                              flags & Py_DTSF_ALT,\r
-                              float_strings, type);\r
-}\r
-#endif /* ifdef PY_NO_SHORT_FLOAT_REPR */\r