summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeal Norwitz <nnorwitz@gmail.com>2006-01-10 06:03:13 (GMT)
committerNeal Norwitz <nnorwitz@gmail.com>2006-01-10 06:03:13 (GMT)
commitfc76d633e8017fd948bc2363738cbdea41586e21 (patch)
tree7015f5d5c57eec77290891b156fa1d2ffa14e06d
parentab92afd100a4406cd296d699a69184b74abe4cf7 (diff)
downloadcpython-fc76d633e8017fd948bc2363738cbdea41586e21.zip
cpython-fc76d633e8017fd948bc2363738cbdea41586e21.tar.gz
cpython-fc76d633e8017fd948bc2363738cbdea41586e21.tar.bz2
- 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. Will backport.
-rw-r--r--Misc/NEWS4
-rw-r--r--Objects/unicodeobject.c37
2 files changed, 25 insertions, 16 deletions
diff --git a/Misc/NEWS b/Misc/NEWS
index ab8dba5..78cf145 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,10 @@ What's New in Python 2.5 alpha 1?
Core and builtins
-----------------
+- 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.
+
- Bug #1244610, #1392915, fix build problem on OpenBSD 3.7 and 3.8.
configure would break checking curses.h.
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 37e292d..037c45c 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -6579,26 +6579,31 @@ getnextarg(PyObject *args, int arglen, int *p_argidx)
#define F_ALT (1<<3)
#define F_ZERO (1<<4)
-static
-int usprintf(register Py_UNICODE *buffer, char *format, ...)
+static int
+strtounicode(Py_UNICODE *buffer, const char *charbuffer)
{
- 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);
+ 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. */
@@ -6648,7 +6653,7 @@ formatfloat(Py_UNICODE *buf,
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*
@@ -6740,9 +6745,9 @@ formatint(Py_UNICODE *buf,
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