Embed.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //////////////////// MainFunction ////////////////////
  2. #ifdef __FreeBSD__
  3. #include <floatingpoint.h>
  4. #endif
  5. #if PY_MAJOR_VERSION < 3
  6. int %(main_method)s(int argc, char** argv) {
  7. #elif defined(WIN32) || defined(MS_WINDOWS)
  8. int %(wmain_method)s(int argc, wchar_t **argv) {
  9. #else
  10. static int __Pyx_main(int argc, wchar_t **argv) {
  11. #endif
  12. /* 754 requires that FP exceptions run in "no stop" mode by default,
  13. * and until C vendors implement C99's ways to control FP exceptions,
  14. * Python requires non-stop mode. Alas, some platforms enable FP
  15. * exceptions by default. Here we disable them.
  16. */
  17. #ifdef __FreeBSD__
  18. fp_except_t m;
  19. m = fpgetmask();
  20. fpsetmask(m & ~FP_X_OFL);
  21. #endif
  22. if (argc && argv)
  23. Py_SetProgramName(argv[0]);
  24. Py_Initialize();
  25. if (argc && argv)
  26. PySys_SetArgv(argc, argv);
  27. { /* init module '%(module_name)s' as '__main__' */
  28. PyObject* m = NULL;
  29. %(module_is_main)s = 1;
  30. #if PY_MAJOR_VERSION < 3
  31. init%(module_name)s();
  32. #elif CYTHON_PEP489_MULTI_PHASE_INIT
  33. m = PyInit_%(module_name)s();
  34. if (!PyModule_Check(m)) {
  35. PyModuleDef *mdef = (PyModuleDef *) m;
  36. PyObject *modname = PyUnicode_FromString("__main__");
  37. m = NULL;
  38. if (modname) {
  39. // FIXME: not currently calling PyModule_FromDefAndSpec() here because we do not have a module spec!
  40. // FIXME: not currently setting __file__, __path__, __spec__, ...
  41. m = PyModule_NewObject(modname);
  42. Py_DECREF(modname);
  43. if (m) PyModule_ExecDef(m, mdef);
  44. }
  45. }
  46. #else
  47. m = PyInit_%(module_name)s();
  48. #endif
  49. if (PyErr_Occurred()) {
  50. PyErr_Print(); /* This exits with the right code if SystemExit. */
  51. #if PY_MAJOR_VERSION < 3
  52. if (Py_FlushLine()) PyErr_Clear();
  53. #endif
  54. return 1;
  55. }
  56. Py_XDECREF(m);
  57. }
  58. Py_Finalize();
  59. return 0;
  60. }
  61. #if PY_MAJOR_VERSION >= 3 && !defined(WIN32) && !defined(MS_WINDOWS)
  62. #include <locale.h>
  63. static wchar_t*
  64. __Pyx_char2wchar(char* arg)
  65. {
  66. wchar_t *res;
  67. #ifdef HAVE_BROKEN_MBSTOWCS
  68. /* Some platforms have a broken implementation of
  69. * mbstowcs which does not count the characters that
  70. * would result from conversion. Use an upper bound.
  71. */
  72. size_t argsize = strlen(arg);
  73. #else
  74. size_t argsize = mbstowcs(NULL, arg, 0);
  75. #endif
  76. size_t count;
  77. unsigned char *in;
  78. wchar_t *out;
  79. #ifdef HAVE_MBRTOWC
  80. mbstate_t mbs;
  81. #endif
  82. if (argsize != (size_t)-1) {
  83. res = (wchar_t *)malloc((argsize+1)*sizeof(wchar_t));
  84. if (!res)
  85. goto oom;
  86. count = mbstowcs(res, arg, argsize+1);
  87. if (count != (size_t)-1) {
  88. wchar_t *tmp;
  89. /* Only use the result if it contains no
  90. surrogate characters. */
  91. for (tmp = res; *tmp != 0 &&
  92. (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
  93. ;
  94. if (*tmp == 0)
  95. return res;
  96. }
  97. free(res);
  98. }
  99. /* Conversion failed. Fall back to escaping with surrogateescape. */
  100. #ifdef HAVE_MBRTOWC
  101. /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
  102. /* Overallocate; as multi-byte characters are in the argument, the
  103. actual output could use less memory. */
  104. argsize = strlen(arg) + 1;
  105. res = (wchar_t *)malloc(argsize*sizeof(wchar_t));
  106. if (!res) goto oom;
  107. in = (unsigned char*)arg;
  108. out = res;
  109. memset(&mbs, 0, sizeof mbs);
  110. while (argsize) {
  111. size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
  112. if (converted == 0)
  113. /* Reached end of string; null char stored. */
  114. break;
  115. if (converted == (size_t)-2) {
  116. /* Incomplete character. This should never happen,
  117. since we provide everything that we have -
  118. unless there is a bug in the C library, or I
  119. misunderstood how mbrtowc works. */
  120. fprintf(stderr, "unexpected mbrtowc result -2\\n");
  121. free(res);
  122. return NULL;
  123. }
  124. if (converted == (size_t)-1) {
  125. /* Conversion error. Escape as UTF-8b, and start over
  126. in the initial shift state. */
  127. *out++ = 0xdc00 + *in++;
  128. argsize--;
  129. memset(&mbs, 0, sizeof mbs);
  130. continue;
  131. }
  132. if (*out >= 0xd800 && *out <= 0xdfff) {
  133. /* Surrogate character. Escape the original
  134. byte sequence with surrogateescape. */
  135. argsize -= converted;
  136. while (converted--)
  137. *out++ = 0xdc00 + *in++;
  138. continue;
  139. }
  140. /* successfully converted some bytes */
  141. in += converted;
  142. argsize -= converted;
  143. out++;
  144. }
  145. #else
  146. /* Cannot use C locale for escaping; manually escape as if charset
  147. is ASCII (i.e. escape all bytes > 128. This will still roundtrip
  148. correctly in the locale's charset, which must be an ASCII superset. */
  149. res = (wchar_t *)malloc((strlen(arg)+1)*sizeof(wchar_t));
  150. if (!res) goto oom;
  151. in = (unsigned char*)arg;
  152. out = res;
  153. while(*in)
  154. if(*in < 128)
  155. *out++ = *in++;
  156. else
  157. *out++ = 0xdc00 + *in++;
  158. *out = 0;
  159. #endif
  160. return res;
  161. oom:
  162. fprintf(stderr, "out of memory\\n");
  163. return NULL;
  164. }
  165. int
  166. %(main_method)s(int argc, char **argv)
  167. {
  168. if (!argc) {
  169. return __Pyx_main(0, NULL);
  170. }
  171. else {
  172. int i, res;
  173. wchar_t **argv_copy = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
  174. /* We need a second copy, as Python might modify the first one. */
  175. wchar_t **argv_copy2 = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
  176. char *oldloc = strdup(setlocale(LC_ALL, NULL));
  177. if (!argv_copy || !argv_copy2 || !oldloc) {
  178. fprintf(stderr, "out of memory\\n");
  179. free(argv_copy);
  180. free(argv_copy2);
  181. free(oldloc);
  182. return 1;
  183. }
  184. res = 0;
  185. setlocale(LC_ALL, "");
  186. for (i = 0; i < argc; i++) {
  187. argv_copy2[i] = argv_copy[i] = __Pyx_char2wchar(argv[i]);
  188. if (!argv_copy[i]) res = 1; /* failure, but continue to simplify cleanup */
  189. }
  190. setlocale(LC_ALL, oldloc);
  191. free(oldloc);
  192. if (res == 0)
  193. res = __Pyx_main(argc, argv_copy);
  194. for (i = 0; i < argc; i++) {
  195. free(argv_copy2[i]);
  196. }
  197. free(argv_copy);
  198. free(argv_copy2);
  199. return res;
  200. }
  201. }
  202. #endif