diff Objects/unicodeobject.c @ 36262:76a1c20e830e 2.4

Backport: - Patch #1400181, fix unicode string formatting to not use the locale. This is how string objects work. u'%f' could use , instead of . for the decimal point. Now both strings and unicode always use periods. This is the code that would break: import locale locale.setlocale(locale.LC_NUMERIC, 'de_DE') u'%.1f' % 1.0 assert '1.0' == u'%.1f' % 1.0 I couldn't create a test case which fails, but this fixes the problem. (tested in interpreter and reported fixed by others)
author Neal Norwitz <nnorwitz@gmail.com>
date Tue, 10 Jan 2006 06:05:57 +0000
parents 321efd037859
children accfb156f9cc
line wrap: on
line diff
--- a/Objects/unicodeobject.c	Mon Jan 09 07:10:56 2006 +0000
+++ b/Objects/unicodeobject.c	Tue Jan 10 06:05:57 2006 +0000
@@ -6538,26 +6538,31 @@
 #define F_ALT	(1<<3)
 #define F_ZERO	(1<<4)
 
-static
-int usprintf(register Py_UNICODE *buffer, char *format, ...)
-{
-    register int i;
-    int len;
-    va_list va;
-    char *charbuffer;
-    va_start(va, format);
-
-    /* First, format the string as char array, then expand to Py_UNICODE
-       array. */
-    charbuffer = (char *)buffer;
-    len = vsprintf(charbuffer, format, va);
+static int
+strtounicode(Py_UNICODE *buffer, const char *charbuffer)
+{
+    register long i;
+    long len = strlen(charbuffer);
     for (i = len - 1; i >= 0; i--)
 	buffer[i] = (Py_UNICODE) charbuffer[i];
 
-    va_end(va);
     return len;
 }
 
+static int
+doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x)
+{
+    PyOS_ascii_formatd((char *)buffer, len, format, x);
+    return strtounicode(buffer, (char *)buffer);
+}
+
+static int
+longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x)
+{
+    PyOS_snprintf((char *)buffer, len, format, x);
+    return strtounicode(buffer, (char *)buffer);
+}
+
 /* XXX To save some code duplication, formatfloat/long/int could have been
    shared with stringobject.c, converting from 8-bit to Unicode after the
    formatting is done. */
@@ -6607,7 +6612,7 @@
     PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c",
 		  (flags&F_ALT) ? "#" : "",
 		  prec, type);
-    return usprintf(buf, fmt, x);
+    return doubletounicode(buf, buflen, fmt, x);
 }
 
 static PyObject*
@@ -6699,9 +6704,9 @@
                       prec, type);
     }
     if (sign[0])
-        return usprintf(buf, fmt, -x);
+        return longtounicode(buf, buflen, fmt, -x);
     else
-        return usprintf(buf, fmt, x);
+        return longtounicode(buf, buflen, fmt, x);
 }
 
 static int