From 66ccf45c763b070e0fdc7ae3aac9ae5b70b4c070 Mon Sep 17 00:00:00 2001 From: Tim Rid <6593626+timrid@users.noreply.github.com> Date: Fri, 21 Mar 2025 19:08:52 +0100 Subject: [PATCH 1/3] Add processing of .pth files from "app_packages". --- .../src/bootstrap/main.c | 56 +++++++++++++++---- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/{{ cookiecutter.format }}/src/bootstrap/main.c b/{{ cookiecutter.format }}/src/bootstrap/main.c index c29c4dc..33ee444 100644 --- a/{{ cookiecutter.format }}/src/bootstrap/main.c +++ b/{{ cookiecutter.format }}/src/bootstrap/main.c @@ -20,6 +20,8 @@ int main(int argc, char *argv[]) { char *app_module_name; char *path; wchar_t *wtmp_str; + wchar_t *app_packages_path_str; + PyObject *app_packages_path; PyObject *app_module; PyObject *module; PyObject *module_attr; @@ -140,18 +142,6 @@ int main(int argc, char *argv[]) { } PyMem_RawFree(wtmp_str); - // Add the app_packages path - path = "/app/briefcase/app_packages"; - debug_log("- %s\n", path); - wtmp_str = Py_DecodeLocale(path, NULL); - status = PyWideStringList_Append(&config.module_search_paths, wtmp_str); - if (PyStatus_Exception(status)) { - // crash_dialog("Unable to set app path: %s", status.err_msg); - PyConfig_Clear(&config); - Py_ExitStatusException(status); - } - PyMem_RawFree(wtmp_str); - // Add the app path path = "/app/briefcase/app"; debug_log("- %s\n", path); @@ -180,6 +170,48 @@ int main(int argc, char *argv[]) { Py_ExitStatusException(status); } + + // Adding the app_packages as site directory. + // + // This adds app_packages to sys.path and executes any .pth + // files in that directory. + path = "/app/briefcase/app_packages"; + app_packages_path_str = Py_DecodeLocale(path, NULL); + + debug_log("Adding app_packages as site directory: %S\n", app_packages_path_str); + + module = PyImport_ImportModule("site"); + if (module == NULL) { + // crash_dialog("Could not import site module"); + exit(-8); + } + + module_attr = PyObject_GetAttrString(module, "addsitedir"); + if (module_attr == NULL || !PyCallable_Check(module_attr)) { + // crash_dialog("Could not access site.addsitedir"); + exit(-9); + } + + 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(-10); + } + 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(-11); + } + + result = PyObject_CallObject(module_attr, method_args); + if (result == NULL) { + // crash_dialog("Could not add app_packages directory using site.addsitedir"); + exit(-12); + } + + // Start the app module. // // From here to Py_ObjectCall(runmodule...) is effectively From 5f6be4bcecd448fe3a97fb531bc91691dd7aea40 Mon Sep 17 00:00:00 2001 From: Tim Rid <6593626+timrid@users.noreply.github.com> Date: Fri, 21 Mar 2025 21:48:44 +0100 Subject: [PATCH 2/3] fixed pre-commit errors --- {{ cookiecutter.format }}/src/bootstrap/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/{{ cookiecutter.format }}/src/bootstrap/main.c b/{{ cookiecutter.format }}/src/bootstrap/main.c index 33ee444..fc1d601 100644 --- a/{{ cookiecutter.format }}/src/bootstrap/main.c +++ b/{{ cookiecutter.format }}/src/bootstrap/main.c @@ -177,7 +177,7 @@ int main(int argc, char *argv[]) { // files in that directory. path = "/app/briefcase/app_packages"; app_packages_path_str = Py_DecodeLocale(path, NULL); - + debug_log("Adding app_packages as site directory: %S\n", app_packages_path_str); module = PyImport_ImportModule("site"); @@ -185,13 +185,13 @@ int main(int argc, char *argv[]) { // crash_dialog("Could not import site module"); exit(-8); } - + module_attr = PyObject_GetAttrString(module, "addsitedir"); if (module_attr == NULL || !PyCallable_Check(module_attr)) { // crash_dialog("Could not access site.addsitedir"); exit(-9); } - + 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"); From 13f6fd7d3c75974b847b8a1d47368eec97560727 Mon Sep 17 00:00:00 2001 From: Tim Rid <6593626+timrid@users.noreply.github.com> Date: Sat, 22 Mar 2025 12:36:21 +0100 Subject: [PATCH 3/3] synchronize exit codes with templates for other platforms --- {{ cookiecutter.format }}/src/bootstrap/main.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/{{ cookiecutter.format }}/src/bootstrap/main.c b/{{ cookiecutter.format }}/src/bootstrap/main.c index fc1d601..a7d440f 100644 --- a/{{ cookiecutter.format }}/src/bootstrap/main.c +++ b/{{ cookiecutter.format }}/src/bootstrap/main.c @@ -183,32 +183,32 @@ int main(int argc, char *argv[]) { module = PyImport_ImportModule("site"); if (module == NULL) { // crash_dialog("Could not import site module"); - exit(-8); + exit(-11); } module_attr = PyObject_GetAttrString(module, "addsitedir"); if (module_attr == NULL || !PyCallable_Check(module_attr)) { // crash_dialog("Could not access site.addsitedir"); - exit(-9); + 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(-10); + 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(-11); + exit(-14); } result = PyObject_CallObject(module_attr, method_args); if (result == NULL) { // crash_dialog("Could not add app_packages directory using site.addsitedir"); - exit(-12); + exit(-15); }