blob: 769b33d0ee2f799c2dfc27ff38bc36daa6618cfc [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"
Martin v. Löwis790465f2008-04-05 20:41:37 +00005#include <locale.h>
Guido van Rossumf56e3db1993-04-01 20:59:32 +00006
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007#ifdef MS_WINDOWS
Thomas Woutersb4bd21c2000-07-22 23:38:01 +00008extern void PyWinFreeze_ExeInit(void);
9extern void PyWinFreeze_ExeTerm(void);
10extern int PyInitFrozenExtensions(void);
Guido van Rossum6deac7a1998-04-03 21:11:15 +000011#endif
12
Guido van Rossum47ad5e71995-08-04 04:10:43 +000013/* Main program */
14
15int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000016Py_FrozenMain(int argc, char **argv)
Guido van Rossumf56e3db1993-04-01 20:59:32 +000017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018 char *p;
Victor Stinnerc588fee2013-07-27 02:39:09 +020019 int i, n, sts = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 int inspect = 0;
21 int unbuffered = 0;
Victor Stinnerc588fee2013-07-27 02:39:09 +020022 char *oldloc = NULL;
23 wchar_t **argv_copy = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 /* We need a second copies, as Python might modify the first one. */
Victor Stinnerc588fee2013-07-27 02:39:09 +020025 wchar_t **argv_copy2 = NULL;
Victor Stinner36577e42013-07-27 01:04:56 +020026
Benjamin Peterson2476b982015-02-14 15:16:32 -050027 if (argc > 0) {
28 argv_copy = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
29 argv_copy2 = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
30 if (!argv_copy || !argv_copy2) {
31 fprintf(stderr, "out of memory\n");
32 goto error;
33 }
Victor Stinner36577e42013-07-27 01:04:56 +020034 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +000035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 Py_FrozenFlag = 1; /* Suppress errors from getpath.c */
Guido van Rossum919b83d1998-02-06 22:30:29 +000037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
39 inspect = 1;
40 if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
41 unbuffered = 1;
Guido van Rossum9e90a671993-06-24 11:10:19 +000042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 if (unbuffered) {
44 setbuf(stdin, (char *)NULL);
45 setbuf(stdout, (char *)NULL);
46 setbuf(stderr, (char *)NULL);
47 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +000048
Victor Stinnerc588fee2013-07-27 02:39:09 +020049 oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
50 if (!oldloc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 fprintf(stderr, "out of memory\n");
Victor Stinnerc588fee2013-07-27 02:39:09 +020052 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 }
Martin v. Löwis790465f2008-04-05 20:41:37 +000054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 setlocale(LC_ALL, "");
56 for (i = 0; i < argc; i++) {
Victor Stinnerf6a271a2014-08-01 12:28:48 +020057 argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
Victor Stinnerc588fee2013-07-27 02:39:09 +020058 argv_copy2[i] = argv_copy[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 if (!argv_copy[i]) {
Victor Stinner739cf4e2013-07-27 02:24:52 +020060 fprintf(stderr, "Unable to decode the command line argument #%i\n",
61 i + 1);
Victor Stinnerc588fee2013-07-27 02:39:09 +020062 argc = i;
63 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 }
66 setlocale(LC_ALL, oldloc);
Victor Stinnerc588fee2013-07-27 02:39:09 +020067 PyMem_RawFree(oldloc);
68 oldloc = NULL;
Martin v. Löwis790465f2008-04-05 20:41:37 +000069
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000070#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 PyInitFrozenExtensions();
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000072#endif /* MS_WINDOWS */
Benjamin Peterson2476b982015-02-14 15:16:32 -050073 if (argc >= 1)
74 Py_SetProgramName(argv_copy[0]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 Py_Initialize();
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000076#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 PyWinFreeze_ExeInit();
Guido van Rossum6deac7a1998-04-03 21:11:15 +000078#endif
Guido van Rossum3768fb11997-07-19 19:24:41 +000079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 if (Py_VerboseFlag)
81 fprintf(stderr, "Python %s\n%s\n",
82 Py_GetVersion(), Py_GetCopyright());
Guido van Rossum3768fb11997-07-19 19:24:41 +000083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 PySys_SetArgv(argc, argv_copy);
Guido van Rossum9e90a671993-06-24 11:10:19 +000085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 n = PyImport_ImportFrozenModule("__main__");
87 if (n == 0)
88 Py_FatalError("__main__ not frozen");
89 if (n < 0) {
90 PyErr_Print();
91 sts = 1;
92 }
93 else
94 sts = 0;
Guido van Rossum9e90a671993-06-24 11:10:19 +000095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 if (inspect && isatty((int)fileno(stdin)))
97 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
Guido van Rossum9e90a671993-06-24 11:10:19 +000098
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000099#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 PyWinFreeze_ExeTerm();
Guido van Rossum6deac7a1998-04-03 21:11:15 +0000101#endif
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000102 if (Py_FinalizeEx() < 0) {
103 sts = 120;
104 }
Victor Stinnerc588fee2013-07-27 02:39:09 +0200105
106error:
Victor Stinnerb5245be2013-07-27 01:13:34 +0200107 PyMem_RawFree(argv_copy);
Victor Stinnerc588fee2013-07-27 02:39:09 +0200108 if (argv_copy2) {
109 for (i = 0; i < argc; i++)
110 PyMem_RawFree(argv_copy2[i]);
111 PyMem_RawFree(argv_copy2);
112 }
113 PyMem_RawFree(oldloc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 return sts;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000115}