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.

74 lines
2.0 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+1));
  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+1));
  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. free(oldloc);
  49. fprintf(stderr, "Fatal Python error: "
  50. "unable to decode the command line argument #%i\n",
  51. i + 1);
  52. return 1;
  53. }
  54. argv_copy2[i] = argv_copy[i];
  55. }
  56. argv_copy2[argc] = argv_copy[argc] = NULL;
  57. setlocale(LC_ALL, oldloc);
  58. free(oldloc);
  59. res = Py_Main(argc, argv_copy);
  60. for (i = 0; i < argc; i++) {
  61. PyMem_Free(argv_copy2[i]);
  62. }
  63. PyMem_Free(argv_copy);
  64. PyMem_Free(argv_copy2);
  65. return res;
  66. }
  67. #endif