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.

70 lines
1.8 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. int
  15. main(int argc, char **argv)
  16. {
  17. wchar_t **argv_copy;
  18. /* We need a second copy, as Python might modify the first one. */
  19. wchar_t **argv_copy2;
  20. int i, res;
  21. char *oldloc;
  22. argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*(argc+1));
  23. argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*(argc+1));
  24. if (!argv_copy || !argv_copy2) {
  25. fprintf(stderr, "out of memory\n");
  26. return 1;
  27. }
  28. /* 754 requires that FP exceptions run in "no stop" mode by default,
  29. * and until C vendors implement C99's ways to control FP exceptions,
  30. * Python requires non-stop mode. Alas, some platforms enable FP
  31. * exceptions by default. Here we disable them.
  32. */
  33. #ifdef __FreeBSD__
  34. fp_except_t m;
  35. m = fpgetmask();
  36. fpsetmask(m & ~FP_X_OFL);
  37. #endif
  38. oldloc = strdup(setlocale(LC_ALL, NULL));
  39. setlocale(LC_ALL, "");
  40. for (i = 0; i < argc; i++) {
  41. argv_copy[i] = _Py_char2wchar(argv[i], NULL);
  42. if (!argv_copy[i]) {
  43. free(oldloc);
  44. fprintf(stderr, "Fatal Python error: "
  45. "unable to decode the command line argument #%i\n",
  46. i + 1);
  47. return 1;
  48. }
  49. argv_copy2[i] = argv_copy[i];
  50. }
  51. argv_copy2[argc] = argv_copy[argc] = NULL;
  52. setlocale(LC_ALL, oldloc);
  53. free(oldloc);
  54. res = Py_Main(argc, argv_copy);
  55. for (i = 0; i < argc; i++) {
  56. PyMem_Free(argv_copy2[i]);
  57. }
  58. PyMem_Free(argv_copy);
  59. PyMem_Free(argv_copy2);
  60. return res;
  61. }
  62. #endif