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.

82 lines
2.1 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. /* Force malloc() allocator to bootstrap Python */
  23. (void)_PyMem_SetupAllocators("malloc");
  24. argv_copy = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
  25. argv_copy2 = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
  26. if (!argv_copy || !argv_copy2) {
  27. fprintf(stderr, "out of memory\n");
  28. return 1;
  29. }
  30. /* 754 requires that FP exceptions run in "no stop" mode by default,
  31. * and until C vendors implement C99's ways to control FP exceptions,
  32. * Python requires non-stop mode. Alas, some platforms enable FP
  33. * exceptions by default. Here we disable them.
  34. */
  35. #ifdef __FreeBSD__
  36. fedisableexcept(FE_OVERFLOW);
  37. #endif
  38. oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
  39. if (!oldloc) {
  40. fprintf(stderr, "out of memory\n");
  41. return 1;
  42. }
  43. setlocale(LC_ALL, "");
  44. for (i = 0; i < argc; i++) {
  45. argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
  46. if (!argv_copy[i]) {
  47. PyMem_RawFree(oldloc);
  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. argv_copy2[argc] = argv_copy[argc] = NULL;
  56. setlocale(LC_ALL, oldloc);
  57. PyMem_RawFree(oldloc);
  58. res = Py_Main(argc, argv_copy);
  59. /* Force again malloc() allocator to release memory blocks allocated
  60. before Py_Main() */
  61. (void)_PyMem_SetupAllocators("malloc");
  62. for (i = 0; i < argc; i++) {
  63. PyMem_RawFree(argv_copy2[i]);
  64. }
  65. PyMem_RawFree(argv_copy);
  66. PyMem_RawFree(argv_copy2);
  67. return res;
  68. }
  69. #endif