blob: 86af2b6f94c528e7b6c1f3960b5784e4f2c19dc7 [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 Stinnerf7e5b562017-11-15 15:48:08 -08005#include "internal/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 Stinnerf7e5b562017-11-15 15:48:08 -080019 _PyInitError err = _PyRuntime_Initialize();
20 if (_Py_INIT_FAILED(err)) {
21 fprintf(stderr, "Fatal Python error: %s\n", err.msg);
22 fflush(stderr);
23 exit(1);
24 }
25
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +020026 const char *p;
Victor Stinnerc588fee2013-07-27 02:39:09 +020027 int i, n, sts = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 int inspect = 0;
29 int unbuffered = 0;
Victor Stinnerc588fee2013-07-27 02:39:09 +020030 char *oldloc = NULL;
31 wchar_t **argv_copy = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 /* We need a second copies, as Python might modify the first one. */
Victor Stinnerc588fee2013-07-27 02:39:09 +020033 wchar_t **argv_copy2 = NULL;
Victor Stinner36577e42013-07-27 01:04:56 +020034
Benjamin Peterson2476b982015-02-14 15:16:32 -050035 if (argc > 0) {
36 argv_copy = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
37 argv_copy2 = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
38 if (!argv_copy || !argv_copy2) {
39 fprintf(stderr, "out of memory\n");
40 goto error;
41 }
Victor Stinner36577e42013-07-27 01:04:56 +020042 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +000043
Victor Stinnerb75d7e22018-08-01 02:13:04 +020044 _PyCoreConfig config = _PyCoreConfig_INIT;
45 config._frozen = 1; /* Suppress errors from getpath.c */
Guido van Rossum919b83d1998-02-06 22:30:29 +000046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
48 inspect = 1;
49 if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
50 unbuffered = 1;
Guido van Rossum9e90a671993-06-24 11:10:19 +000051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 if (unbuffered) {
53 setbuf(stdin, (char *)NULL);
54 setbuf(stdout, (char *)NULL);
55 setbuf(stderr, (char *)NULL);
56 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +000057
Victor Stinnerc588fee2013-07-27 02:39:09 +020058 oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
59 if (!oldloc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 fprintf(stderr, "out of memory\n");
Victor Stinnerc588fee2013-07-27 02:39:09 +020061 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 }
Martin v. Löwis790465f2008-04-05 20:41:37 +000063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 setlocale(LC_ALL, "");
65 for (i = 0; i < argc; i++) {
Victor Stinnerf6a271a2014-08-01 12:28:48 +020066 argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
Victor Stinnerc588fee2013-07-27 02:39:09 +020067 argv_copy2[i] = argv_copy[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 if (!argv_copy[i]) {
Victor Stinner739cf4e2013-07-27 02:24:52 +020069 fprintf(stderr, "Unable to decode the command line argument #%i\n",
70 i + 1);
Victor Stinnerc588fee2013-07-27 02:39:09 +020071 argc = i;
72 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 }
75 setlocale(LC_ALL, oldloc);
Victor Stinnerc588fee2013-07-27 02:39:09 +020076 PyMem_RawFree(oldloc);
77 oldloc = NULL;
Martin v. Löwis790465f2008-04-05 20:41:37 +000078
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000079#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 PyInitFrozenExtensions();
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000081#endif /* MS_WINDOWS */
Benjamin Peterson2476b982015-02-14 15:16:32 -050082 if (argc >= 1)
83 Py_SetProgramName(argv_copy[0]);
Victor Stinnerb75d7e22018-08-01 02:13:04 +020084
85 err = _Py_InitializeFromConfig(&config);
86 /* No need to call _PyCoreConfig_Clear() since we didn't allocate any
87 memory: program_name is a constant string. */
88 if (_Py_INIT_FAILED(err)) {
89 _Py_FatalInitError(err);
90 }
91
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000092#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 PyWinFreeze_ExeInit();
Guido van Rossum6deac7a1998-04-03 21:11:15 +000094#endif
Guido van Rossum3768fb11997-07-19 19:24:41 +000095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 if (Py_VerboseFlag)
97 fprintf(stderr, "Python %s\n%s\n",
98 Py_GetVersion(), Py_GetCopyright());
Guido van Rossum3768fb11997-07-19 19:24:41 +000099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 PySys_SetArgv(argc, argv_copy);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 n = PyImport_ImportFrozenModule("__main__");
103 if (n == 0)
104 Py_FatalError("__main__ not frozen");
105 if (n < 0) {
106 PyErr_Print();
107 sts = 1;
108 }
109 else
110 sts = 0;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 if (inspect && isatty((int)fileno(stdin)))
113 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000114
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000115#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 PyWinFreeze_ExeTerm();
Guido van Rossum6deac7a1998-04-03 21:11:15 +0000117#endif
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000118 if (Py_FinalizeEx() < 0) {
119 sts = 120;
120 }
Victor Stinnerc588fee2013-07-27 02:39:09 +0200121
122error:
Victor Stinnerb5245be2013-07-27 01:13:34 +0200123 PyMem_RawFree(argv_copy);
Victor Stinnerc588fee2013-07-27 02:39:09 +0200124 if (argv_copy2) {
125 for (i = 0; i < argc; i++)
126 PyMem_RawFree(argv_copy2[i]);
127 PyMem_RawFree(argv_copy2);
128 }
129 PyMem_RawFree(oldloc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 return sts;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000131}