comparison Modules/python.c @ 60688:fe6b6c189bb4

Move _Py_char2wchar from python.c to main.c. This fixes issue #8441: python.c is not included in the framework while main.c is and without this patch you get a link error when building Python.framework on OSX.
author Ronald Oussoren <ronaldoussoren@mac.com>
date Sun, 18 Apr 2010 14:46:12 +0000
parents c532afb0fa89
children cab14be0ada1
comparison
equal deleted inserted replaced
60683:fa8a03dfc2d1 60688:fe6b6c189bb4
12 wmain(int argc, wchar_t **argv) 12 wmain(int argc, wchar_t **argv)
13 { 13 {
14 return Py_Main(argc, argv); 14 return Py_Main(argc, argv);
15 } 15 }
16 #else 16 #else
17 wchar_t*
18 _Py_char2wchar(char* arg)
19 {
20 wchar_t *res;
21 #ifdef HAVE_BROKEN_MBSTOWCS
22 /* Some platforms have a broken implementation of
23 * mbstowcs which does not count the characters that
24 * would result from conversion. Use an upper bound.
25 */
26 size_t argsize = strlen(arg);
27 #else
28 size_t argsize = mbstowcs(NULL, arg, 0);
29 #endif
30 size_t count;
31 unsigned char *in;
32 wchar_t *out;
33 #ifdef HAVE_MBRTOWC
34 mbstate_t mbs;
35 #endif
36 if (argsize != (size_t)-1) {
37 res = (wchar_t *)PyMem_Malloc((argsize+1)*sizeof(wchar_t));
38 if (!res)
39 goto oom;
40 count = mbstowcs(res, arg, argsize+1);
41 if (count != (size_t)-1) {
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 }
51 PyMem_Free(res);
52 }
53 /* Conversion failed. Fall back to escaping with surrogateescape. */
54 #ifdef HAVE_MBRTOWC
55 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
56
57 /* Overallocate; as multi-byte characters are in the argument, the
58 actual output could use less memory. */
59 argsize = strlen(arg) + 1;
60 res = (wchar_t*)PyMem_Malloc(argsize*sizeof(wchar_t));
61 if (!res) goto oom;
62 in = (unsigned char*)arg;
63 out = res;
64 memset(&mbs, 0, sizeof mbs);
65 while (argsize) {
66 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
67 if (converted == 0)
68 /* Reached end of string; null char stored. */
69 break;
70 if (converted == (size_t)-2) {
71 /* Incomplete character. This should never happen,
72 since we provide everything that we have -
73 unless there is a bug in the C library, or I
74 misunderstood how mbrtowc works. */
75 fprintf(stderr, "unexpected mbrtowc result -2\n");
76 return NULL;
77 }
78 if (converted == (size_t)-1) {
79 /* Conversion error. Escape as UTF-8b, and start over
80 in the initial shift state. */
81 *out++ = 0xdc00 + *in++;
82 argsize--;
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++;
92 continue;
93 }
94 /* successfully converted some bytes */
95 in += converted;
96 argsize -= converted;
97 out++;
98 }
99 #else
100 /* Cannot use C locale for escaping; manually escape as if charset
101 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
102 correctly in the locale's charset, which must be an ASCII superset. */
103 res = PyMem_Malloc((strlen(arg)+1)*sizeof(wchar_t));
104 if (!res) goto oom;
105 in = (unsigned char*)arg;
106 out = res;
107 while(*in)
108 if(*in < 128)
109 *out++ = *in++;
110 else
111 *out++ = 0xdc00 + *in++;
112 *out = 0;
113 #endif
114 return res;
115 oom:
116 fprintf(stderr, "out of memory\n");
117 return NULL;
118 }
119 17
120 int 18 int
121 main(int argc, char **argv) 19 main(int argc, char **argv)
122 { 20 {
123 wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc); 21 wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);