-
-
Notifications
You must be signed in to change notification settings - Fork 210
/
console.c
127 lines (106 loc) · 3.24 KB
/
console.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//-----------------------------------------------------------------------------
// console.c
// Main routine for frozen programs which run in a console.
//-----------------------------------------------------------------------------
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <locale.h>
#ifdef MS_WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
// disable filename globbing on Windows
#ifdef MS_WINDOWS
int _CRT_glob = 0;
#endif
//-----------------------------------------------------------------------------
// FatalError()
// Prints a fatal error.
//-----------------------------------------------------------------------------
static int FatalError(const char *message)
{
if (Py_IsInitialized()) {
PyErr_Print();
Py_FatalError(message);
} else fprintf(stderr, "Fatal error: %s\n", message);
return -1;
}
//-----------------------------------------------------------------------------
// FatalScriptError()
// Prints a fatal error in the initialization script.
//-----------------------------------------------------------------------------
static int FatalScriptError(void)
{
PyErr_Print();
return -1;
}
#include "common.c"
//-----------------------------------------------------------------------------
// main()
// Main routine for frozen programs.
//-----------------------------------------------------------------------------
#if defined(MS_WINDOWS)
int wmain(int argc, wchar_t **argv)
{
int status = 0;
// initialize Python
if (InitializePython(argc, argv) < 0)
status = 1;
// do the work
if (status == 0 && ExecuteScript() < 0)
status = 1;
Py_Finalize();
return status;
}
#else
int main(int argc, char **argv)
{
int status = 0;
wchar_t **wargv;
/* We need a second copy, as Python might modify the first one. */
wchar_t **wargv2;
int i;
char *oldloc;
oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
if (!oldloc)
return FatalError("Out of memory!");
// convert arguments to wide characters, using the system default locale
setlocale(LC_ALL, "");
wargv = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
if (!wargv)
return FatalError("Out of memory converting arguments!");
wargv2 = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1));
if (!wargv2) {
PyMem_RawFree(wargv);
return FatalError("Out of memory converting arguments!");
}
for (i = 0; i < argc; i++) {
wargv[i] = Py_DecodeLocale(argv[i], NULL);
if (!wargv[i]) {
status = FatalError("Unable to convert argument to string!");
argc = i;
break;
}
wargv2[i] = wargv[i];
}
wargv2[argc] = wargv[argc] = NULL;
// global pointer to argv[0] for use in SetExecutableName()
g_argv0 = argv[0];
// initialize Python
if (status == 0) {
status = InitializePython(argc, wargv);
// do the work
if (status == 0)
status = ExecuteScript();
Py_Finalize();
}
// free the memory
for (i = 0; i < argc; i++)
PyMem_RawFree(wargv2[i]);
PyMem_RawFree(wargv);
PyMem_RawFree(wargv2);
setlocale(LC_ALL, oldloc);
PyMem_RawFree(oldloc);
return status;
}
#endif