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" |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 5 | #include "pycore_pystate.h" |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 6 | #include <locale.h> |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 7 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 8 | #ifdef MS_WINDOWS |
Thomas Wouters | b4bd21c | 2000-07-22 23:38:01 +0000 | [diff] [blame] | 9 | extern void PyWinFreeze_ExeInit(void); |
| 10 | extern void PyWinFreeze_ExeTerm(void); |
| 11 | extern int PyInitFrozenExtensions(void); |
Guido van Rossum | 6deac7a | 1998-04-03 21:11:15 +0000 | [diff] [blame] | 12 | #endif |
| 13 | |
Guido van Rossum | 47ad5e7 | 1995-08-04 04:10:43 +0000 | [diff] [blame] | 14 | /* Main program */ |
| 15 | |
| 16 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 17 | Py_FrozenMain(int argc, char **argv) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 18 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 19 | PyStatus status = _PyRuntime_Initialize(); |
| 20 | if (PyStatus_Exception(status)) { |
| 21 | Py_ExitStatusException(status); |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 22 | } |
| 23 | |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 24 | const char *p; |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 25 | int i, n, sts = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 26 | int inspect = 0; |
| 27 | int unbuffered = 0; |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 28 | char *oldloc = NULL; |
| 29 | wchar_t **argv_copy = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 30 | /* We need a second copies, as Python might modify the first one. */ |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 31 | wchar_t **argv_copy2 = NULL; |
Victor Stinner | 36577e4 | 2013-07-27 01:04:56 +0200 | [diff] [blame] | 32 | |
Benjamin Peterson | 2476b98 | 2015-02-14 15:16:32 -0500 | [diff] [blame] | 33 | if (argc > 0) { |
| 34 | argv_copy = PyMem_RawMalloc(sizeof(wchar_t*) * argc); |
| 35 | argv_copy2 = PyMem_RawMalloc(sizeof(wchar_t*) * argc); |
| 36 | if (!argv_copy || !argv_copy2) { |
| 37 | fprintf(stderr, "out of memory\n"); |
| 38 | goto error; |
| 39 | } |
Victor Stinner | 36577e4 | 2013-07-27 01:04:56 +0200 | [diff] [blame] | 40 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 41 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 42 | PyConfig config; |
Victor Stinner | 8462a49 | 2019-10-01 12:06:16 +0200 | [diff] [blame] | 43 | PyConfig_InitPythonConfig(&config); |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 44 | config.pathconfig_warnings = 0; /* Suppress errors from getpath.c */ |
Guido van Rossum | 919b83d | 1998-02-06 22:30:29 +0000 | [diff] [blame] | 45 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') |
| 47 | inspect = 1; |
| 48 | if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0') |
| 49 | unbuffered = 1; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 50 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | if (unbuffered) { |
| 52 | setbuf(stdin, (char *)NULL); |
| 53 | setbuf(stdout, (char *)NULL); |
| 54 | setbuf(stderr, (char *)NULL); |
| 55 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 56 | |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 57 | oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL)); |
| 58 | if (!oldloc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 59 | fprintf(stderr, "out of memory\n"); |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 60 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | } |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 62 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | setlocale(LC_ALL, ""); |
| 64 | for (i = 0; i < argc; i++) { |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 65 | argv_copy[i] = Py_DecodeLocale(argv[i], NULL); |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 66 | argv_copy2[i] = argv_copy[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | if (!argv_copy[i]) { |
Victor Stinner | 739cf4e | 2013-07-27 02:24:52 +0200 | [diff] [blame] | 68 | fprintf(stderr, "Unable to decode the command line argument #%i\n", |
| 69 | i + 1); |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 70 | argc = i; |
| 71 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | } |
| 74 | setlocale(LC_ALL, oldloc); |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 75 | PyMem_RawFree(oldloc); |
| 76 | oldloc = NULL; |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 77 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 78 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | PyInitFrozenExtensions(); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 80 | #endif /* MS_WINDOWS */ |
Benjamin Peterson | 2476b98 | 2015-02-14 15:16:32 -0500 | [diff] [blame] | 81 | if (argc >= 1) |
| 82 | Py_SetProgramName(argv_copy[0]); |
Victor Stinner | b75d7e2 | 2018-08-01 02:13:04 +0200 | [diff] [blame] | 83 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 84 | status = Py_InitializeFromConfig(&config); |
| 85 | PyConfig_Clear(&config); |
| 86 | if (PyStatus_Exception(status)) { |
| 87 | Py_ExitStatusException(status); |
Victor Stinner | b75d7e2 | 2018-08-01 02:13:04 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 90 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | PyWinFreeze_ExeInit(); |
Guido van Rossum | 6deac7a | 1998-04-03 21:11:15 +0000 | [diff] [blame] | 92 | #endif |
Guido van Rossum | 3768fb1 | 1997-07-19 19:24:41 +0000 | [diff] [blame] | 93 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | if (Py_VerboseFlag) |
| 95 | fprintf(stderr, "Python %s\n%s\n", |
| 96 | Py_GetVersion(), Py_GetCopyright()); |
Guido van Rossum | 3768fb1 | 1997-07-19 19:24:41 +0000 | [diff] [blame] | 97 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | PySys_SetArgv(argc, argv_copy); |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 99 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | n = PyImport_ImportFrozenModule("__main__"); |
| 101 | if (n == 0) |
| 102 | Py_FatalError("__main__ not frozen"); |
| 103 | if (n < 0) { |
| 104 | PyErr_Print(); |
| 105 | sts = 1; |
| 106 | } |
| 107 | else |
| 108 | sts = 0; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 110 | if (inspect && isatty((int)fileno(stdin))) |
| 111 | sts = PyRun_AnyFile(stdin, "<stdin>") != 0; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 112 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 113 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | PyWinFreeze_ExeTerm(); |
Guido van Rossum | 6deac7a | 1998-04-03 21:11:15 +0000 | [diff] [blame] | 115 | #endif |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 116 | if (Py_FinalizeEx() < 0) { |
| 117 | sts = 120; |
| 118 | } |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 119 | |
| 120 | error: |
Victor Stinner | b5245be | 2013-07-27 01:13:34 +0200 | [diff] [blame] | 121 | PyMem_RawFree(argv_copy); |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 122 | if (argv_copy2) { |
| 123 | for (i = 0; i < argc; i++) |
| 124 | PyMem_RawFree(argv_copy2[i]); |
| 125 | PyMem_RawFree(argv_copy2); |
| 126 | } |
| 127 | PyMem_RawFree(oldloc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | return sts; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 129 | } |