Commit 659eb844 authored by Victor Stinner's avatar Victor Stinner

Merged revisions 88481 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r88481 | victor.stinner | 2011-02-21 22:13:44 +0100 (lun., 21 févr. 2011) | 4 lines

  Fix PyUnicode_FromFormatV("%c") for non-BMP char

  Issue #10830: Fix PyUnicode_FromFormatV("%c") for non-BMP characters on
  narrow build.
........
parent 02bfdb3f
...@@ -1427,7 +1427,7 @@ class UnicodeTest(string_tests.CommonTest, ...@@ -1427,7 +1427,7 @@ class UnicodeTest(string_tests.CommonTest,
# Test PyUnicode_FromFormat() # Test PyUnicode_FromFormat()
def test_from_format(self): def test_from_format(self):
support.import_module('ctypes') support.import_module('ctypes')
from ctypes import pythonapi, py_object from ctypes import pythonapi, py_object, c_int
if sys.maxunicode == 65535: if sys.maxunicode == 65535:
name = "PyUnicodeUCS2_FromFormat" name = "PyUnicodeUCS2_FromFormat"
else: else:
...@@ -1452,6 +1452,9 @@ class UnicodeTest(string_tests.CommonTest, ...@@ -1452,6 +1452,9 @@ class UnicodeTest(string_tests.CommonTest,
'string, got a non-ASCII byte: 0xe9$', 'string, got a non-ASCII byte: 0xe9$',
PyUnicode_FromFormat, b'unicode\xe9=%s', 'ascii') PyUnicode_FromFormat, b'unicode\xe9=%s', 'ascii')
self.assertEqual(PyUnicode_FromFormat(b'%c', c_int(0xabcd)), '\uabcd')
self.assertEqual(PyUnicode_FromFormat(b'%c', c_int(0x10ffff)), '\U0010ffff')
# other tests # other tests
text = PyUnicode_FromFormat(b'%%A:%A', 'abc\xe9\uabcd\U0010ffff') text = PyUnicode_FromFormat(b'%%A:%A', 'abc\xe9\uabcd\U0010ffff')
self.assertEqual(text, r"%A:'abc\xe9\uabcd\U0010ffff'") self.assertEqual(text, r"%A:'abc\xe9\uabcd\U0010ffff'")
......
...@@ -13,6 +13,9 @@ Core and Builtins ...@@ -13,6 +13,9 @@ Core and Builtins
- Issue #11272: On Windows, input() strips '\r' (and not only '\n'), and - Issue #11272: On Windows, input() strips '\r' (and not only '\n'), and
sys.stdin uses universal newline (replace '\r\n' by '\n'). sys.stdin uses universal newline (replace '\r\n' by '\n').
- Issue #10830: Fix PyUnicode_FromFormatV("%c") for non-BMP characters on
narrow build.
- Check for NULL result in PyType_FromSpec. - Check for NULL result in PyType_FromSpec.
Library Library
......
...@@ -813,8 +813,19 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) ...@@ -813,8 +813,19 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
switch (*f) { switch (*f) {
case 'c': case 'c':
{
#ifndef Py_UNICODE_WIDE
int ordinal = va_arg(count, int);
if (ordinal > 0xffff)
n += 2;
else
n++;
#else
(void)va_arg(count, int); (void)va_arg(count, int);
/* fall through... */ n++;
#endif
break;
}
case '%': case '%':
n++; n++;
break; break;
...@@ -992,8 +1003,18 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) ...@@ -992,8 +1003,18 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
switch (*f) { switch (*f) {
case 'c': case 'c':
*s++ = va_arg(vargs, int); {
int ordinal = va_arg(vargs, int);
#ifndef Py_UNICODE_WIDE
if (ordinal > 0xffff) {
ordinal -= 0x10000;
*s++ = 0xD800 | (ordinal >> 10);
*s++ = 0xDC00 | (ordinal & 0x3FF);
} else
#endif
*s++ = ordinal;
break; break;
}
case 'd': case 'd':
makefmt(fmt, longflag, longlongflag, size_tflag, zeropad, makefmt(fmt, longflag, longlongflag, size_tflag, zeropad,
width, precision, 'd'); width, precision, 'd');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment