From fa1f911fd795598e8b436d16e544551317f1320b Mon Sep 17 00:00:00 2001 From: Tim Rid <6593626+timrid@users.noreply.github.com> Date: Mon, 17 Mar 2025 20:59:09 +0100 Subject: [PATCH 1/5] Add processing of .pth files from "app_packages". --- .../x64/Release/sitecustomize.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 {{ cookiecutter.format }}/x64/Release/sitecustomize.py diff --git a/{{ cookiecutter.format }}/x64/Release/sitecustomize.py b/{{ cookiecutter.format }}/x64/Release/sitecustomize.py new file mode 100644 index 0000000..a8e8e75 --- /dev/null +++ b/{{ cookiecutter.format }}/x64/Release/sitecustomize.py @@ -0,0 +1,18 @@ +# A site customization that processes .pth files from "app_packages". +import os +import sys +import site + +# Add the "app_packages" directory to the path and process its .pth files. +site.addsitedir(os.path.join(os.path.dirname(__file__), "app_packages")) + +# Call the next sitecustomize script if there is one +# (https://nedbatchelder.com/blog/201001/running_code_at_python_startup.html). +del sys.modules["sitecustomize"] +this_dir = os.path.dirname(__file__) +path_index = sys.path.index(this_dir) +del sys.path[path_index] +try: + import sitecustomize # noqa: F401 +finally: + sys.path.insert(path_index, this_dir) \ No newline at end of file From d486140d1a59c9e53e5038ae16ad2abb01e212e0 Mon Sep 17 00:00:00 2001 From: Tim Rid <6593626+timrid@users.noreply.github.com> Date: Tue, 18 Mar 2025 19:08:19 +0100 Subject: [PATCH 2/5] added `site.addsitedir` to the binary instead to "sitecustomize.py" --- .../x64/Release/sitecustomize.py | 18 ------- .../{{ cookiecutter.formal_name }}/Main.cpp | 51 +++++++++++++++---- 2 files changed, 41 insertions(+), 28 deletions(-) delete mode 100644 {{ cookiecutter.format }}/x64/Release/sitecustomize.py diff --git a/{{ cookiecutter.format }}/x64/Release/sitecustomize.py b/{{ cookiecutter.format }}/x64/Release/sitecustomize.py deleted file mode 100644 index a8e8e75..0000000 --- a/{{ cookiecutter.format }}/x64/Release/sitecustomize.py +++ /dev/null @@ -1,18 +0,0 @@ -# A site customization that processes .pth files from "app_packages". -import os -import sys -import site - -# Add the "app_packages" directory to the path and process its .pth files. -site.addsitedir(os.path.join(os.path.dirname(__file__), "app_packages")) - -# Call the next sitecustomize script if there is one -# (https://nedbatchelder.com/blog/201001/running_code_at_python_startup.html). -del sys.modules["sitecustomize"] -this_dir = os.path.dirname(__file__) -path_index = sys.path.index(this_dir) -del sys.path[path_index] -try: - import sitecustomize # noqa: F401 -finally: - sys.path.insert(path_index, this_dir) \ No newline at end of file diff --git a/{{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp b/{{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp index 3a549eb..24aacbd 100644 --- a/{{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp +++ b/{{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp @@ -37,7 +37,9 @@ int Main(array^ args) { String^ app_module_name; String^ path; String^ traceback_str; + wchar_t *app_packages_path_str; wchar_t *app_module_str; + PyObject *app_packages_path; PyObject *app_module; PyObject *module; PyObject *module_attr; @@ -145,16 +147,6 @@ int Main(array^ args) { Py_ExitStatusException(status); } - // Add the app_packages path - path = System::Windows::Forms::Application::StartupPath + "\\app_packages"; - debug_log("- %S\n", wstr(path)); - status = PyWideStringList_Append(&config.module_search_paths, wstr(path)); - if (PyStatus_Exception(status)) { - crash_dialog("Unable to set app packages path: " + gcnew String(status.err_msg)); - PyConfig_Clear(&config); - Py_ExitStatusException(status); - } - // Add the app path path = System::Windows::Forms::Application::StartupPath + "\\app"; debug_log("- %S\n", wstr(path)); @@ -187,6 +179,45 @@ int Main(array^ args) { } try { + // Adding the app_packages as site directory. + // + // This adds app_packages to sys.path and executes any .pth + // files in that directory. + path = System::Windows::Forms::Application::StartupPath + "\\app_packages"; + app_packages_path_str = wstr(path); + + 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_module == NULL) { + crash_dialog("Could not convert app_packages path to unicode"); + exit(-10); + } + + method_args = Py_BuildValue("(O)", app_packages_path); + if (method_args == NULL) { + crash_dialog("Could not create arguments for runpy._run_module_as_main"); + 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 62620ac9de0dad2b7a004470cdf717bb074ce5fd Mon Sep 17 00:00:00 2001 From: timrid <6593626+timrid@users.noreply.github.com> Date: Thu, 20 Mar 2025 20:19:32 +0100 Subject: [PATCH 3/5] Update {{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp Co-authored-by: Russell Keith-Magee --- .../{{ cookiecutter.formal_name }}/Main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/{{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp b/{{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp index 24aacbd..0182a3b 100644 --- a/{{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp +++ b/{{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp @@ -208,7 +208,7 @@ int Main(array^ args) { method_args = Py_BuildValue("(O)", app_packages_path); if (method_args == NULL) { - crash_dialog("Could not create arguments for runpy._run_module_as_main"); + crash_dialog("Could not create arguments for site.addsitedir"); exit(-11); } From ae164cb6d878617f78c5d03bcdcd9cfa20726533 Mon Sep 17 00:00:00 2001 From: timrid <6593626+timrid@users.noreply.github.com> Date: Fri, 21 Mar 2025 18:41:16 +0100 Subject: [PATCH 4/5] Correct wrong variable check --- .../{{ cookiecutter.formal_name }}/Main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/{{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp b/{{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp index 0182a3b..303d091 100644 --- a/{{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp +++ b/{{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp @@ -201,7 +201,7 @@ int Main(array^ args) { } app_packages_path = PyUnicode_FromWideChar(app_packages_path_str, wcslen(app_packages_path_str)); - if (app_module == NULL) { + if (app_packages_path == NULL) { crash_dialog("Could not convert app_packages path to unicode"); exit(-10); } From 26afaff828be6abe3703dc2d9960be41104a3534 Mon Sep 17 00:00:00 2001 From: Tim Rid <6593626+timrid@users.noreply.github.com> Date: Sat, 22 Mar 2025 12:34:19 +0100 Subject: [PATCH 5/5] synchronize exit codes with templates for other platforms --- .../{{ cookiecutter.formal_name }}/Main.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/{{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp b/{{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp index 303d091..a483eef 100644 --- a/{{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp +++ b/{{ cookiecutter.format }}/{{ cookiecutter.formal_name }}/Main.cpp @@ -191,31 +191,31 @@ int Main(array^ args) { 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); } 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); } // Start the app module.