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.

73 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 <fenv.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_RawMalloc(sizeof(wchar_t*) * (argc+1));
  23. argv_copy2 = (wchar_t **)PyMem_RawMalloc(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. fedisableexcept(FE_OVERFLOW);
  35. #endif
  36. oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
  37. if (!oldloc) {
  38. fprintf(stderr, "out of memory\n");
  39. return 1;
  40. }
  41. setlocale(LC_ALL, "");
  42. for (i = 0; i < argc; i++) {
  43. argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
  44. if (!argv_copy[i]) {
  45. PyMem_RawFree(oldloc);
  46. fprintf(stderr, "Fatal Python error: "
  47. "unable to decode the command line argument #%i\n",
  48. i + 1);
  49. return 1;
  50. }
  51. argv_copy2[i] = argv_copy[i];
  52. }
  53. argv_copy2[argc] = argv_copy[argc] = NULL;
  54. setlocale(LC_ALL, oldloc);
  55. PyMem_RawFree(oldloc);
  56. res = Py_Main(argc, argv_copy);
  57. for (i = 0; i < argc; i++) {
  58. PyMem_RawFree(argv_copy2[i]);
  59. }
  60. PyMem_RawFree(argv_copy);
  61. PyMem_RawFree(argv_copy2);
  62. return res;
  63. }
  64. #endif