Skip to content

Commit afe17df

Browse files
committed
Fix #321 Sys.Path missing some directories in latest 3.0.17 version
- moved Py_InitializeFromConfig() before appending/prepending own path to sys.path because handling of https://docs.python.org/3/c-api/init_config.html#c.PyConfig.module_search_paths changed in python3.11
1 parent b1b3b1e commit afe17df

File tree

1 file changed

+23
-92
lines changed

1 file changed

+23
-92
lines changed

PythonScript/src/PythonHandler.cpp

Lines changed: 23 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ void PythonHandler::initPython()
113113
PyConfig_Clear(&config);
114114
}
115115

116-
bool configSetFailed = false;
116+
status = Py_InitializeFromConfig(&config);
117+
if (PyStatus_Exception(status))
118+
{
119+
PyConfig_Clear(&config);
120+
}
117121

118122
//appended or prepended below in this order
119123
std::wstring machinelib = m_machineBaseDir + std::wstring(L"lib");
@@ -122,109 +126,36 @@ void PythonHandler::initPython()
122126
std::wstring userScripts = m_userBaseDir + std::wstring(L"scripts");
123127
std::wstring machinelibTK = m_machineBaseDir + std::wstring(L"lib\\lib-tk");
124128

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+
125135
// If the user wants to use their installed python version, append the paths.
126136
// If not (and they want to use the bundled python install), the default, then prepend the paths
127137
if (ConfigFile::getInstance()->getSetting(_T("PREFERINSTALLEDPYTHON")) == _T("1"))
128138
{
129139
/* 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"))
170141
{
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()));
172147
}
173148
}
174149
else
175150
{
176151
/* 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"))
226153
{
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()));
228159
}
229160
}
230161

0 commit comments

Comments
 (0)