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.

108 lines
2.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. /* Force malloc() allocator to bootstrap Python */
  23. #ifdef Py_DEBUG
  24. (void)_PyMem_SetupAllocators("malloc_debug");
  25. # else
  26. (void)_PyMem_SetupAllocators("malloc");
  27. # endif
  28. argv_copy = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
  29. argv_copy2 = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
  30. if (!argv_copy || !argv_copy2) {
  31. fprintf(stderr, "out of memory\n");
  32. return 1;
  33. }
  34. /* 754 requires that FP exceptions run in "no stop" mode by default,
  35. * and until C vendors implement C99's ways to control FP exceptions,
  36. * Python requires non-stop mode. Alas, some platforms enable FP
  37. * exceptions by default. Here we disable them.
  38. */
  39. #ifdef __FreeBSD__
  40. fedisableexcept(FE_OVERFLOW);
  41. #endif
  42. oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
  43. if (!oldloc) {
  44. fprintf(stderr, "out of memory\n");
  45. return 1;
  46. }
  47. /* Reconfigure the locale to the default for this process */
  48. _Py_SetLocaleFromEnv(LC_ALL);
  49. /* The legacy C locale assumes ASCII as the default text encoding, which
  50. * causes problems not only for the CPython runtime, but also other
  51. * components like GNU readline.
  52. *
  53. * Accordingly, when the CLI detects it, it attempts to coerce it to a
  54. * more capable UTF-8 based alternative.
  55. *
  56. * See the documentation of the PYTHONCOERCECLOCALE setting for more
  57. * details.
  58. */
  59. if (_Py_LegacyLocaleDetected()) {
  60. _Py_CoerceLegacyLocale();
  61. }
  62. /* Convert from char to wchar_t based on the locale settings */
  63. for (i = 0; i < argc; i++) {
  64. argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
  65. if (!argv_copy[i]) {
  66. PyMem_RawFree(oldloc);
  67. fprintf(stderr, "Fatal Python error: "
  68. "unable to decode the command line argument #%i\n",
  69. i + 1);
  70. return 1;
  71. }
  72. argv_copy2[i] = argv_copy[i];
  73. }
  74. argv_copy2[argc] = argv_copy[argc] = NULL;
  75. setlocale(LC_ALL, oldloc);
  76. PyMem_RawFree(oldloc);
  77. res = Py_Main(argc, argv_copy);
  78. /* Force again malloc() allocator to release memory blocks allocated
  79. before Py_Main() */
  80. #ifdef Py_DEBUG
  81. (void)_PyMem_SetupAllocators("malloc_debug");
  82. # else
  83. (void)_PyMem_SetupAllocators("malloc");
  84. # endif
  85. for (i = 0; i < argc; i++) {
  86. PyMem_RawFree(argv_copy2[i]);
  87. }
  88. PyMem_RawFree(argv_copy);
  89. PyMem_RawFree(argv_copy2);
  90. return res;
  91. }
  92. #endif