Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add processing of .pth files from "app_packages". #72

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 44 additions & 12 deletions {{ cookiecutter.format }}/{{ cookiecutter.class_name }}/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ int main(int argc, char *argv[]) {
NSString *path;
NSString *traceback_str;
wchar_t *wtmp_str;
wchar_t *app_packages_path_str;
const char *app_module_str;
PyObject *app_packages_path;
PyObject *app_module;
PyObject *module;
PyObject *module_attr;
Expand Down Expand Up @@ -155,18 +157,6 @@ int main(int argc, char *argv[]) {
}
PyMem_RawFree(wtmp_str);

// Add the app_packages path
path = [NSString stringWithFormat:@"%@/app_packages", resourcePath, nil];
debug_log(@"- %@", path);
wtmp_str = Py_DecodeLocale([path UTF8String], NULL);
status = PyWideStringList_Append(&config.module_search_paths, wtmp_str);
if (PyStatus_Exception(status)) {
crash_dialog([NSString stringWithFormat:@"Unable to set app packages path: %s", status.err_msg, nil]);
PyConfig_Clear(&config);
Py_ExitStatusException(status);
}
PyMem_RawFree(wtmp_str);

// Add the app path
path = [NSString stringWithFormat:@"%@/app", resourcePath, nil];
debug_log(@"- %@", path);
Expand Down Expand Up @@ -199,6 +189,48 @@ int main(int argc, char *argv[]) {
// Set up an stdout/stderr handling that is required
setup_stdout(mainBundle);


// Adding the app_packages as site directory.
//
// This adds app_packages to sys.path and executes any .pth
// files in that directory.
path = [NSString stringWithFormat:@"%@/app_packages", resourcePath, nil];
app_packages_path_str = Py_DecodeLocale([path UTF8String], NULL);

debug_log(@"Adding app_packages as site directory: %@", path);

module = PyImport_ImportModule("site");
if (module == NULL) {
crash_dialog(@"Could not import site module");
exit(-11);
}

module_attr = PyObject_GetAttrString(module, "addsitedir");
if (module_attr == NULL || !PyCallable_Check(module_attr)) {
crash_dialog(@"Could not access site.addsitedir");
exit(-12);
}

app_packages_path = PyUnicode_FromWideChar(app_packages_path_str, wcslen(app_packages_path_str));
if (app_packages_path == NULL) {
crash_dialog(@"Could not convert app_packages path to unicode");
exit(-13);
}
PyMem_RawFree(app_packages_path_str);

method_args = Py_BuildValue("(O)", app_packages_path);
if (method_args == NULL) {
crash_dialog(@"Could not create arguments for site.addsitedir");
exit(-14);
}

result = PyObject_CallObject(module_attr, method_args);
if (result == NULL) {
crash_dialog(@"Could not add app_packages directory using site.addsitedir");
exit(-15);
}


// Start the app module.
//
// From here to Py_ObjectCall(runmodule...) is effectively
Expand Down