frozenmain.c 2.38 KB
Newer Older
1 2 3

/* Python interpreter main program for frozen scripts */

Guido van Rossum's avatar
Guido van Rossum committed
4
#include "Python.h"
5
#include <locale.h>
6

7
#ifdef MS_WINDOWS
8 9 10
extern void PyWinFreeze_ExeInit(void);
extern void PyWinFreeze_ExeTerm(void);
extern int PyInitFrozenExtensions(void);
11 12
#endif

13 14 15
/* Main program */

int
16
Py_FrozenMain(int argc, char **argv)
17 18
{
	char *p;
19
	int i, n, sts;
20
	int inspect = 0;
21
	int unbuffered = 0;
22 23 24 25
	char *oldloc;
	wchar_t **argv_copy = PyMem_Malloc(sizeof(wchar_t*)*argc);
	/* We need a second copies, as Python might modify the first one. */
	wchar_t **argv_copy2 = PyMem_Malloc(sizeof(wchar_t*)*argc);
26

27 28
	Py_FrozenFlag = 1; /* Suppress errors from getpath.c */

29
	if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
30
		inspect = 1;
31
	if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
32 33 34
		unbuffered = 1;

	if (unbuffered) {
35
		setbuf(stdin, (char *)NULL);
36 37 38
		setbuf(stdout, (char *)NULL);
		setbuf(stderr, (char *)NULL);
	}
39

40
	if (!argv_copy) {
41
		fprintf(stderr, "out of memory\n");
42 43 44 45 46 47
		return 1;
	}

	oldloc = setlocale(LC_ALL, NULL);
	setlocale(LC_ALL, "");
	for (i = 0; i < argc; i++) {
48 49 50
#ifdef HAVE_BROKEN_MBSTOWCS
		size_t argsize = strlen(argv[i]);
#else
51
		size_t argsize = mbstowcs(NULL, argv[i], 0);
52 53
#endif
		size_t count;
54
		if (argsize == (size_t)-1) {
55
			fprintf(stderr, "Could not convert argument %d to string\n", i);
56 57 58 59 60
			return 1;
		}
		argv_copy[i] = PyMem_Malloc((argsize+1)*sizeof(wchar_t));
		argv_copy2[i] = argv_copy[i];
		if (!argv_copy[i]) {
61
			fprintf(stderr, "out of memory\n");
62 63
			return 1;
		}
64 65
		count = mbstowcs(argv_copy[i], argv[i], argsize+1);
		if (count == (size_t)-1) {
66
			fprintf(stderr, "Could not convert argument %d to string\n", i);
67 68
			return 1;
		}
69 70 71
	}
	setlocale(LC_ALL, oldloc);

72
#ifdef MS_WINDOWS
73
	PyInitFrozenExtensions();
74
#endif /* MS_WINDOWS */
75
	Py_SetProgramName(argv_copy[0]);
76
	Py_Initialize();
77
#ifdef MS_WINDOWS
78 79
	PyWinFreeze_ExeInit();
#endif
80

Guido van Rossum's avatar
Guido van Rossum committed
81
	if (Py_VerboseFlag)
82
		fprintf(stderr, "Python %s\n%s\n",
83
			Py_GetVersion(), Py_GetCopyright());
84

85
	PySys_SetArgv(argc, argv_copy);
86

Guido van Rossum's avatar
Guido van Rossum committed
87
	n = PyImport_ImportFrozenModule("__main__");
88
	if (n == 0)
Guido van Rossum's avatar
Guido van Rossum committed
89
		Py_FatalError("__main__ not frozen");
90
	if (n < 0) {
Guido van Rossum's avatar
Guido van Rossum committed
91
		PyErr_Print();
92
		sts = 1;
93 94
	}
	else
95 96
		sts = 0;

97
	if (inspect && isatty((int)fileno(stdin)))
Guido van Rossum's avatar
Guido van Rossum committed
98
		sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
99

100
#ifdef MS_WINDOWS
101 102
	PyWinFreeze_ExeTerm();
#endif
Guido van Rossum's avatar
Guido van Rossum committed
103
	Py_Finalize();
104 105 106 107 108
	for (i = 0; i < argc; i++) {
		PyMem_Free(argv_copy2[i]);
	}
	PyMem_Free(argv_copy);
	PyMem_Free(argv_copy2);
Guido van Rossum's avatar
Guido van Rossum committed
109
	return sts;
110
}