Commits

Martin von Löwis committed 082cef3

Change %s argument for PyUnicode_FromFormat to
be UTF-8. Fixes #1070.

Comments (0)

Files changed (1)

Objects/unicodeobject.c

 					abuffersize = width;
 				break;
 			case 's':
-				n += strlen(va_arg(count, char*));
+			{
+				/* UTF-8 */
+				unsigned char*s;
+				s = va_arg(count, unsigned char*);
+				while (*s) {
+					if (*s < 128) {
+						n++; s++;
+					} else if (*s < 0xc0) {
+						/* invalid UTF-8 */
+						n++; s++;
+					} else if (*s < 0xc0) {
+						n++;
+						s++; if(!*s)break;
+						s++;
+					} else if (*s < 0xe0) {
+						n++;
+						s++; if(!*s)break;
+						s++; if(!*s)break;
+						s++;
+					} else {
+						#ifdef Py_UNICODE_WIDE
+						n++;
+						#else
+						n+=2;
+						#endif
+						s++; if(!*s)break;
+						s++; if(!*s)break;
+						s++; if(!*s)break;
+						s++;
+					}
+				}
 				break;
+			}
 			case 'U':
 			{
 				PyObject *obj = va_arg(count, PyObject *);
 				appendstring(realbuffer);
 				break;
 			case 's':
+			{
+				/* Parameter must be UTF-8 encoded.
+				   In case of encoding errors, use
+				   the replacement character. */
+				PyObject *u;
 				p = va_arg(vargs, char*);
-				appendstring(p);
+				u = PyUnicode_DecodeUTF8(p, strlen(p), 
+							 "replace");
+				if (!u)
+					goto fail;
+				Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(u),
+						PyUnicode_GET_SIZE(u));
+				s += PyUnicode_GET_SIZE(u);
+				Py_DECREF(u);
 				break;
+			}
 			case 'U':
 			{
 				PyObject *obj = va_arg(vargs, PyObject *);