@@ -113,7 +113,11 @@ void PythonHandler::initPython()
113
113
PyConfig_Clear (&config);
114
114
}
115
115
116
- bool configSetFailed = false ;
116
+ status = Py_InitializeFromConfig (&config);
117
+ if (PyStatus_Exception (status))
118
+ {
119
+ PyConfig_Clear (&config);
120
+ }
117
121
118
122
// appended or prepended below in this order
119
123
std::wstring machinelib = m_machineBaseDir + std::wstring (L" lib" );
@@ -122,109 +126,36 @@ void PythonHandler::initPython()
122
126
std::wstring userScripts = m_userBaseDir + std::wstring (L" scripts" );
123
127
std::wstring machinelibTK = m_machineBaseDir + std::wstring (L" lib\\ lib-tk" );
124
128
129
+ std::shared_ptr<char > machinelibInUtf8 = WcharMbcsConverter::wchar2char (machinelib.c_str ());
130
+ std::shared_ptr<char > userlibInUtf8 = WcharMbcsConverter::wchar2char (userlib.c_str ());
131
+ std::shared_ptr<char > machineScriptsInUtf8 = WcharMbcsConverter::wchar2char (machineScripts.c_str ());
132
+ std::shared_ptr<char > userScriptsInUtf8 = WcharMbcsConverter::wchar2char (userScripts.c_str ());
133
+ std::shared_ptr<char > machinelibTKInUtf8 = WcharMbcsConverter::wchar2char (machinelibTK.c_str ());
134
+
125
135
// If the user wants to use their installed python version, append the paths.
126
136
// If not (and they want to use the bundled python install), the default, then prepend the paths
127
137
if (ConfigFile::getInstance ()->getSetting (_T (" PREFERINSTALLEDPYTHON" )) == _T (" 1" ))
128
138
{
129
139
/* Append our custom search path to sys.path */
130
- status = PyWideStringList_Append (&config.module_search_paths ,
131
- machinelib.c_str ());
132
-
133
- if (PyStatus_Exception (status))
134
- {
135
- configSetFailed = true ;
136
- }
137
-
138
- /* Append our custom search path to sys.path */
139
- status = PyWideStringList_Append (&config.module_search_paths ,
140
- userlib.c_str ());
141
-
142
- if (PyStatus_Exception (status))
143
- {
144
- configSetFailed = true ;
145
- }
146
-
147
- /* Append our custom search path to sys.path */
148
- status = PyWideStringList_Append (&config.module_search_paths ,
149
- machineScripts.c_str ());
150
-
151
- if (PyStatus_Exception (status))
152
- {
153
- configSetFailed = true ;
154
- }
155
-
156
- /* Append our custom search path to sys.path */
157
- status = PyWideStringList_Append (&config.module_search_paths ,
158
- userScripts.c_str ());
159
-
160
- if (PyStatus_Exception (status))
161
- {
162
- configSetFailed = true ;
163
- }
164
-
165
- /* Append our custom search path to sys.path */
166
- status = PyWideStringList_Append (&config.module_search_paths ,
167
- machinelibTK.c_str ());
168
-
169
- if (PyStatus_Exception (status))
140
+ if (PySys_GetObject (" path" ))
170
141
{
171
- configSetFailed = true ;
142
+ PyList_Append (PySys_GetObject (" path" ), PyUnicode_FromString (machinelibInUtf8.get ()));
143
+ PyList_Append (PySys_GetObject (" path" ), PyUnicode_FromString (userlibInUtf8.get ()));
144
+ PyList_Append (PySys_GetObject (" path" ), PyUnicode_FromString (machineScriptsInUtf8.get ()));
145
+ PyList_Append (PySys_GetObject (" path" ), PyUnicode_FromString (userScriptsInUtf8.get ()));
146
+ PyList_Append (PySys_GetObject (" path" ), PyUnicode_FromString (machinelibTKInUtf8.get ()));
172
147
}
173
148
}
174
149
else
175
150
{
176
151
/* Prepend via insert our custom search path to sys.path */
177
- status = PyWideStringList_Insert (&config.module_search_paths , 0 ,
178
- machinelib.c_str ());
179
-
180
- if (PyStatus_Exception (status))
181
- {
182
- configSetFailed = true ;
183
- }
184
-
185
- /* Prepend via insert our custom search path to sys.path */
186
- status = PyWideStringList_Insert (&config.module_search_paths , 1 ,
187
- userlib.c_str ());
188
-
189
- if (PyStatus_Exception (status))
190
- {
191
- PyConfig_Clear (&config);
192
- }
193
-
194
- /* Prepend via insert our custom search path to sys.path */
195
- status = PyWideStringList_Insert (&config.module_search_paths , 2 ,
196
- machineScripts.c_str ());
197
-
198
- if (PyStatus_Exception (status))
199
- {
200
- configSetFailed = true ;
201
- }
202
-
203
- /* Prepend via insert our custom search path to sys.path */
204
- status = PyWideStringList_Insert (&config.module_search_paths , 3 ,
205
- userScripts.c_str ());
206
-
207
- if (PyStatus_Exception (status))
208
- {
209
- PyConfig_Clear (&config);
210
- }
211
-
212
- /* Prepend via insert our custom search path to sys.path */
213
- status = PyWideStringList_Insert (&config.module_search_paths , 4 ,
214
- machinelibTK.c_str ());
215
-
216
- if (PyStatus_Exception (status))
217
- {
218
- configSetFailed = true ;
219
- }
220
- }
221
-
222
- if (!configSetFailed)
223
- {
224
- status = Py_InitializeFromConfig (&config);
225
- if (PyStatus_Exception (status))
152
+ if (PySys_GetObject (" path" ))
226
153
{
227
- PyConfig_Clear (&config);
154
+ PyList_Insert (PySys_GetObject (" path" ), 0 , PyUnicode_FromString (machinelibInUtf8.get ()));
155
+ PyList_Insert (PySys_GetObject (" path" ), 1 , PyUnicode_FromString (userlibInUtf8.get ()));
156
+ PyList_Insert (PySys_GetObject (" path" ), 2 , PyUnicode_FromString (machineScriptsInUtf8.get ()));
157
+ PyList_Insert (PySys_GetObject (" path" ), 3 , PyUnicode_FromString (userScriptsInUtf8.get ()));
158
+ PyList_Insert (PySys_GetObject (" path" ), 4 , PyUnicode_FromString (machinelibTKInUtf8.get ()));
228
159
}
229
160
}
230
161
0 commit comments