1993-04-02 04:59:32 +08:00
|
|
|
|
|
|
|
/* Python interpreter main program for frozen scripts */
|
|
|
|
|
1995-03-31 18:27:23 +08:00
|
|
|
#include "Python.h"
|
2017-11-16 07:48:08 +08:00
|
|
|
#include "internal/pystate.h"
|
2008-04-06 04:41:37 +08:00
|
|
|
#include <locale.h>
|
1993-04-02 04:59:32 +08:00
|
|
|
|
2002-06-30 23:26:10 +08:00
|
|
|
#ifdef MS_WINDOWS
|
2000-07-23 07:38:01 +08:00
|
|
|
extern void PyWinFreeze_ExeInit(void);
|
|
|
|
extern void PyWinFreeze_ExeTerm(void);
|
|
|
|
extern int PyInitFrozenExtensions(void);
|
1998-04-04 05:11:15 +08:00
|
|
|
#endif
|
|
|
|
|
1995-08-04 12:10:43 +08:00
|
|
|
/* Main program */
|
|
|
|
|
|
|
|
int
|
2000-07-23 02:47:25 +08:00
|
|
|
Py_FrozenMain(int argc, char **argv)
|
1993-04-02 04:59:32 +08:00
|
|
|
{
|
2017-11-16 07:48:08 +08:00
|
|
|
_PyInitError err = _PyRuntime_Initialize();
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
fprintf(stderr, "Fatal Python error: %s\n", err.msg);
|
|
|
|
fflush(stderr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-12-12 19:55:04 +08:00
|
|
|
const char *p;
|
2013-07-27 08:39:09 +08:00
|
|
|
int i, n, sts = 1;
|
2010-05-09 23:52:27 +08:00
|
|
|
int inspect = 0;
|
|
|
|
int unbuffered = 0;
|
2013-07-27 08:39:09 +08:00
|
|
|
char *oldloc = NULL;
|
|
|
|
wchar_t **argv_copy = NULL;
|
2010-05-09 23:52:27 +08:00
|
|
|
/* We need a second copies, as Python might modify the first one. */
|
2013-07-27 08:39:09 +08:00
|
|
|
wchar_t **argv_copy2 = NULL;
|
2013-07-27 07:04:56 +08:00
|
|
|
|
2015-02-15 04:16:32 +08:00
|
|
|
if (argc > 0) {
|
|
|
|
argv_copy = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
|
|
|
|
argv_copy2 = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
|
|
|
|
if (!argv_copy || !argv_copy2) {
|
|
|
|
fprintf(stderr, "out of memory\n");
|
|
|
|
goto error;
|
|
|
|
}
|
2013-07-27 07:04:56 +08:00
|
|
|
}
|
2010-05-09 23:52:27 +08:00
|
|
|
|
2018-08-01 08:13:04 +08:00
|
|
|
_PyCoreConfig config = _PyCoreConfig_INIT;
|
|
|
|
config._frozen = 1; /* Suppress errors from getpath.c */
|
2010-05-09 23:52:27 +08:00
|
|
|
|
|
|
|
if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
|
|
|
|
inspect = 1;
|
|
|
|
if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
|
|
|
|
unbuffered = 1;
|
|
|
|
|
|
|
|
if (unbuffered) {
|
|
|
|
setbuf(stdin, (char *)NULL);
|
|
|
|
setbuf(stdout, (char *)NULL);
|
|
|
|
setbuf(stderr, (char *)NULL);
|
|
|
|
}
|
|
|
|
|
2013-07-27 08:39:09 +08:00
|
|
|
oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
|
|
|
|
if (!oldloc) {
|
2010-05-09 23:52:27 +08:00
|
|
|
fprintf(stderr, "out of memory\n");
|
2013-07-27 08:39:09 +08:00
|
|
|
goto error;
|
2010-05-09 23:52:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
for (i = 0; i < argc; i++) {
|
2014-08-01 18:28:48 +08:00
|
|
|
argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
|
2013-07-27 08:39:09 +08:00
|
|
|
argv_copy2[i] = argv_copy[i];
|
2010-05-09 23:52:27 +08:00
|
|
|
if (!argv_copy[i]) {
|
2013-07-27 08:24:52 +08:00
|
|
|
fprintf(stderr, "Unable to decode the command line argument #%i\n",
|
|
|
|
i + 1);
|
2013-07-27 08:39:09 +08:00
|
|
|
argc = i;
|
|
|
|
goto error;
|
2010-05-09 23:52:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
setlocale(LC_ALL, oldloc);
|
2013-07-27 08:39:09 +08:00
|
|
|
PyMem_RawFree(oldloc);
|
|
|
|
oldloc = NULL;
|
2008-04-06 04:41:37 +08:00
|
|
|
|
2002-06-30 23:26:10 +08:00
|
|
|
#ifdef MS_WINDOWS
|
2010-05-09 23:52:27 +08:00
|
|
|
PyInitFrozenExtensions();
|
2002-06-30 23:26:10 +08:00
|
|
|
#endif /* MS_WINDOWS */
|
2015-02-15 04:16:32 +08:00
|
|
|
if (argc >= 1)
|
|
|
|
Py_SetProgramName(argv_copy[0]);
|
2018-08-01 08:13:04 +08:00
|
|
|
|
|
|
|
err = _Py_InitializeFromConfig(&config);
|
|
|
|
/* No need to call _PyCoreConfig_Clear() since we didn't allocate any
|
|
|
|
memory: program_name is a constant string. */
|
|
|
|
if (_Py_INIT_FAILED(err)) {
|
|
|
|
_Py_FatalInitError(err);
|
|
|
|
}
|
|
|
|
|
2002-06-30 23:26:10 +08:00
|
|
|
#ifdef MS_WINDOWS
|
2010-05-09 23:52:27 +08:00
|
|
|
PyWinFreeze_ExeInit();
|
1998-04-04 05:11:15 +08:00
|
|
|
#endif
|
1997-07-20 03:24:41 +08:00
|
|
|
|
2010-05-09 23:52:27 +08:00
|
|
|
if (Py_VerboseFlag)
|
|
|
|
fprintf(stderr, "Python %s\n%s\n",
|
|
|
|
Py_GetVersion(), Py_GetCopyright());
|
1997-07-20 03:24:41 +08:00
|
|
|
|
2010-05-09 23:52:27 +08:00
|
|
|
PySys_SetArgv(argc, argv_copy);
|
1993-06-24 19:10:19 +08:00
|
|
|
|
2010-05-09 23:52:27 +08:00
|
|
|
n = PyImport_ImportFrozenModule("__main__");
|
|
|
|
if (n == 0)
|
|
|
|
Py_FatalError("__main__ not frozen");
|
|
|
|
if (n < 0) {
|
|
|
|
PyErr_Print();
|
|
|
|
sts = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sts = 0;
|
1993-06-24 19:10:19 +08:00
|
|
|
|
2010-05-09 23:52:27 +08:00
|
|
|
if (inspect && isatty((int)fileno(stdin)))
|
|
|
|
sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
|
1993-06-24 19:10:19 +08:00
|
|
|
|
2002-06-30 23:26:10 +08:00
|
|
|
#ifdef MS_WINDOWS
|
2010-05-09 23:52:27 +08:00
|
|
|
PyWinFreeze_ExeTerm();
|
1998-04-04 05:11:15 +08:00
|
|
|
#endif
|
2015-11-30 11:18:29 +08:00
|
|
|
if (Py_FinalizeEx() < 0) {
|
|
|
|
sts = 120;
|
|
|
|
}
|
2013-07-27 08:39:09 +08:00
|
|
|
|
|
|
|
error:
|
2013-07-27 07:13:34 +08:00
|
|
|
PyMem_RawFree(argv_copy);
|
2013-07-27 08:39:09 +08:00
|
|
|
if (argv_copy2) {
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
PyMem_RawFree(argv_copy2[i]);
|
|
|
|
PyMem_RawFree(argv_copy2);
|
|
|
|
}
|
|
|
|
PyMem_RawFree(oldloc);
|
2010-05-09 23:52:27 +08:00
|
|
|
return sts;
|
1993-04-02 04:59:32 +08:00
|
|
|
}
|