1 /* Minimal main program -- everything is loaded from the library */
7 #include <floatingpoint.h>
12 wmain(int argc
, wchar_t **argv
)
14 return Py_Main(argc
, argv
);
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.
26 size_t argsize
= strlen(arg
);
28 size_t argsize
= mbstowcs(NULL
, arg
, 0);
36 if (argsize
!= (size_t)-1) {
37 res
= (wchar_t *)PyMem_Malloc((argsize
+1)*sizeof(wchar_t));
40 count
= mbstowcs(res
, arg
, argsize
+1);
41 if (count
!= (size_t)-1) {
43 /* Only use the result if it contains no
44 surrogate characters. */
45 for (tmp
= res
; *tmp
!= 0 &&
46 (*tmp
< 0xd800 || *tmp
> 0xdfff); tmp
++)
53 /* Conversion failed. Fall back to escaping with surrogateescape. */
55 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
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
= PyMem_Malloc(argsize
*sizeof(wchar_t));
62 in
= (unsigned char*)arg
;
64 memset(&mbs
, 0, sizeof mbs
);
66 size_t converted
= mbrtowc(out
, (char*)in
, argsize
, &mbs
);
68 /* Reached end of string; null char stored. */
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");
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
++;
83 memset(&mbs
, 0, sizeof mbs
);
86 if (*out
>= 0xd800 && *out
<= 0xdfff) {
87 /* Surrogate character. Escape the original
88 byte sequence with surrogateescape. */
91 *out
++ = 0xdc00 + *in
++;
94 /* successfully converted some bytes */
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));
105 in
= (unsigned char*)arg
;
111 *out
++ = 0xdc00 + *in
++;
116 fprintf(stderr
, "out of memory\n");
121 main(int argc
, char **argv
)
123 wchar_t **argv_copy
= (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc
);
124 /* We need a second copies, as Python might modify the first one. */
125 wchar_t **argv_copy2
= (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc
);
128 /* 754 requires that FP exceptions run in "no stop" mode by default,
129 * and until C vendors implement C99's ways to control FP exceptions,
130 * Python requires non-stop mode. Alas, some platforms enable FP
131 * exceptions by default. Here we disable them.
137 fpsetmask(m
& ~FP_X_OFL
);
139 if (!argv_copy
|| !argv_copy2
) {
140 fprintf(stderr
, "out of memory\n");
143 oldloc
= strdup(setlocale(LC_ALL
, NULL
));
144 setlocale(LC_ALL
, "");
145 for (i
= 0; i
< argc
; i
++) {
146 argv_copy2
[i
] = argv_copy
[i
] = char2wchar(argv
[i
]);
150 setlocale(LC_ALL
, oldloc
);
152 res
= Py_Main(argc
, argv_copy
);
153 for (i
= 0; i
< argc
; i
++) {
154 PyMem_Free(argv_copy2
[i
]);
156 PyMem_Free(argv_copy
);
157 PyMem_Free(argv_copy2
);