blob: f08caf23b901035337542ec12a857b33aaf9b7a6 [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;
19 int i, n, sts;
20 int inspect = 0;
21 int unbuffered = 0;
22 char *oldloc;
23 wchar_t **argv_copy = PyMem_Malloc(sizeof(wchar_t*)*argc);
24 /* We need a second copies, as Python might modify the first one. */
25 wchar_t **argv_copy2 = PyMem_Malloc(sizeof(wchar_t*)*argc);
Guido van Rossum1d5735e1994-08-30 08:27:36 +000026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 Py_FrozenFlag = 1; /* Suppress errors from getpath.c */
Guido van Rossum919b83d1998-02-06 22:30:29 +000028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
30 inspect = 1;
31 if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
32 unbuffered = 1;
Guido van Rossum9e90a671993-06-24 11:10:19 +000033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 if (unbuffered) {
35 setbuf(stdin, (char *)NULL);
36 setbuf(stdout, (char *)NULL);
37 setbuf(stderr, (char *)NULL);
38 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +000039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 if (!argv_copy) {
41 fprintf(stderr, "out of memory\n");
42 return 1;
43 }
Martin v. Löwis790465f2008-04-05 20:41:37 +000044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 oldloc = setlocale(LC_ALL, NULL);
46 setlocale(LC_ALL, "");
47 for (i = 0; i < argc; i++) {
Antoine Pitroufff95302008-09-03 18:58:51 +000048#ifdef HAVE_BROKEN_MBSTOWCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 size_t argsize = strlen(argv[i]);
Antoine Pitroufff95302008-09-03 18:58:51 +000050#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 size_t argsize = mbstowcs(NULL, argv[i], 0);
Antoine Pitroufff95302008-09-03 18:58:51 +000052#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 size_t count;
54 if (argsize == (size_t)-1) {
55 fprintf(stderr, "Could not convert argument %d to string\n", i);
56 return 1;
57 }
58 argv_copy[i] = PyMem_Malloc((argsize+1)*sizeof(wchar_t));
59 argv_copy2[i] = argv_copy[i];
60 if (!argv_copy[i]) {
61 fprintf(stderr, "out of memory\n");
62 return 1;
63 }
64 count = mbstowcs(argv_copy[i], argv[i], argsize+1);
65 if (count == (size_t)-1) {
66 fprintf(stderr, "Could not convert argument %d to string\n", i);
67 return 1;
68 }
69 }
70 setlocale(LC_ALL, oldloc);
Martin v. Löwis790465f2008-04-05 20:41:37 +000071
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000072#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 PyInitFrozenExtensions();
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000074#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 Py_SetProgramName(argv_copy[0]);
76 Py_Initialize();
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000077#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 PyWinFreeze_ExeInit();
Guido van Rossum6deac7a1998-04-03 21:11:15 +000079#endif
Guido van Rossum3768fb11997-07-19 19:24:41 +000080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 if (Py_VerboseFlag)
82 fprintf(stderr, "Python %s\n%s\n",
83 Py_GetVersion(), Py_GetCopyright());
Guido van Rossum3768fb11997-07-19 19:24:41 +000084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 PySys_SetArgv(argc, argv_copy);
Guido van Rossum9e90a671993-06-24 11:10:19 +000086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 n = PyImport_ImportFrozenModule("__main__");
88 if (n == 0)
89 Py_FatalError("__main__ not frozen");
90 if (n < 0) {
91 PyErr_Print();
92 sts = 1;
93 }
94 else
95 sts = 0;
Guido van Rossum9e90a671993-06-24 11:10:19 +000096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 if (inspect && isatty((int)fileno(stdin)))
98 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
Guido van Rossum9e90a671993-06-24 11:10:19 +000099
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000100#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 PyWinFreeze_ExeTerm();
Guido van Rossum6deac7a1998-04-03 21:11:15 +0000102#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 Py_Finalize();
104 for (i = 0; i < argc; i++) {
105 PyMem_Free(argv_copy2[i]);
106 }
107 PyMem_Free(argv_copy);
108 PyMem_Free(argv_copy2);
109 return sts;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000110}