Skip to content

Commit 1917638

Browse files
authored
Add processing of .pth files from "app_packages". (#72)
1 parent 0d0be02 commit 1917638

File tree

1 file changed

+44
-12
lines changed
  • {{ cookiecutter.format }}/{{ cookiecutter.class_name }}

1 file changed

+44
-12
lines changed

{{ cookiecutter.format }}/{{ cookiecutter.class_name }}/main.m

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ int main(int argc, char *argv[]) {
3939
NSString *path;
4040
NSString *traceback_str;
4141
wchar_t *wtmp_str;
42+
wchar_t *app_packages_path_str;
4243
const char *app_module_str;
44+
PyObject *app_packages_path;
4345
PyObject *app_module;
4446
PyObject *module;
4547
PyObject *module_attr;
@@ -155,18 +157,6 @@ int main(int argc, char *argv[]) {
155157
}
156158
PyMem_RawFree(wtmp_str);
157159

158-
// Add the app_packages path
159-
path = [NSString stringWithFormat:@"%@/app_packages", resourcePath, nil];
160-
debug_log(@"- %@", path);
161-
wtmp_str = Py_DecodeLocale([path UTF8String], NULL);
162-
status = PyWideStringList_Append(&config.module_search_paths, wtmp_str);
163-
if (PyStatus_Exception(status)) {
164-
crash_dialog([NSString stringWithFormat:@"Unable to set app packages path: %s", status.err_msg, nil]);
165-
PyConfig_Clear(&config);
166-
Py_ExitStatusException(status);
167-
}
168-
PyMem_RawFree(wtmp_str);
169-
170160
// Add the app path
171161
path = [NSString stringWithFormat:@"%@/app", resourcePath, nil];
172162
debug_log(@"- %@", path);
@@ -199,6 +189,48 @@ int main(int argc, char *argv[]) {
199189
// Set up an stdout/stderr handling that is required
200190
setup_stdout(mainBundle);
201191

192+
193+
// Adding the app_packages as site directory.
194+
//
195+
// This adds app_packages to sys.path and executes any .pth
196+
// files in that directory.
197+
path = [NSString stringWithFormat:@"%@/app_packages", resourcePath, nil];
198+
app_packages_path_str = Py_DecodeLocale([path UTF8String], NULL);
199+
200+
debug_log(@"Adding app_packages as site directory: %@", path);
201+
202+
module = PyImport_ImportModule("site");
203+
if (module == NULL) {
204+
crash_dialog(@"Could not import site module");
205+
exit(-11);
206+
}
207+
208+
module_attr = PyObject_GetAttrString(module, "addsitedir");
209+
if (module_attr == NULL || !PyCallable_Check(module_attr)) {
210+
crash_dialog(@"Could not access site.addsitedir");
211+
exit(-12);
212+
}
213+
214+
app_packages_path = PyUnicode_FromWideChar(app_packages_path_str, wcslen(app_packages_path_str));
215+
if (app_packages_path == NULL) {
216+
crash_dialog(@"Could not convert app_packages path to unicode");
217+
exit(-13);
218+
}
219+
PyMem_RawFree(app_packages_path_str);
220+
221+
method_args = Py_BuildValue("(O)", app_packages_path);
222+
if (method_args == NULL) {
223+
crash_dialog(@"Could not create arguments for site.addsitedir");
224+
exit(-14);
225+
}
226+
227+
result = PyObject_CallObject(module_attr, method_args);
228+
if (result == NULL) {
229+
crash_dialog(@"Could not add app_packages directory using site.addsitedir");
230+
exit(-15);
231+
}
232+
233+
202234
// Start the app module.
203235
//
204236
// From here to Py_ObjectCall(runmodule...) is effectively

0 commit comments

Comments
 (0)