Skip to content

Commit 384c96d

Browse files
authored
Add processing of .pth files from "app_packages". (#49)
1 parent 0a0ceea commit 384c96d

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
@@ -23,8 +23,10 @@ int main(int argc, char *argv[]) {
2323
NSString *path;
2424
NSString *traceback_str;
2525
wchar_t *wtmp_str;
26+
wchar_t *app_packages_path_str;
2627
const char* app_module_str;
2728
const char* nslog_script;
29+
PyObject *app_packages_path;
2830
PyObject *app_module;
2931
PyObject *module;
3032
PyObject *module_attr;
@@ -125,18 +127,6 @@ int main(int argc, char *argv[]) {
125127
}
126128
PyMem_RawFree(wtmp_str);
127129

128-
// Add the app_packages path
129-
path = [NSString stringWithFormat:@"%@/app_packages", resourcePath, nil];
130-
NSLog(@"- %@", path);
131-
wtmp_str = Py_DecodeLocale([path UTF8String], NULL);
132-
status = PyWideStringList_Append(&config.module_search_paths, wtmp_str);
133-
if (PyStatus_Exception(status)) {
134-
crash_dialog([NSString stringWithFormat:@"Unable to set app packages path: %s", status.err_msg, nil]);
135-
PyConfig_Clear(&config);
136-
Py_ExitStatusException(status);
137-
}
138-
PyMem_RawFree(wtmp_str);
139-
140130
// Add the app path
141131
path = [NSString stringWithFormat:@"%@/app", resourcePath, nil];
142132
NSLog(@"- %@", path);
@@ -189,6 +179,48 @@ int main(int argc, char *argv[]) {
189179
}
190180
}
191181

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

0 commit comments

Comments
 (0)