1 //////////////////// MainFunction ////////////////////
4 #include <floatingpoint.h>
7 #if PY_MAJOR_VERSION < 3
8 int %(main_method)s(int argc, char** argv) {
9 #elif defined(WIN32) || defined(MS_WINDOWS)
10 int %(wmain_method)s(int argc, wchar_t **argv) {
12 static int __Pyx_main(int argc, wchar_t **argv) {
14 /* 754 requires that FP exceptions run in "no stop" mode by default,
15 * and until C vendors implement C99's ways to control FP exceptions,
16 * Python requires non-stop mode. Alas, some platforms enable FP
17 * exceptions by default. Here we disable them.
23 fpsetmask(m & ~FP_X_OFL);
26 Py_SetProgramName(argv[0]);
29 PySys_SetArgv(argc, argv);
30 { /* init module '%(module_name)s' as '__main__' */
32 %(module_is_main)s = 1;
33 #if PY_MAJOR_VERSION < 3
34 init%(module_name)s();
36 m = PyInit_%(module_name)s();
38 if (PyErr_Occurred()) {
39 PyErr_Print(); /* This exits with the right code if SystemExit. */
40 #if PY_MAJOR_VERSION < 3
41 if (Py_FlushLine()) PyErr_Clear();
52 #if PY_MAJOR_VERSION >= 3 && !defined(WIN32) && !defined(MS_WINDOWS)
56 __Pyx_char2wchar(char* arg)
59 #ifdef HAVE_BROKEN_MBSTOWCS
60 /* Some platforms have a broken implementation of
61 * mbstowcs which does not count the characters that
62 * would result from conversion. Use an upper bound.
64 size_t argsize = strlen(arg);
66 size_t argsize = mbstowcs(NULL, arg, 0);
74 if (argsize != (size_t)-1) {
75 res = (wchar_t *)malloc((argsize+1)*sizeof(wchar_t));
78 count = mbstowcs(res, arg, argsize+1);
79 if (count != (size_t)-1) {
81 /* Only use the result if it contains no
82 surrogate characters. */
83 for (tmp = res; *tmp != 0 &&
84 (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
91 /* Conversion failed. Fall back to escaping with surrogateescape. */
93 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
95 /* Overallocate; as multi-byte characters are in the argument, the
96 actual output could use less memory. */
97 argsize = strlen(arg) + 1;
98 res = malloc(argsize*sizeof(wchar_t));
100 in = (unsigned char*)arg;
102 memset(&mbs, 0, sizeof mbs);
104 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
106 /* Reached end of string; null char stored. */
108 if (converted == (size_t)-2) {
109 /* Incomplete character. This should never happen,
110 since we provide everything that we have -
111 unless there is a bug in the C library, or I
112 misunderstood how mbrtowc works. */
113 fprintf(stderr, "unexpected mbrtowc result -2\\n");
116 if (converted == (size_t)-1) {
117 /* Conversion error. Escape as UTF-8b, and start over
118 in the initial shift state. */
119 *out++ = 0xdc00 + *in++;
121 memset(&mbs, 0, sizeof mbs);
124 if (*out >= 0xd800 && *out <= 0xdfff) {
125 /* Surrogate character. Escape the original
126 byte sequence with surrogateescape. */
127 argsize -= converted;
129 *out++ = 0xdc00 + *in++;
132 /* successfully converted some bytes */
134 argsize -= converted;
138 /* Cannot use C locale for escaping; manually escape as if charset
139 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
140 correctly in the locale's charset, which must be an ASCII superset. */
141 res = malloc((strlen(arg)+1)*sizeof(wchar_t));
143 in = (unsigned char*)arg;
149 *out++ = 0xdc00 + *in++;
154 fprintf(stderr, "out of memory\\n");
159 %(main_method)s(int argc, char **argv)
162 return __Pyx_main(0, NULL);
165 wchar_t **argv_copy = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
166 /* We need a second copies, as Python might modify the first one. */
167 wchar_t **argv_copy2 = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
170 if (!argv_copy || !argv_copy2) {
171 fprintf(stderr, "out of memory\\n");
174 oldloc = strdup(setlocale(LC_ALL, NULL));
175 setlocale(LC_ALL, "");
176 for (i = 0; i < argc; i++) {
177 argv_copy2[i] = argv_copy[i] = __Pyx_char2wchar(argv[i]);
181 setlocale(LC_ALL, oldloc);
183 res = __Pyx_main(argc, argv_copy);
184 for (i = 0; i < argc; i++) {