]> granicus.if.org Git - python/commitdiff
Added ',' thousands grouping to int.__format__. See PEP 378.
authorEric Smith <eric@trueblade.com>
Fri, 3 Apr 2009 14:45:06 +0000 (14:45 +0000)
committerEric Smith <eric@trueblade.com>
Fri, 3 Apr 2009 14:45:06 +0000 (14:45 +0000)
This is incomplete, but I want to get some version into the next alpha. I am still working on:
Documentation.
More tests.
Implement for floats.

In addition, there's an existing bug with 'n' formatting that carries forward to thousands grouping (issue 5515).

Include/bytesobject.h
Include/unicodeobject.h
Lib/test/test_types.py
Objects/bytesobject.c
Objects/stringlib/formatter.h
Objects/stringlib/localeutil.h
Objects/stringlib/stringdefs.h
Objects/stringlib/unicodedefs.h
Objects/unicodeobject.c
Python/pystrtod.c

index 3f275a86850fb11fdcbd965ac117d4f8abfbfd08..f5a5085bbd4f7956463ef07c52d3f3c3d9221bcc 100644 (file)
@@ -91,13 +91,25 @@ PyAPI_FUNC(int) PyBytes_AsStringAndSize(
    into the string pointed to by buffer.  For the argument descriptions,
    see Objects/stringlib/localeutil.h */
 
-PyAPI_FUNC(int) _PyBytes_InsertThousandsGrouping(char *buffer,
+PyAPI_FUNC(int) _PyBytes_InsertThousandsGroupingLocale(char *buffer,
                                                  Py_ssize_t n_buffer,
                                                  Py_ssize_t n_digits,
                                                  Py_ssize_t buf_size,
                                                  Py_ssize_t *count,
                                                  int append_zero_char);
 
+/* Using explicit passed-in values, insert the thousands grouping
+   into the string pointed to by buffer.  For the argument descriptions,
+   see Objects/stringlib/localeutil.h */
+PyAPI_FUNC(int) _PyBytes_InsertThousandsGrouping(char *buffer,
+                                                Py_ssize_t n_buffer,
+                                                Py_ssize_t n_digits,
+                                                Py_ssize_t buf_size,
+                                                Py_ssize_t *count,
+                                                 int append_zero_char,
+                                                 const char *grouping,
+                                                 const char *thousands_sep);
+
 /* Flags used by string formatting */
 #define F_LJUST (1<<0)
 #define F_SIGN (1<<1)
index 8259743a5a0fd16dbdcad97ca929c6ecf0dd0479..98c0372061a5e04a59ba200ca47fbd74e93f17ad 100644 (file)
@@ -1482,13 +1482,24 @@ PyAPI_FUNC(PyObject *) _PyUnicode_XStrip(
    into the string pointed to by buffer.  For the argument descriptions,
    see Objects/stringlib/localeutil.h */
 
-PyAPI_FUNC(int) _PyUnicode_InsertThousandsGrouping(Py_UNICODE *buffer,
+PyAPI_FUNC(int) _PyUnicode_InsertThousandsGroupingLocale(Py_UNICODE *buffer,
                                                  Py_ssize_t n_buffer,
                                                  Py_ssize_t n_digits,
                                                  Py_ssize_t buf_size,
                                                  Py_ssize_t *count,
                                                  int append_zero_char);
 
+/* Using explicit passed-in values, insert the thousands grouping
+   into the string pointed to by buffer.  For the argument descriptions,
+   see Objects/stringlib/localeutil.h */
+PyAPI_FUNC(int) _PyUnicode_InsertThousandsGrouping(Py_UNICODE *buffer,
+                                                Py_ssize_t n_buffer,
+                                                Py_ssize_t n_digits,
+                                                Py_ssize_t buf_size,
+                                                Py_ssize_t *count,
+                                                 int append_zero_char,
+                                                 const char *grouping,
+                                                 const char *thousands_sep);
 /* === Characters Type APIs =============================================== */
 
 /* Helper array used by Py_UNICODE_ISSPACE(). */
index c200e07b2a346e485f26a57d53e190ed032c142d..0d4d1b3edc9bdf8e287ae492b03745425f700c9f 100644 (file)
@@ -338,6 +338,15 @@ class TypesTests(unittest.TestCase):
         test(123456, "#012X", '0X000001E240')
         test(-123456, "#012X", '-0X00001E240')
 
+        test(123, ',', '123')
+        test(-123, ',', '-123')
+        test(1234, ',', '1,234')
+        test(-1234, ',', '-1,234')
+        test(123456, ',', '123,456')
+        test(-123456, ',', '-123,456')
+        test(1234567, ',', '1,234,567')
+        test(-1234567, ',', '-1,234,567')
+
         # make sure these are errors
 
         # precision disallowed
@@ -347,6 +356,8 @@ class TypesTests(unittest.TestCase):
         # format spec must be string
         self.assertRaises(TypeError, 3 .__format__, None)
         self.assertRaises(TypeError, 3 .__format__, 0)
+        # can't have ',' with 'n'
+        self.assertRaises(ValueError, 3 .__format__, ",n")
 
         # ensure that only int and float type specifiers work
         for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
index d3b598e0e8b6029265396058b4875a6365165282..d5c2bea35e5f9d931f750baf41d749047fd7bc5e 100644 (file)
@@ -583,6 +583,7 @@ PyBytes_AsStringAndSize(register PyObject *obj,
 #include "stringlib/transmogrify.h"
 
 #define _Py_InsertThousandsGrouping _PyBytes_InsertThousandsGrouping
+#define _Py_InsertThousandsGroupingLocale _PyBytes_InsertThousandsGroupingLocale
 #include "stringlib/localeutil.h"
 
 PyObject *
index c367ec53ee7f38412fede2317b3b6aaf33e660a7..57e54526fb793dd9e8675ca89fe8ee43b4f2313d 100644 (file)
@@ -120,6 +120,7 @@ typedef struct {
     int alternate;
     STRINGLIB_CHAR sign;
     Py_ssize_t width;
+    int thousands_separators;
     Py_ssize_t precision;
     STRINGLIB_CHAR type;
 } InternalFormatSpec;
@@ -149,6 +150,7 @@ parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
     format->alternate = 0;
     format->sign = '\0';
     format->width = -1;
+    format->thousands_separators = 0;
     format->precision = -1;
     format->type = default_type;
 
@@ -201,6 +203,12 @@ parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
         format->width = -1;
     }
 
+    /* Comma signifies add thousands separators */
+    if (end-ptr && ptr[0] == ',') {
+        format->thousands_separators = 1;
+        ++ptr;
+    }
+
     /* Parse field precision */
     if (end-ptr && ptr[0] == '.') {
         ++ptr;
@@ -230,6 +238,11 @@ parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
         ++ptr;
     }
 
+    if (format->type == 'n' && format->thousands_separators) {
+        PyErr_Format(PyExc_ValueError, "Cannot specify ',' with 'n'.");
+        return 0;
+    }
+
     return 1;
 }
 
@@ -630,8 +643,13 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
     if (format->type == 'n')
             /* Compute how many additional chars we need to allocate
                to hold the thousands grouping. */
-            STRINGLIB_GROUPING(NULL, n_digits, n_digits,
+            STRINGLIB_GROUPING_LOCALE(NULL, n_digits, n_digits,
                                0, &n_grouping_chars, 0);
+    if (format->thousands_separators)
+            /* Compute how many additional chars we need to allocate
+               to hold the thousands grouping. */
+            STRINGLIB_GROUPING(NULL, n_digits, n_digits,
+                               0, &n_grouping_chars, 0, "\3", ",");
 
     /* Calculate the widths of the various leading and trailing parts */
     calc_number_widths(&spec, sign, n_prefix, n_digits + n_grouping_chars,
@@ -670,11 +688,22 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
                reserved enough space. */
             STRINGLIB_CHAR *pstart = p + n_leading_chars;
 #ifndef NDEBUG
-            int r =
+            int r;
+#endif
+            if (format->type == 'n')
+#ifndef NDEBUG
+                r = 
 #endif
-                STRINGLIB_GROUPING(pstart, n_digits, n_digits,
+                    STRINGLIB_GROUPING_LOCALE(pstart, n_digits, n_digits,
                            spec.n_total+n_grouping_chars-n_leading_chars,
                            NULL, 0);
+            else
+#ifndef NDEBUG
+                r =
+                    STRINGLIB_GROUPING(pstart, n_digits, n_digits,
+                           spec.n_total+n_grouping_chars-n_leading_chars,
+                           NULL, 0, "\3", ",");
+#endif
             assert(r);
     }
 
index 1105609ff3234407fd902680de670d7faa8e9d2f..9254c09a7f28b7ebbee826496c68075844bb59aa 100644 (file)
  * @append_zero_char: If non-zero, put a trailing zero at the end of
  *         of the resulting string, if and only if we modified the
  *         string.
+ * @grouping: see definition in localeconv().
+ * @thousands_sep: see definition in localeconv().
  *
- * Inserts thousand grouping characters (as defined in the current
- *  locale) into the string between buffer and buffer+n_digits.  If
- *  count is non-NULL, don't do any formatting, just count the number
- *  of characters to insert.  This is used by the caller to
+ * Inserts thousand grouping characters (as defined by grouping and
+ *  thousands_sep) into the string between buffer and buffer+n_digits.
+ *  If count is non-NULL, don't do any formatting, just count the
+ *  number of characters to insert.  This is used by the caller to
  *  appropriately resize the buffer, if needed.  If count is non-NULL,
  *  buffer can be NULL (it is not dereferenced at all in that case).
  *
  **/
 int
 _Py_InsertThousandsGrouping(STRINGLIB_CHAR *buffer,
-                           Py_ssize_t n_buffer,
-                           Py_ssize_t n_digits,
-                           Py_ssize_t buf_size,
-                           Py_ssize_t *count,
-                           int append_zero_char)
+                            Py_ssize_t n_buffer,
+                            Py_ssize_t n_digits,
+                            Py_ssize_t buf_size,
+                            Py_ssize_t *count,
+                            int append_zero_char,
+                            const char *grouping,
+                            const char *thousands_sep)
 {
-       struct lconv *locale_data = localeconv();
-       const char *grouping = locale_data->grouping;
-       const char *thousands_sep = locale_data->thousands_sep;
-       Py_ssize_t thousands_sep_len = strlen(thousands_sep);
-       STRINGLIB_CHAR *pend = NULL; /* current end of buffer */
-       STRINGLIB_CHAR *pmax = NULL; /* max of buffer */
-       char current_grouping;
-       Py_ssize_t remaining = n_digits; /* Number of chars remaining to
-                                           be looked at */
+        Py_ssize_t thousands_sep_len = strlen(thousands_sep);
+        STRINGLIB_CHAR *pend = NULL; /* current end of buffer */
+        STRINGLIB_CHAR *pmax = NULL; /* max of buffer */
+        char current_grouping;
+        Py_ssize_t remaining = n_digits; /* Number of chars remaining to
+                                            be looked at */
 
-       /* Initialize the character count, if we're just counting. */
-       if (count)
-               *count = 0;
-       else {
-               /* We're not just counting, we're modifying buffer */
-               pend = buffer + n_buffer;
-               pmax = buffer + buf_size;
-       }
+        /* Initialize the character count, if we're just counting. */
+        if (count)
+                *count = 0;
+        else {
+                /* We're not just counting, we're modifying buffer */
+                pend = buffer + n_buffer;
+                pmax = buffer + buf_size;
+        }
 
-       /* Starting at the end and working right-to-left, keep track of
-          what grouping needs to be added and insert that. */
-       current_grouping = *grouping++;
+        /* Starting at the end and working right-to-left, keep track of
+           what grouping needs to be added and insert that. */
+        current_grouping = *grouping++;
 
-       /* If the first character is 0, perform no grouping at all. */
-       if (current_grouping == 0)
-               return 1;
+        /* If the first character is 0, perform no grouping at all. */
+        if (current_grouping == 0)
+                return 1;
 
-       while (remaining > current_grouping) {
-               /* Always leave buffer and pend valid at the end of this
-                  loop, since we might leave with a return statement. */
+        while (remaining > current_grouping) {
+                /* Always leave buffer and pend valid at the end of this
+                   loop, since we might leave with a return statement. */
 
-               remaining -= current_grouping;
-               if (count) {
-                       /* We're only counting, not touching the memory. */
-                       *count += thousands_sep_len;
-               }
-               else {
-                       /* Do the formatting. */
+                remaining -= current_grouping;
+                if (count) {
+                        /* We're only counting, not touching the memory. */
+                        *count += thousands_sep_len;
+                }
+                else {
+                        /* Do the formatting. */
 
-                       STRINGLIB_CHAR *plast = buffer + remaining;
+                        STRINGLIB_CHAR *plast = buffer + remaining;
 
-                       /* Is there room to insert thousands_sep_len chars? */
-                       if (pmax - pend < thousands_sep_len)
-                               /* No room. */
-                               return 0;
+                        /* Is there room to insert thousands_sep_len chars? */
+                        if (pmax - pend < thousands_sep_len)
+                                /* No room. */
+                                return 0;
 
-                       /* Move the rest of the string down. */
-                       memmove(plast + thousands_sep_len,
-                               plast,
-                               (pend - plast) * sizeof(STRINGLIB_CHAR));
-                       /* Copy the thousands_sep chars into the buffer. */
+                        /* Move the rest of the string down. */
+                        memmove(plast + thousands_sep_len,
+                                plast,
+                                (pend - plast) * sizeof(STRINGLIB_CHAR));
+                        /* Copy the thousands_sep chars into the buffer. */
 #if STRINGLIB_IS_UNICODE
-                       /* Convert from the char's of the thousands_sep from
-                          the locale into unicode. */
-                       {
-                               Py_ssize_t i;
-                               for (i = 0; i < thousands_sep_len; ++i)
-                                       plast[i] = thousands_sep[i];
-                       }
+                        /* Convert from the char's of the thousands_sep from
+                           the locale into unicode. */
+                        {
+                                Py_ssize_t i;
+                                for (i = 0; i < thousands_sep_len; ++i)
+                                        plast[i] = thousands_sep[i];
+                        }
 #else
-                       /* No conversion, just memcpy the thousands_sep. */
-                       memcpy(plast, thousands_sep, thousands_sep_len);
+                        /* No conversion, just memcpy the thousands_sep. */
+                        memcpy(plast, thousands_sep, thousands_sep_len);
 #endif
-               }
+                }
 
-               /* Adjust end pointer. */
-               pend += thousands_sep_len;
+                /* Adjust end pointer. */
+                pend += thousands_sep_len;
 
-               /* Move to the next grouping character, unless we're
-                  repeating (which is designated by a grouping of 0). */
-               if (*grouping != 0) {
-                       current_grouping = *grouping++;
-                       if (current_grouping == CHAR_MAX)
-                               /* We're done. */
-                               break;
-               }
-       }
-       if (append_zero_char) {
-               /* Append a zero character to mark the end of the string,
-                  if there's room. */
-               if (pend - (buffer + remaining) < 1)
-                       /* No room, error. */
-                       return 0;
-               *pend = 0;
-       }
-       return 1;
+                /* Move to the next grouping character, unless we're
+                   repeating (which is designated by a grouping of 0). */
+                if (*grouping != 0) {
+                        current_grouping = *grouping++;
+                        if (current_grouping == CHAR_MAX)
+                                /* We're done. */
+                                break;
+                }
+        }
+        if (append_zero_char) {
+                /* Append a zero character to mark the end of the string,
+                   if there's room. */
+                if (pend - (buffer + remaining) < 1)
+                        /* No room, error. */
+                        return 0;
+                *pend = 0;
+        }
+        return 1;
+}
+
+/**
+ * _Py_InsertThousandsGroupingLocale:
+ * @buffer: A pointer to the start of a string.
+ * @n_buffer: The length of the string.
+ * @n_digits: The number of digits in the string, in which we want
+ *            to put the grouping chars.
+ * @buf_size: The maximum size of the buffer pointed to by buffer.
+ * @count: If non-NULL, points to a variable that will receive the
+ *         number of characters we need to insert (and no formatting
+ *         will actually occur).
+ * @append_zero_char: If non-zero, put a trailing zero at the end of
+ *         of the resulting string, if and only if we modified the
+ *         string.
+ *
+ * Reads thee current locale and calls _Py_InsertThousandsGrouping().
+ **/
+int
+_Py_InsertThousandsGroupingLocale(STRINGLIB_CHAR *buffer,
+                                  Py_ssize_t n_buffer,
+                                  Py_ssize_t n_digits,
+                                  Py_ssize_t buf_size,
+                                  Py_ssize_t *count,
+                                  int append_zero_char)
+{
+        struct lconv *locale_data = localeconv();
+        const char *grouping = locale_data->grouping;
+        const char *thousands_sep = locale_data->thousands_sep;
+
+        return _Py_InsertThousandsGrouping(buffer, n_buffer, n_digits,
+                                           buf_size, count,
+                                           append_zero_char, grouping,
+                                           thousands_sep);
 }
 #endif /* STRINGLIB_LOCALEUTIL_H */
index 946de050320f88d82ae24330e42dc923696b4318..1659315ea4e618b43b79d02a7f5a10b954b043b2 100644 (file)
@@ -24,5 +24,6 @@
 #define STRINGLIB_CMP            memcmp
 #define STRINGLIB_TOSTR          PyObject_Str
 #define STRINGLIB_GROUPING       _PyBytes_InsertThousandsGrouping
+#define STRINGLIB_GROUPING_LOCALE _PyBytes_InsertThousandsGroupingLocale
 #define STRINGLIB_TOASCII        PyObject_Repr
 #endif /* !STRINGLIB_STRINGDEFS_H */
index d579c1c689e129b12cac2714b81cd7badf476006..366acfe001775da46bd9318004e6660e21aaa921 100644 (file)
@@ -22,6 +22,7 @@
 #define STRINGLIB_RESIZE         PyUnicode_Resize
 #define STRINGLIB_CHECK          PyUnicode_Check
 #define STRINGLIB_GROUPING       _PyUnicode_InsertThousandsGrouping
+#define STRINGLIB_GROUPING_LOCALE _PyUnicode_InsertThousandsGroupingLocale
 
 #if PY_VERSION_HEX < 0x03000000
 #define STRINGLIB_TOSTR          PyObject_Unicode
index 4def53768cc8277e30957bd2a372d3f5f6454a4d..d3e528313770bd509afcfed36759a8f387883918 100644 (file)
@@ -5635,6 +5635,7 @@ int PyUnicode_EncodeDecimal(Py_UNICODE *s,
 #include "stringlib/partition.h"
 
 #define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping
+#define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale
 #include "stringlib/localeutil.h"
 
 /* helper macro to fixup start/end slice values */
index b3738528dbfe7f1c108a892beae449052898e985..b81abcea9943a6420e5ed2497363fce5d770fb25 100644 (file)
@@ -368,7 +368,7 @@ add_thousands_grouping(char* buffer, size_t buf_size)
        /* At this point, p points just past the right-most character we
           want to format.  We need to add the grouping string for the
           characters between buffer and p. */
-       return _PyBytes_InsertThousandsGrouping(buffer, len, p-buffer,
+       return _PyBytes_InsertThousandsGroupingLocale(buffer, len, p-buffer,
                                                buf_size, NULL, 1);
 }