comparison Modules/python.c @ 54427:ec5d228a83ef

Issue #6097: Escape UTF-8 surrogates resulting from mbstocs conversion of the command line.
author Martin v. Löwis <martin@v.loewis.de>
date Fri, 29 May 2009 16:22:26 +0000
parents 7c29bfbfd352
children f197407bdba2 62d11785204f
comparison
equal deleted inserted replaced
54425:134d12c2e745 54427:ec5d228a83ef
36 if (argsize != (size_t)-1) { 36 if (argsize != (size_t)-1) {
37 res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t)); 37 res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t));
38 if (!res) 38 if (!res)
39 goto oom; 39 goto oom;
40 count = mbstowcs(res, arg, argsize+1); 40 count = mbstowcs(res, arg, argsize+1);
41 if (count != (size_t)-1) 41 if (count != (size_t)-1) {
42 return res; 42 wchar_t *tmp;
43 /* Only use the result if it contains no
44 surrogate characters. */
45 for (tmp = res; *tmp != 0 &&
46 (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
47 ;
48 if (*tmp == 0)
49 return res;
50 }
43 PyMem_Free(res); 51 PyMem_Free(res);
44 } 52 }
45 /* Conversion failed. Fall back to escaping with surrogateescape. */ 53 /* Conversion failed. Fall back to escaping with surrogateescape. */
46 #ifdef HAVE_MBRTOWC 54 #ifdef HAVE_MBRTOWC
47 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */ 55 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
71 /* Conversion error. Escape as UTF-8b, and start over 79 /* Conversion error. Escape as UTF-8b, and start over
72 in the initial shift state. */ 80 in the initial shift state. */
73 *out++ = 0xdc00 + *in++; 81 *out++ = 0xdc00 + *in++;
74 argsize--; 82 argsize--;
75 memset(&mbs, 0, sizeof mbs); 83 memset(&mbs, 0, sizeof mbs);
84 continue;
85 }
86 if (*out >= 0xd800 && *out <= 0xdfff) {
87 /* Surrogate character. Escape the original
88 byte sequence with surrogateescape. */
89 argsize -= converted;
90 while (converted--)
91 *out++ = 0xdc00 + *in++;
76 continue; 92 continue;
77 } 93 }
78 /* successfully converted some bytes */ 94 /* successfully converted some bytes */
79 in += converted; 95 in += converted;
80 argsize -= converted; 96 argsize -= converted;