diff Objects/unicodeobject.c @ 68087:ef28aad3bbae

Fix PyUnicode_FromFormatV("%c") for non-BMP char Issue #10830: Fix PyUnicode_FromFormatV("%c") for non-BMP characters on narrow build.
author Victor Stinner <victor.stinner@haypocalc.com>
date Mon, 21 Feb 2011 21:13:44 +0000
parents ab1664b298fc
children e2f96bd29252
line wrap: on
line diff
--- a/Objects/unicodeobject.c	Mon Feb 21 21:05:50 2011 +0000
+++ b/Objects/unicodeobject.c	Mon Feb 21 21:13:44 2011 +0000
@@ -813,8 +813,19 @@
 
             switch (*f) {
             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);
-                /* fall through... */
+                n++;
+#endif
+                break;
+            }
             case '%':
                 n++;
                 break;
@@ -992,8 +1003,18 @@
 
             switch (*f) {
             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;
+            }
             case 'd':
                 makefmt(fmt, longflag, longlongflag, size_tflag, zeropad,
                         width, precision, 'd');