1 /*
2 * ****************************************************************************
3 * Copyright (c) 2013-2019, PyInstaller Development Team.
4 * Distributed under the terms of the GNU General Public License with exception
5 * for distributing bootloader.
6 *
7 * The full license is in the file COPYING.txt, distributed with this software.
8 * ****************************************************************************
9 */
10
11 /*
12 * Python.h replacements.
13 */
14
15 #ifdef _WIN32
16 #include <windows.h> /* HMODULE */
17 #include <winsock.h> /* ntohl */
18 #else
19 #include <dlfcn.h> /* dlsym */
20 #endif
21 #include <stddef.h> /* ptrdiff_t */
22 #include <stdlib.h>
23
24 /* PyInstaller headers. */
25 #include "pyi_global.h"
26 #include "pyi_python.h"
27
28 /*
29 * Python Entry point declarations (see macros in pyi_python.h).
30 */
31 /* external variables */
32 DECLVAR(Py_DontWriteBytecodeFlag);
33 DECLVAR(Py_FileSystemDefaultEncoding);
34 DECLVAR(Py_FrozenFlag);
35 DECLVAR(Py_IgnoreEnvironmentFlag);
36 DECLVAR(Py_NoSiteFlag);
37 DECLVAR(Py_NoUserSiteDirectory);
38 DECLVAR(Py_OptimizeFlag);
39 DECLVAR(Py_VerboseFlag);
40
41 /* functions with prefix `Py_` */
42 DECLPROC(Py_BuildValue);
43 DECLPROC(Py_DecRef);
44 DECLPROC(Py_Finalize);
45 DECLPROC(Py_IncRef);
46 DECLPROC(Py_Initialize);
47 DECLPROC(Py_SetPath);
48 DECLPROC(Py_GetPath);
49 DECLPROC(Py_SetProgramName);
50 DECLPROC(Py_SetPythonHome);
51
52 /* other functions */
53 DECLPROC(PyDict_GetItemString);
54 DECLPROC(PyErr_Clear);
55 DECLPROC(PyErr_Occurred);
56 DECLPROC(PyErr_Print);
57
58 DECLPROC(PyImport_AddModule);
59 DECLPROC(PyImport_ExecCodeModule);
60 DECLPROC(PyImport_ImportModule);
61 DECLPROC(PyList_Append);
62 DECLPROC(PyList_New);
63 DECLPROC(PyLong_AsLong);
64 DECLPROC(PyModule_GetDict);
65 DECLPROC(PyObject_CallFunction);
66 DECLPROC(PyObject_SetAttrString);
67 DECLPROC(PyRun_SimpleString);
68 DECLPROC(PyString_FromString);
69 DECLPROC(PySys_AddWarnOption);
70 DECLPROC(PySys_SetArgvEx);
71 DECLPROC(PySys_GetObject);
72 DECLPROC(PySys_SetObject);
73 DECLPROC(PySys_SetPath);
74 DECLPROC(PyUnicode_FromString);
75
76 DECLPROC(Py_DecodeLocale);
77 DECLPROC(PyString_FromFormat);
78 DECLPROC(PyUnicode_FromFormat);
79 DECLPROC(PyUnicode_DecodeFSDefault);
80 DECLPROC(PyUnicode_Decode);
81
82 DECLPROC(PyEval_EvalCode);
83 DECLPROC(PyMarshal_ReadObjectFromString);
84
85 /*
86 * Get all of the entry points from libpython
87 * that we are interested in.
88 */
89 int
pyi_python_map_names(HMODULE dll,int pyvers)90 pyi_python_map_names(HMODULE dll, int pyvers)
91 {
92 GETVAR(dll, Py_DontWriteBytecodeFlag);
93 GETVAR(dll, Py_FileSystemDefaultEncoding);
94 GETVAR(dll, Py_FrozenFlag);
95 GETVAR(dll, Py_IgnoreEnvironmentFlag);
96 GETVAR(dll, Py_NoSiteFlag);
97 GETVAR(dll, Py_NoUserSiteDirectory);
98 GETVAR(dll, Py_OptimizeFlag);
99 GETVAR(dll, Py_VerboseFlag);
100
101 /* functions with prefix `Py_` */
102 GETPROC(dll, Py_BuildValue);
103 GETPROC(dll, Py_DecRef);
104 GETPROC(dll, Py_Finalize);
105 GETPROC(dll, Py_IncRef);
106 GETPROC(dll, Py_Initialize);
107
108 if (pyvers >= 30) {
109 /* new in Python 3 */
110 GETPROC(dll, Py_SetPath);
111 GETPROC(dll, Py_GetPath);
112 }
113 ;
114 GETPROC(dll, Py_SetProgramName);
115 GETPROC(dll, Py_SetPythonHome);
116
117 /* other functions */
118 GETPROC(dll, PyDict_GetItemString);
119 GETPROC(dll, PyErr_Clear);
120 GETPROC(dll, PyErr_Occurred);
121 GETPROC(dll, PyErr_Print);
122 GETPROC(dll, PyImport_AddModule);
123 GETPROC(dll, PyImport_ExecCodeModule);
124 GETPROC(dll, PyImport_ImportModule);
125 GETPROC(dll, PyList_Append);
126 GETPROC(dll, PyList_New);
127 GETPROC(dll, PyLong_AsLong);
128 GETPROC(dll, PyModule_GetDict);
129 GETPROC(dll, PyObject_CallFunction);
130 GETPROC(dll, PyObject_SetAttrString);
131 GETPROC(dll, PyRun_SimpleString);
132
133 if (pyvers < 30) {
134 GETPROC(dll, PyString_FromString);
135 GETPROC(dll, PyString_FromFormat);
136 }
137 ;
138 GETPROC(dll, PySys_AddWarnOption);
139 GETPROC(dll, PySys_SetArgvEx);
140 GETPROC(dll, PySys_GetObject);
141 GETPROC(dll, PySys_SetObject);
142 GETPROC(dll, PySys_SetPath);
143 GETPROC(dll, PyEval_EvalCode);
144 GETPROC(dll, PyMarshal_ReadObjectFromString);
145
146 if (pyvers >= 30) {
147 /* new in Python 2.6, but not reliable available in all Linux distros */
148 GETPROC(dll, PyUnicode_FromString);
149
150 /* _Py_char2wchar is new in Python 3, in Python 3.5 renamed to Py_DecodeLocale */
151 if (pyvers >= 35) {
152 GETPROC(dll, Py_DecodeLocale);
153 }
154 else {
155 GETPROC_RENAMED(dll, Py_DecodeLocale, _Py_char2wchar);
156 };
157 }
158 ;
159
160 if (pyvers >= 30) {
161 /* only used on py3 */
162 GETPROC(dll, PyUnicode_FromFormat);
163 GETPROC(dll, PyUnicode_Decode);
164 }
165
166 if (pyvers >= 32) {
167 /* new in Python 3.2 */
168 GETPROC(dll, PyUnicode_DecodeFSDefault);
169 }
170
171 VS("LOADER: Loaded functions from Python library.\n");
172
173 return 0;
174 }
175