Embed.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #if PY_VERSION_HEX < 0x03060000
  59. Py_Finalize();
  60. #else
  61. if (Py_FinalizeEx() < 0)
  62. return 2;
  63. #endif
  64. return 0;
  65. }
  66. #if PY_MAJOR_VERSION >= 3 && !defined(WIN32) && !defined(MS_WINDOWS)
  67. #include <locale.h>
  68. static wchar_t*
  69. __Pyx_char2wchar(char* arg)
  70. {
  71. wchar_t *res;
  72. #ifdef HAVE_BROKEN_MBSTOWCS
  73. /* Some platforms have a broken implementation of
  74. * mbstowcs which does not count the characters that
  75. * would result from conversion. Use an upper bound.
  76. */
  77. size_t argsize = strlen(arg);
  78. #else
  79. size_t argsize = mbstowcs(NULL, arg, 0);
  80. #endif
  81. size_t count;
  82. unsigned char *in;
  83. wchar_t *out;
  84. #ifdef HAVE_MBRTOWC
  85. mbstate_t mbs;
  86. #endif
  87. if (argsize != (size_t)-1) {
  88. res = (wchar_t *)malloc((argsize+1)*sizeof(wchar_t));
  89. if (!res)
  90. goto oom;
  91. count = mbstowcs(res, arg, argsize+1);
  92. if (count != (size_t)-1) {
  93. wchar_t *tmp;
  94. /* Only use the result if it contains no
  95. surrogate characters. */
  96. for (tmp = res; *tmp != 0 &&
  97. (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
  98. ;
  99. if (*tmp == 0)
  100. return res;
  101. }
  102. free(res);
  103. }
  104. /* Conversion failed. Fall back to escaping with surrogateescape. */
  105. #ifdef HAVE_MBRTOWC
  106. /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
  107. /* Overallocate; as multi-byte characters are in the argument, the
  108. actual output could use less memory. */
  109. argsize = strlen(arg) + 1;
  110. res = (wchar_t *)malloc(argsize*sizeof(wchar_t));
  111. if (!res) goto oom;
  112. in = (unsigned char*)arg;
  113. out = res;
  114. memset(&mbs, 0, sizeof mbs);
  115. while (argsize) {
  116. size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
  117. if (converted == 0)
  118. /* Reached end of string; null char stored. */
  119. break;
  120. if (converted == (size_t)-2) {
  121. /* Incomplete character. This should never happen,
  122. since we provide everything that we have -
  123. unless there is a bug in the C library, or I
  124. misunderstood how mbrtowc works. */
  125. fprintf(stderr, "unexpected mbrtowc result -2\\n");
  126. free(res);
  127. return NULL;
  128. }
  129. if (converted == (size_t)-1) {
  130. /* Conversion error. Escape as UTF-8b, and start over
  131. in the initial shift state. */
  132. *out++ = 0xdc00 + *in++;
  133. argsize--;
  134. memset(&mbs, 0, sizeof mbs);
  135. continue;
  136. }
  137. if (*out >= 0xd800 && *out <= 0xdfff) {
  138. /* Surrogate character. Escape the original
  139. byte sequence with surrogateescape. */
  140. argsize -= converted;
  141. while (converted--)
  142. *out++ = 0xdc00 + *in++;
  143. continue;
  144. }
  145. /* successfully converted some bytes */
  146. in += converted;
  147. argsize -= converted;
  148. out++;
  149. }
  150. #else
  151. /* Cannot use C locale for escaping; manually escape as if charset
  152. is ASCII (i.e. escape all bytes > 128. This will still roundtrip
  153. correctly in the locale's charset, which must be an ASCII superset. */
  154. res = (wchar_t *)malloc((strlen(arg)+1)*sizeof(wchar_t));
  155. if (!res) goto oom;
  156. in = (unsigned char*)arg;
  157. out = res;
  158. while(*in)
  159. if(*in < 128)
  160. *out++ = *in++;
  161. else
  162. *out++ = 0xdc00 + *in++;
  163. *out = 0;
  164. #endif
  165. return res;
  166. oom:
  167. fprintf(stderr, "out of memory\\n");
  168. return NULL;
  169. }
  170. int
  171. %(main_method)s(int argc, char **argv)
  172. {
  173. if (!argc) {
  174. return __Pyx_main(0, NULL);
  175. }
  176. else {
  177. int i, res;
  178. wchar_t **argv_copy = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
  179. /* We need a second copy, as Python might modify the first one. */
  180. wchar_t **argv_copy2 = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
  181. char *oldloc = strdup(setlocale(LC_ALL, NULL));
  182. if (!argv_copy || !argv_copy2 || !oldloc) {
  183. fprintf(stderr, "out of memory\\n");
  184. free(argv_copy);
  185. free(argv_copy2);
  186. free(oldloc);
  187. return 1;
  188. }
  189. res = 0;
  190. setlocale(LC_ALL, "");
  191. for (i = 0; i < argc; i++) {
  192. argv_copy2[i] = argv_copy[i] = __Pyx_char2wchar(argv[i]);
  193. if (!argv_copy[i]) res = 1; /* failure, but continue to simplify cleanup */
  194. }
  195. setlocale(LC_ALL, oldloc);
  196. free(oldloc);
  197. if (res == 0)
  198. res = __Pyx_main(argc, argv_copy);
  199. for (i = 0; i < argc; i++) {
  200. #if PY_VERSION_HEX < 0x03050000
  201. free(argv_copy2[i]);
  202. #else
  203. PyMem_RawFree(argv_copy2[i]);
  204. #endif
  205. }
  206. free(argv_copy);
  207. free(argv_copy2);
  208. return res;
  209. }
  210. }
  211. #endif