Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1 | |
| 2 | /* Python interpreter main program for frozen scripts */ |
| 3 | |
Guido van Rossum | 4e2e0f9 | 1995-03-31 10:27:23 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 5 | #include <locale.h> |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 6 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | #ifdef MS_WINDOWS |
Thomas Wouters | b4bd21c | 2000-07-22 23:38:01 +0000 | [diff] [blame] | 8 | extern void PyWinFreeze_ExeInit(void); |
| 9 | extern void PyWinFreeze_ExeTerm(void); |
| 10 | extern int PyInitFrozenExtensions(void); |
Guido van Rossum | 6deac7a | 1998-04-03 21:11:15 +0000 | [diff] [blame] | 11 | #endif |
| 12 | |
Guido van Rossum | 47ad5e7 | 1995-08-04 04:10:43 +0000 | [diff] [blame] | 13 | /* Main program */ |
| 14 | |
| 15 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 16 | Py_FrozenMain(int argc, char **argv) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 17 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | char *p; |
| 19 | int i, n, sts; |
| 20 | int inspect = 0; |
| 21 | int unbuffered = 0; |
| 22 | char *oldloc; |
| 23 | wchar_t **argv_copy = PyMem_Malloc(sizeof(wchar_t*)*argc); |
| 24 | /* We need a second copies, as Python might modify the first one. */ |
| 25 | wchar_t **argv_copy2 = PyMem_Malloc(sizeof(wchar_t*)*argc); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 26 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 | Py_FrozenFlag = 1; /* Suppress errors from getpath.c */ |
Guido van Rossum | 919b83d | 1998-02-06 22:30:29 +0000 | [diff] [blame] | 28 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') |
| 30 | inspect = 1; |
| 31 | if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0') |
| 32 | unbuffered = 1; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 33 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 34 | if (unbuffered) { |
| 35 | setbuf(stdin, (char *)NULL); |
| 36 | setbuf(stdout, (char *)NULL); |
| 37 | setbuf(stderr, (char *)NULL); |
| 38 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 39 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 40 | if (!argv_copy) { |
| 41 | fprintf(stderr, "out of memory\n"); |
| 42 | return 1; |
| 43 | } |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 44 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | oldloc = setlocale(LC_ALL, NULL); |
| 46 | setlocale(LC_ALL, ""); |
| 47 | for (i = 0; i < argc; i++) { |
Antoine Pitrou | fff9530 | 2008-09-03 18:58:51 +0000 | [diff] [blame] | 48 | #ifdef HAVE_BROKEN_MBSTOWCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | size_t argsize = strlen(argv[i]); |
Antoine Pitrou | fff9530 | 2008-09-03 18:58:51 +0000 | [diff] [blame] | 50 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | size_t argsize = mbstowcs(NULL, argv[i], 0); |
Antoine Pitrou | fff9530 | 2008-09-03 18:58:51 +0000 | [diff] [blame] | 52 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 53 | size_t count; |
| 54 | if (argsize == (size_t)-1) { |
| 55 | fprintf(stderr, "Could not convert argument %d to string\n", i); |
| 56 | return 1; |
| 57 | } |
| 58 | argv_copy[i] = PyMem_Malloc((argsize+1)*sizeof(wchar_t)); |
| 59 | argv_copy2[i] = argv_copy[i]; |
| 60 | if (!argv_copy[i]) { |
| 61 | fprintf(stderr, "out of memory\n"); |
| 62 | return 1; |
| 63 | } |
| 64 | count = mbstowcs(argv_copy[i], argv[i], argsize+1); |
| 65 | if (count == (size_t)-1) { |
| 66 | fprintf(stderr, "Could not convert argument %d to string\n", i); |
| 67 | return 1; |
| 68 | } |
| 69 | } |
| 70 | setlocale(LC_ALL, oldloc); |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 71 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 72 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | PyInitFrozenExtensions(); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 74 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | Py_SetProgramName(argv_copy[0]); |
| 76 | Py_Initialize(); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 77 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | PyWinFreeze_ExeInit(); |
Guido van Rossum | 6deac7a | 1998-04-03 21:11:15 +0000 | [diff] [blame] | 79 | #endif |
Guido van Rossum | 3768fb1 | 1997-07-19 19:24:41 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | if (Py_VerboseFlag) |
| 82 | fprintf(stderr, "Python %s\n%s\n", |
| 83 | Py_GetVersion(), Py_GetCopyright()); |
Guido van Rossum | 3768fb1 | 1997-07-19 19:24:41 +0000 | [diff] [blame] | 84 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | PySys_SetArgv(argc, argv_copy); |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 86 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | n = PyImport_ImportFrozenModule("__main__"); |
| 88 | if (n == 0) |
| 89 | Py_FatalError("__main__ not frozen"); |
| 90 | if (n < 0) { |
| 91 | PyErr_Print(); |
| 92 | sts = 1; |
| 93 | } |
| 94 | else |
| 95 | sts = 0; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 96 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | if (inspect && isatty((int)fileno(stdin))) |
| 98 | sts = PyRun_AnyFile(stdin, "<stdin>") != 0; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 99 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 100 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | PyWinFreeze_ExeTerm(); |
Guido van Rossum | 6deac7a | 1998-04-03 21:11:15 +0000 | [diff] [blame] | 102 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | Py_Finalize(); |
| 104 | for (i = 0; i < argc; i++) { |
| 105 | PyMem_Free(argv_copy2[i]); |
| 106 | } |
| 107 | PyMem_Free(argv_copy); |
| 108 | PyMem_Free(argv_copy2); |
| 109 | return sts; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 110 | } |