From bcec400e09bd4967a2318254ed2a5a72f6a97c58 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Sat, 11 Nov 2023 10:47:39 +0100 Subject: [PATCH] Fixed a crash with -without-qt: Python appears to need a non-empty application path upon initialization --- src/pya/pya/pya.cc | 5 +---- src/tl/tl/tlFileUtils.cc | 22 ++++++++++++++++------ src/tl/tl/tlFileUtils.h | 7 ++++++- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/pya/pya/pya.cc b/src/pya/pya/pya.cc index 2666e0e5f..377c7211f 100644 --- a/src/pya/pya/pya.cc +++ b/src/pya/pya/pya.cc @@ -236,10 +236,7 @@ PythonInterpreter::PythonInterpreter (bool embedded) tl::SelfTimer timer (tl::verbosity () >= 21, "Initializing Python"); - std::string app_path; -#if defined(HAVE_QT) - app_path = tl::to_string (QCoreApplication::applicationFilePath ()); -#endif + std::string app_path = tl::get_app_path (); #if PY_MAJOR_VERSION >= 3 diff --git a/src/tl/tl/tlFileUtils.cc b/src/tl/tl/tlFileUtils.cc index 0112910de..e9d9d4dc2 100644 --- a/src/tl/tl/tlFileUtils.cc +++ b/src/tl/tl/tlFileUtils.cc @@ -939,14 +939,14 @@ get_home_path () } static std::string -get_inst_path_internal () +get_app_path_internal () { #if defined(_WIN32) wchar_t buffer[MAX_PATH]; int len; if ((len = GetModuleFileNameW (NULL, buffer, MAX_PATH)) > 0) { - return tl::absolute_path (tl::to_string (std::wstring (buffer))); + return tl::to_string (std::wstring (buffer)); } #elif __APPLE__ @@ -955,7 +955,7 @@ get_inst_path_internal () int ret = proc_pidpath (getpid (), buffer, sizeof (buffer)); if (ret > 0) { // TODO: does this correctly translate paths? (MacOS uses UTF-8 encoding with D-like normalization) - return tl::absolute_path (buffer); + return buffer; } #elif defined (__FreeBSD__) @@ -964,7 +964,7 @@ get_inst_path_internal () size_t len = PATH_MAX; const int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; if (sysctl(&mib[0], 4, &path, &len, NULL, 0) == 0) { - return tl::absolute_path(path); + return path; } return ""; @@ -972,7 +972,7 @@ get_inst_path_internal () std::string pf = tl::sprintf ("/proc/%d/exe", getpid ()); if (tl::file_exists (pf)) { - return tl::absolute_path (pf); + return pf; } #endif @@ -984,11 +984,21 @@ get_inst_path () { static std::string s_inst_path; if (s_inst_path.empty ()) { - s_inst_path = get_inst_path_internal (); + s_inst_path = tl::absolute_path (get_app_path_internal ()); } return s_inst_path; } +std::string +get_app_path () +{ + static std::string s_app_path; + if (s_app_path.empty ()) { + s_app_path = get_app_path_internal (); + } + return s_app_path; +} + std::string get_module_path (void *addr) { diff --git a/src/tl/tl/tlFileUtils.h b/src/tl/tl/tlFileUtils.h index 474ec7e29..58bfbe44e 100644 --- a/src/tl/tl/tlFileUtils.h +++ b/src/tl/tl/tlFileUtils.h @@ -269,10 +269,15 @@ std::vector TL_PUBLIC split_path (const std::string &p, bool keep_l std::string TL_PUBLIC get_home_path (); /** - * @brief Gets the path of the currently running process + * @brief Gets the path (directory) of the currently running process */ std::string TL_PUBLIC get_inst_path (); +/** + * @brief Gets the path (full exe file name) of the currently running process + */ +std::string TL_PUBLIC get_app_path (); + /** * @brief Gets the absolute path of the module (DLL/.so) which contains the given address * "address" is supposed to be the address of a function inside the module.