1
0
mirror of https://github.com/thp/pyotherside.git synced 2025-02-05 08:08:23 +08:00

Prepend the PyOtherSide .so to the PYTHONPATH

This commit is contained in:
Thomas Perl 2015-03-05 19:30:45 +01:00
parent ed58d65d6a
commit d931071fbd
2 changed files with 32 additions and 4 deletions

View File

@ -20,9 +20,21 @@
#include <QStandardPaths>
#include <QDir>
#include <QDebug>
#if defined(HAVE_DLADDR)
# include <dlfcn.h>
#endif
namespace PythonLibLoader {
static void prependPythonPath(const QString &path)
{
QString pythonpath(path + ":" + QString::fromUtf8(qgetenv("PYTHONPATH")));
QByteArray pythonpath_utf8 = pythonpath.toUtf8();
qputenv("PYTHONPATH", pythonpath_utf8.constData());
}
#if defined(PYTHONLIB_LOADER_HAVE_PYTHONLIB_ZIP)
bool extractPythonLibrary()
@ -30,9 +42,7 @@ bool extractPythonLibrary()
QString source(":/io/thp/pyotherside/pythonlib.zip");
QString destdir(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
QString destination(QDir(destdir).filePath("pythonlib.zip"));
// Our embedded Python library should have priority over other path elements
qputenv("PYTHONPATH", (destination + ":" + QString::fromUtf8(qgetenv("PYTHONPATH"))).toUtf8());
prependPythonPath(destination);
if (QFile::exists(destination)) {
return true;
@ -45,7 +55,20 @@ bool extractPythonLibrary()
bool extractPythonLibrary()
{
// No need to extract the Python library
#if defined(HAVE_DLADDR)
// Add the library into the path in case it has a .zip file appended
Dl_info info;
memset(&info, 0, sizeof(info));
int res = dladdr((void *)&extractPythonLibrary, &info);
if (!res) {
qWarning() << "Could not determine library path";
return false;
}
QString fname = QString::fromUtf8(info.dli_fname);
qDebug() << "Got library name: " << fname;
prependPythonPath(fname);
#endif
return true;
}

View File

@ -44,6 +44,11 @@ exists (pythonlib.zip) {
RESOURCES += pythonlib_loader.qrc
DEFINES *= PYTHONLIB_LOADER_HAVE_PYTHONLIB_ZIP
}
!windows {
DEFINES *= HAVE_DLADDR
}
HEADERS += pythonlib_loader.h
SOURCES += pythonlib_loader.cpp