Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fc76d63

Browse files
committed
- 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.
1 parent ab92afd commit fc76d63

2 files changed

Lines changed: 25 additions & 16 deletions

File tree

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 2.5 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- Patch #1400181, fix unicode string formatting to not use the locale.
16+
This is how string objects work. u'%f' could use , instead of .
17+
for the decimal point. Now both strings and unicode always use periods.
18+
1519
- Bug #1244610, #1392915, fix build problem on OpenBSD 3.7 and 3.8.
1620
configure would break checking curses.h.
1721

Objects/unicodeobject.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6579,26 +6579,31 @@ getnextarg(PyObject *args, int arglen, int *p_argidx)
65796579
#define F_ALT (1<<3)
65806580
#define F_ZERO (1<<4)
65816581

6582-
static
6583-
int usprintf(register Py_UNICODE *buffer, char *format, ...)
6582+
static int
6583+
strtounicode(Py_UNICODE *buffer, const char *charbuffer)
65846584
{
6585-
register int i;
6586-
int len;
6587-
va_list va;
6588-
char *charbuffer;
6589-
va_start(va, format);
6590-
6591-
/* First, format the string as char array, then expand to Py_UNICODE
6592-
array. */
6593-
charbuffer = (char *)buffer;
6594-
len = vsprintf(charbuffer, format, va);
6585+
register long i;
6586+
long len = strlen(charbuffer);
65956587
for (i = len - 1; i >= 0; i--)
65966588
buffer[i] = (Py_UNICODE) charbuffer[i];
65976589

6598-
va_end(va);
65996590
return len;
66006591
}
66016592

6593+
static int
6594+
doubletounicode(Py_UNICODE *buffer, size_t len, const char *format, double x)
6595+
{
6596+
PyOS_ascii_formatd((char *)buffer, len, format, x);
6597+
return strtounicode(buffer, (char *)buffer);
6598+
}
6599+
6600+
static int
6601+
longtounicode(Py_UNICODE *buffer, size_t len, const char *format, long x)
6602+
{
6603+
PyOS_snprintf((char *)buffer, len, format, x);
6604+
return strtounicode(buffer, (char *)buffer);
6605+
}
6606+
66026607
/* XXX To save some code duplication, formatfloat/long/int could have been
66036608
shared with stringobject.c, converting from 8-bit to Unicode after the
66046609
formatting is done. */
@@ -6648,7 +6653,7 @@ formatfloat(Py_UNICODE *buf,
66486653
PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c",
66496654
(flags&F_ALT) ? "#" : "",
66506655
prec, type);
6651-
return usprintf(buf, fmt, x);
6656+
return doubletounicode(buf, buflen, fmt, x);
66526657
}
66536658

66546659
static PyObject*
@@ -6740,9 +6745,9 @@ formatint(Py_UNICODE *buf,
67406745
prec, type);
67416746
}
67426747
if (sign[0])
6743-
return usprintf(buf, fmt, -x);
6748+
return longtounicode(buf, buflen, fmt, -x);
67446749
else
6745-
return usprintf(buf, fmt, x);
6750+
return longtounicode(buf, buflen, fmt, x);
67466751
}
67476752

67486753
static int

0 commit comments

Comments
 (0)