Home | History | Annotate | Download | only in Programs
      1 /* Minimal main program -- everything is loaded from the library */
      2 
      3 #include "Python.h"
      4 #include <locale.h>
      5 
      6 #ifdef __FreeBSD__
      7 #include <fenv.h>
      8 #endif
      9 
     10 #ifdef MS_WINDOWS
     11 int
     12 wmain(int argc, wchar_t **argv)
     13 {
     14     return Py_Main(argc, argv);
     15 }
     16 #else
     17 
     18 int
     19 main(int argc, char **argv)
     20 {
     21     wchar_t **argv_copy;
     22     /* We need a second copy, as Python might modify the first one. */
     23     wchar_t **argv_copy2;
     24     int i, res;
     25     char *oldloc;
     26 
     27     /* Force malloc() allocator to bootstrap Python */
     28     (void)_PyMem_SetupAllocators("malloc");
     29 
     30     argv_copy = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
     31     argv_copy2 = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
     32     if (!argv_copy || !argv_copy2) {
     33         fprintf(stderr, "out of memory\n");
     34         return 1;
     35     }
     36 
     37     /* 754 requires that FP exceptions run in "no stop" mode by default,
     38      * and until C vendors implement C99's ways to control FP exceptions,
     39      * Python requires non-stop mode.  Alas, some platforms enable FP
     40      * exceptions by default.  Here we disable them.
     41      */
     42 #ifdef __FreeBSD__
     43     fedisableexcept(FE_OVERFLOW);
     44 #endif
     45 
     46     oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
     47     if (!oldloc) {
     48         fprintf(stderr, "out of memory\n");
     49         return 1;
     50     }
     51 
     52     setlocale(LC_ALL, "");
     53     for (i = 0; i < argc; i++) {
     54         argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
     55         if (!argv_copy[i]) {
     56             PyMem_RawFree(oldloc);
     57             fprintf(stderr, "Fatal Python error: "
     58                             "unable to decode the command line argument #%i\n",
     59                             i + 1);
     60             return 1;
     61         }
     62         argv_copy2[i] = argv_copy[i];
     63     }
     64     argv_copy2[argc] = argv_copy[argc] = NULL;
     65 
     66     setlocale(LC_ALL, oldloc);
     67     PyMem_RawFree(oldloc);
     68 
     69     res = Py_Main(argc, argv_copy);
     70 
     71     /* Force again malloc() allocator to release memory blocks allocated
     72        before Py_Main() */
     73     (void)_PyMem_SetupAllocators("malloc");
     74 
     75     for (i = 0; i < argc; i++) {
     76         PyMem_RawFree(argv_copy2[i]);
     77     }
     78     PyMem_RawFree(argv_copy);
     79     PyMem_RawFree(argv_copy2);
     80     return res;
     81 }
     82 #endif
     83