You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.9 KiB

  1. /* Minimal main program -- everything is loaded from the library */
  2. #include "Python.h"
  3. #include <locale.h>
  4. #ifdef __FreeBSD__
  5. #include <floatingpoint.h>
  6. #endif
  7. #ifdef MS_WINDOWS
  8. int
  9. wmain(int argc, wchar_t **argv)
  10. {
  11. return Py_Main(argc, argv);
  12. }
  13. #else
  14. #ifdef __APPLE__
  15. extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size);
  16. #endif
  17. int
  18. main(int argc, char **argv)
  19. {
  20. wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);
  21. /* We need a second copies, as Python might modify the first one. */
  22. wchar_t **argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*argc);
  23. int i, res;
  24. char *oldloc;
  25. /* 754 requires that FP exceptions run in "no stop" mode by default,
  26. * and until C vendors implement C99's ways to control FP exceptions,
  27. * Python requires non-stop mode. Alas, some platforms enable FP
  28. * exceptions by default. Here we disable them.
  29. */
  30. #ifdef __FreeBSD__
  31. fp_except_t m;
  32. m = fpgetmask();
  33. fpsetmask(m & ~FP_X_OFL);
  34. #endif
  35. if (!argv_copy || !argv_copy2) {
  36. fprintf(stderr, "out of memory\n");
  37. return 1;
  38. }
  39. oldloc = strdup(setlocale(LC_ALL, NULL));
  40. setlocale(LC_ALL, "");
  41. for (i = 0; i < argc; i++) {
  42. #ifdef __APPLE__
  43. argv_copy[i] = _Py_DecodeUTF8_surrogateescape(argv[i], strlen(argv[i]));
  44. #else
  45. argv_copy[i] = _Py_char2wchar(argv[i], NULL);
  46. #endif
  47. if (!argv_copy[i]) {
  48. fprintf(stderr, "Fatal Python error: "
  49. "unable to decode the command line argument #%i\n",
  50. i + 1);
  51. return 1;
  52. }
  53. argv_copy2[i] = argv_copy[i];
  54. }
  55. setlocale(LC_ALL, oldloc);
  56. free(oldloc);
  57. res = Py_Main(argc, argv_copy);
  58. for (i = 0; i < argc; i++) {
  59. PyMem_Free(argv_copy2[i]);
  60. }
  61. PyMem_Free(argv_copy);
  62. PyMem_Free(argv_copy2);
  63. return res;
  64. }
  65. #endif