Embed.c 7.7 KB

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