blob: c56938ab489948f49c5e3c1a11e8393599ac0f25 [file] [log] [blame]
Guido van Rossumf56e3db1993-04-01 20:59:32 +00001
2/* Python interpreter main program for frozen scripts */
3
Guido van Rossum4e2e0f91995-03-31 10:27:23 +00004#include "Python.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01005#include "pycore_pystate.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +00006#include <locale.h>
Guido van Rossumf56e3db1993-04-01 20:59:32 +00007
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008#ifdef MS_WINDOWS
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00009extern void PyWinFreeze_ExeInit(void);
10extern void PyWinFreeze_ExeTerm(void);
11extern int PyInitFrozenExtensions(void);
Guido van Rossum6deac7a1998-04-03 21:11:15 +000012#endif
13
Guido van Rossum47ad5e71995-08-04 04:10:43 +000014/* Main program */
15
16int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000017Py_FrozenMain(int argc, char **argv)
Guido van Rossumf56e3db1993-04-01 20:59:32 +000018{
Victor Stinner331a6a52019-05-27 16:39:22 +020019 PyStatus status = _PyRuntime_Initialize();
20 if (PyStatus_Exception(status)) {
21 Py_ExitStatusException(status);
Victor Stinnerf7e5b562017-11-15 15:48:08 -080022 }
23
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +020024 const char *p;
Victor Stinnerc588fee2013-07-27 02:39:09 +020025 int i, n, sts = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 int inspect = 0;
27 int unbuffered = 0;
Victor Stinnerc588fee2013-07-27 02:39:09 +020028 char *oldloc = NULL;
29 wchar_t **argv_copy = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 /* We need a second copies, as Python might modify the first one. */
Victor Stinnerc588fee2013-07-27 02:39:09 +020031 wchar_t **argv_copy2 = NULL;
Victor Stinner36577e42013-07-27 01:04:56 +020032
Benjamin Peterson2476b982015-02-14 15:16:32 -050033 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 Stinner36577e42013-07-27 01:04:56 +020040 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +000041
Victor Stinner331a6a52019-05-27 16:39:22 +020042 PyConfig config;
43 status = PyConfig_InitPythonConfig(&config);
44 if (PyStatus_Exception(status)) {
45 PyConfig_Clear(&config);
46 Py_ExitStatusException(status);
Victor Stinner022be022019-05-22 23:58:50 +020047 }
Victor Stinner9ef5dca2019-05-16 17:38:16 +020048 config.pathconfig_warnings = 0; /* Suppress errors from getpath.c */
Guido van Rossum919b83d1998-02-06 22:30:29 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
51 inspect = 1;
52 if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
53 unbuffered = 1;
Guido van Rossum9e90a671993-06-24 11:10:19 +000054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 if (unbuffered) {
56 setbuf(stdin, (char *)NULL);
57 setbuf(stdout, (char *)NULL);
58 setbuf(stderr, (char *)NULL);
59 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +000060
Victor Stinnerc588fee2013-07-27 02:39:09 +020061 oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
62 if (!oldloc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 fprintf(stderr, "out of memory\n");
Victor Stinnerc588fee2013-07-27 02:39:09 +020064 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 }
Martin v. Löwis790465f2008-04-05 20:41:37 +000066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 setlocale(LC_ALL, "");
68 for (i = 0; i < argc; i++) {
Victor Stinnerf6a271a2014-08-01 12:28:48 +020069 argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
Victor Stinnerc588fee2013-07-27 02:39:09 +020070 argv_copy2[i] = argv_copy[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (!argv_copy[i]) {
Victor Stinner739cf4e2013-07-27 02:24:52 +020072 fprintf(stderr, "Unable to decode the command line argument #%i\n",
73 i + 1);
Victor Stinnerc588fee2013-07-27 02:39:09 +020074 argc = i;
75 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 }
78 setlocale(LC_ALL, oldloc);
Victor Stinnerc588fee2013-07-27 02:39:09 +020079 PyMem_RawFree(oldloc);
80 oldloc = NULL;
Martin v. Löwis790465f2008-04-05 20:41:37 +000081
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000082#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 PyInitFrozenExtensions();
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000084#endif /* MS_WINDOWS */
Benjamin Peterson2476b982015-02-14 15:16:32 -050085 if (argc >= 1)
86 Py_SetProgramName(argv_copy[0]);
Victor Stinnerb75d7e22018-08-01 02:13:04 +020087
Victor Stinner331a6a52019-05-27 16:39:22 +020088 status = Py_InitializeFromConfig(&config);
89 PyConfig_Clear(&config);
90 if (PyStatus_Exception(status)) {
91 Py_ExitStatusException(status);
Victor Stinnerb75d7e22018-08-01 02:13:04 +020092 }
93
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000094#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 PyWinFreeze_ExeInit();
Guido van Rossum6deac7a1998-04-03 21:11:15 +000096#endif
Guido van Rossum3768fb11997-07-19 19:24:41 +000097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 if (Py_VerboseFlag)
99 fprintf(stderr, "Python %s\n%s\n",
100 Py_GetVersion(), Py_GetCopyright());
Guido van Rossum3768fb11997-07-19 19:24:41 +0000101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 PySys_SetArgv(argc, argv_copy);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 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 Rossum9e90a671993-06-24 11:10:19 +0000113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 if (inspect && isatty((int)fileno(stdin)))
115 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000116
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000117#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 PyWinFreeze_ExeTerm();
Guido van Rossum6deac7a1998-04-03 21:11:15 +0000119#endif
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000120 if (Py_FinalizeEx() < 0) {
121 sts = 120;
122 }
Victor Stinnerc588fee2013-07-27 02:39:09 +0200123
124error:
Victor Stinnerb5245be2013-07-27 01:13:34 +0200125 PyMem_RawFree(argv_copy);
Victor Stinnerc588fee2013-07-27 02:39:09 +0200126 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 Pitrouf95a1b32010-05-09 15:52:27 +0000132 return sts;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000133}