diff -r 568019d6bf6b Include/unicodeobject.h --- a/Include/unicodeobject.h Sat Dec 22 14:57:16 2012 +0100 +++ b/Include/unicodeobject.h Sun Dec 23 18:19:57 2012 +0200 @@ -737,12 +737,13 @@ Py_ssize_t end); #ifndef Py_LIMITED_API -/* Compute the maximum character of the substring unicode[start:end]. - Return 127 for an empty string. */ +/* Compute the maximum character of the substring unicode[start:end] and + maxchar. */ PyAPI_FUNC(Py_UCS4) _PyUnicode_FindMaxChar ( PyObject *unicode, Py_ssize_t start, - Py_ssize_t end); + Py_ssize_t end, + Py_UCS4 maxchar); #endif /* Copy the string into a UCS4 buffer including the null character if copy_null diff -r 568019d6bf6b Objects/stringlib/unicode_format.h --- a/Objects/stringlib/unicode_format.h Sat Dec 22 14:57:16 2012 +0100 +++ b/Objects/stringlib/unicode_format.h Sun Dec 23 18:19:57 2012 +0200 @@ -887,7 +887,8 @@ sublen = literal.end - literal.start; if (sublen) { maxchar = _PyUnicode_FindMaxChar(literal.str, - literal.start, literal.end); + literal.start, literal.end, + writer->maxchar); err = _PyUnicodeWriter_Prepare(writer, sublen, maxchar); if (err == -1) return 0; diff -r 568019d6bf6b Objects/unicodeobject.c --- a/Objects/unicodeobject.c Sat Dec 22 14:57:16 2012 +0100 +++ b/Objects/unicodeobject.c Sun Dec 23 18:19:57 2012 +0200 @@ -2002,7 +2002,8 @@ } Py_UCS4 -_PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end) +_PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end, + Py_UCS4 maxchar) { enum PyUnicode_Kind kind; void *startptr, *endptr; @@ -2013,13 +2014,16 @@ assert(start <= end); if (start == 0 && end == PyUnicode_GET_LENGTH(unicode)) - return PyUnicode_MAX_CHAR_VALUE(unicode); + return MAX_MAXCHAR(maxchar, PyUnicode_MAX_CHAR_VALUE(unicode)); if (start == end) - return 127; + return maxchar; if (PyUnicode_IS_ASCII(unicode)) - return 127; + return maxchar; + + if (maxchar >= PyUnicode_MAX_CHAR_VALUE(unicode)) + return maxchar; kind = PyUnicode_KIND(unicode); startptr = PyUnicode_DATA(unicode); @@ -2027,11 +2031,11 @@ startptr = (char *)startptr + start * kind; switch(kind) { case PyUnicode_1BYTE_KIND: - return ucs1lib_find_max_char(startptr, endptr); + return MAX_MAXCHAR(maxchar, ucs1lib_find_max_char(startptr, endptr)); case PyUnicode_2BYTE_KIND: - return ucs2lib_find_max_char(startptr, endptr); + return MAX_MAXCHAR(maxchar, ucs2lib_find_max_char(startptr, endptr)); case PyUnicode_4BYTE_KIND: - return ucs4lib_find_max_char(startptr, endptr); + return MAX_MAXCHAR(maxchar, ucs4lib_find_max_char(startptr, endptr)); default: assert(0); return 0; @@ -13740,7 +13744,7 @@ Py_ssize_t pindex; Py_UCS4 signchar; Py_ssize_t buflen; - Py_UCS4 maxchar, bufmaxchar; + Py_UCS4 bufmaxchar; Py_ssize_t sublen; _PyUnicodeWriter *writer = &ctx->writer; Py_UCS4 fill; @@ -13793,7 +13797,7 @@ arg->width = len; /* Prepare the writer */ - bufmaxchar = 127; + bufmaxchar = writer->maxchar; if (!(arg->flags & F_LJUST)) { if (arg->sign) { if ((arg->width-1) > len) @@ -13804,8 +13808,7 @@ bufmaxchar = MAX_MAXCHAR(bufmaxchar, fill); } } - maxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len); - bufmaxchar = MAX_MAXCHAR(bufmaxchar, maxchar); + bufmaxchar = _PyUnicode_FindMaxChar(str, 0, pindex+len, bufmaxchar); buflen = arg->width; if (arg->sign && len == arg->width) buflen++; @@ -13976,7 +13979,8 @@ } sublen = ctx.fmtpos - nonfmtpos; maxchar = _PyUnicode_FindMaxChar(ctx.fmtstr, - nonfmtpos, nonfmtpos + sublen); + nonfmtpos, nonfmtpos + sublen, + ctx.writer.maxchar); if (_PyUnicodeWriter_Prepare(&ctx.writer, sublen, maxchar) == -1) goto onError; diff -r 568019d6bf6b Python/formatter_unicode.c --- a/Python/formatter_unicode.c Sat Dec 22 14:57:16 2012 +0100 +++ b/Python/formatter_unicode.c Sun Dec 23 18:19:57 2012 +0200 @@ -771,7 +771,7 @@ calc_padding(len, format->width, format->align, &lpad, &rpad, &total); - maxchar = _PyUnicode_FindMaxChar(value, 0, len); + maxchar = _PyUnicode_FindMaxChar(value, 0, len, 127); if (lpad != 0 || rpad != 0) maxchar = Py_MAX(maxchar, format->fill_char);