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