Skip to content

Commit

Permalink
Add suport for third-party modules (c-extensions) (#34)
Browse files Browse the repository at this point in the history
* Add support for third-party modules (c-extensions)
* Add Python3.8 support
* Format
  • Loading branch information
MinyazevR authored Nov 15, 2024
1 parent a9ad8ea commit 59cc1af
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/PythonQt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ static inline PyObject *PyCode_GetVarnames(PyCodeObject *o) {
}
#endif

#if PY_VERSION_HEX >= 0x03080000 && defined(__linux__)
#include <dlfcn.h>
#include <link.h>
#include <stdio.h>
constexpr int LD_LIBPYTHON_LOAD = 1;
constexpr int LD_LIBPYTHON_NOT_LOAD = 2;
#endif

#include <vector>

PythonQt* PythonQt::_self = nullptr;
Expand Down Expand Up @@ -337,11 +345,35 @@ void PythonQt::preCleanup()

PythonQt* PythonQt::self() { return _self; }

#if PY_VERSION_HEX >= 0x03080000 && defined(__linux__)
static int find_libpython_callback(struct dl_phdr_info *info, size_t size, void *data) {
Q_UNUSED(size);
auto *library_name = (const char *)data;
if (info->dlpi_name && strstr(info->dlpi_name, library_name)) {
if (!dlopen(info->dlpi_name, RTLD_GLOBAL | RTLD_NOLOAD | RTLD_LAZY)) {
std::cerr << library_name << "not load";
return LD_LIBPYTHON_NOT_LOAD;
}
return LD_LIBPYTHON_LOAD;
}
return 0;
}
#endif

PythonQt::PythonQt(int flags, const QByteArray& pythonQtModuleName)
{
_p = new PythonQtPrivate;
_p->_initFlags = flags;

#if PY_VERSION_HEX >= 0x03080000 && defined(__linux__)
if (flags & ExternalModule) {
const char *library_name = "libpython";
if (dl_iterate_phdr(find_libpython_callback, (void *)library_name) != LD_LIBPYTHON_LOAD) {
std::cerr << "The problem with supporting the import of third-party modules";
}
}
#endif

if ((flags & PythonAlreadyInitialized) == 0) {
#ifdef PY3K
Py_SetProgramName(const_cast<wchar_t*>(L"PythonQt"));
Expand Down
3 changes: 2 additions & 1 deletion src/PythonQt.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ class PYTHONQT_EXPORT PythonQt : public QObject {
RedirectStdOut = 1, //!<< sets if the std out/err is redirected to pythonStdOut() and pythonStdErr() signals
IgnoreSiteModule = 2, //!<< sets if Python should ignore the site module
ExternalHelp = 4, //!<< sets if help() calls on PythonQt modules are forwarded to the pythonHelpRequest() signal
PythonAlreadyInitialized = 8 //!<< sets that PythonQt should not can PyInitialize, since it is already done
PythonAlreadyInitialized = 8, //!<< sets that PythonQt should not can PyInitialize, since it is already done
ExternalModule = 16
};

//! flags that tell PythonQt which operators to expect on the registered type
Expand Down

0 comments on commit 59cc1af

Please sign in to comment.