1 
2 /* Frozen modules bootstrap
3  *
4  * Limited and restricted Python interpreter to run
5  * "Tools/scripts/deepfreeze.py" on systems with no or older Python
6  * interpreter.
7  */
8 
9 #include "Python.h"
10 #include "pycore_import.h"
11 
12 /* Includes for frozen modules: */
13 #include "Python/frozen_modules/importlib._bootstrap.h"
14 #include "Python/frozen_modules/importlib._bootstrap_external.h"
15 /* End includes */
16 
17 /* Note that a negative size indicates a package. */
18 
19 static const struct _frozen bootstrap_modules[] = {
20     {"_frozen_importlib", _Py_M__importlib__bootstrap, (int)sizeof(_Py_M__importlib__bootstrap)},
21     {"_frozen_importlib_external", _Py_M__importlib__bootstrap_external, (int)sizeof(_Py_M__importlib__bootstrap_external)},
22     {0, 0, 0} /* bootstrap sentinel */
23 };
24 static const struct _frozen stdlib_modules[] = {
25     {0, 0, 0} /* stdlib sentinel */
26 };
27 static const struct _frozen test_modules[] = {
28     {0, 0, 0} /* test sentinel */
29 };
30 const struct _frozen *_PyImport_FrozenBootstrap = bootstrap_modules;
31 const struct _frozen *_PyImport_FrozenStdlib = stdlib_modules;
32 const struct _frozen *_PyImport_FrozenTest = test_modules;
33 
34 static const struct _module_alias aliases[] = {
35     {"_frozen_importlib", "importlib._bootstrap"},
36     {"_frozen_importlib_external", "importlib._bootstrap_external"},
37     {0, 0} /* aliases sentinel */
38 };
39 const struct _module_alias *_PyImport_FrozenAliases = aliases;
40 
41 /* Embedding apps may change this pointer to point to their favorite
42    collection of frozen modules: */
43 
44 const struct _frozen *PyImport_FrozenModules = NULL;
45 
46 int
47 #ifdef MS_WINDOWS
wmain(int argc,wchar_t ** argv)48 wmain(int argc, wchar_t **argv)
49 #else
50 main(int argc, char **argv)
51 #endif
52 {
53     PyStatus status;
54 
55     PyConfig config;
56     PyConfig_InitIsolatedConfig(&config);
57     // don't warn, pybuilddir.txt does not exist yet
58     config.pathconfig_warnings = 0;
59     // parse arguments
60     config.parse_argv = 1;
61     // add current script dir to sys.path
62     config.isolated = 0;
63 
64 #ifdef MS_WINDOWS
65     status = PyConfig_SetArgv(&config, argc, argv);
66 #else
67     status = PyConfig_SetBytesArgv(&config, argc, argv);
68 #endif
69     if (PyStatus_Exception(status)) {
70         goto error;
71     }
72 
73     status = PyConfig_Read(&config);
74     if (config.run_filename == NULL) {
75         status = PyStatus_Error("Run filename expected");
76         goto error;
77     }
78 
79 #define CLEAR(ATTR) \
80     do { \
81         PyMem_RawFree(ATTR); \
82         ATTR = NULL; \
83     } while (0)
84 
85     // isolate from system Python
86     CLEAR(config.base_prefix);
87     CLEAR(config.prefix);
88     CLEAR(config.base_exec_prefix);
89     CLEAR(config.exec_prefix);
90 
91     status = Py_InitializeFromConfig(&config);
92     if (PyStatus_Exception(status)) {
93         goto error;
94     }
95     PyConfig_Clear(&config);
96 
97     return Py_RunMain();
98 
99 error:
100     PyConfig_Clear(&config);
101     if (PyStatus_IsExit(status)) {
102         return status.exitcode;
103     }
104     Py_ExitStatusException(status);
105 }
106