Fixed a crash with -without-qt: Python appears to need a non-empty application path upon initialization

This commit is contained in:
Matthias Koefferlein
2023-11-11 10:47:39 +01:00
parent 5c5c11fd93
commit bcec400e09
3 changed files with 23 additions and 11 deletions
+1 -4
View File
@@ -236,10 +236,7 @@ PythonInterpreter::PythonInterpreter (bool embedded)
tl::SelfTimer timer (tl::verbosity () >= 21, "Initializing Python"); tl::SelfTimer timer (tl::verbosity () >= 21, "Initializing Python");
std::string app_path; std::string app_path = tl::get_app_path ();
#if defined(HAVE_QT)
app_path = tl::to_string (QCoreApplication::applicationFilePath ());
#endif
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
+16 -6
View File
@@ -939,14 +939,14 @@ get_home_path ()
} }
static std::string static std::string
get_inst_path_internal () get_app_path_internal ()
{ {
#if defined(_WIN32) #if defined(_WIN32)
wchar_t buffer[MAX_PATH]; wchar_t buffer[MAX_PATH];
int len; int len;
if ((len = GetModuleFileNameW (NULL, buffer, MAX_PATH)) > 0) { 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__ #elif __APPLE__
@@ -955,7 +955,7 @@ get_inst_path_internal ()
int ret = proc_pidpath (getpid (), buffer, sizeof (buffer)); int ret = proc_pidpath (getpid (), buffer, sizeof (buffer));
if (ret > 0) { if (ret > 0) {
// TODO: does this correctly translate paths? (MacOS uses UTF-8 encoding with D-like normalization) // 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__) #elif defined (__FreeBSD__)
@@ -964,7 +964,7 @@ get_inst_path_internal ()
size_t len = PATH_MAX; size_t len = PATH_MAX;
const int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; const int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
if (sysctl(&mib[0], 4, &path, &len, NULL, 0) == 0) { if (sysctl(&mib[0], 4, &path, &len, NULL, 0) == 0) {
return tl::absolute_path(path); return path;
} }
return ""; return "";
@@ -972,7 +972,7 @@ get_inst_path_internal ()
std::string pf = tl::sprintf ("/proc/%d/exe", getpid ()); std::string pf = tl::sprintf ("/proc/%d/exe", getpid ());
if (tl::file_exists (pf)) { if (tl::file_exists (pf)) {
return tl::absolute_path (pf); return pf;
} }
#endif #endif
@@ -984,11 +984,21 @@ get_inst_path ()
{ {
static std::string s_inst_path; static std::string s_inst_path;
if (s_inst_path.empty ()) { 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; 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 std::string
get_module_path (void *addr) get_module_path (void *addr)
{ {
+6 -1
View File
@@ -269,10 +269,15 @@ std::vector<std::string> TL_PUBLIC split_path (const std::string &p, bool keep_l
std::string TL_PUBLIC get_home_path (); 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 (); 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 * @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. * "address" is supposed to be the address of a function inside the module.