Skip to content

Commit

Permalink
[FIX] Better way to load python
Browse files Browse the repository at this point in the history
  • Loading branch information
katyukha committed Jan 27, 2025
1 parent 1d85fe9 commit 34cca10
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
10 changes: 6 additions & 4 deletions subpackages/tipy/source/odood/tipy/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ private import std.range: ElementType, iota;


private static import bindbc.loader;
private bindbc.loader.SharedLib pylib;

private import odood.tipy.python;


private static immutable enum supported_lib_names = [
// Loaded library
private bindbc.loader.SharedLib pylib;


private static immutable enum supported_lib_names = mixin(bindbc.loader.makeLibPaths([
"libpython3.13.so",
"libpython3.12.so",
"libpython3.11.so",
Expand All @@ -24,7 +26,7 @@ private static immutable enum supported_lib_names = [
"libpython3.5.so",
"libpython3.4.so",
"libpython3.3.so",
];
]));


// Load python library
Expand Down
16 changes: 12 additions & 4 deletions subpackages/utils/source/odood/utils/addons/addon_manifest.d
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,18 @@ private shared bool _py_initialized = false;
// Initialize python interpreter (import ast.literal_eval)
shared static this() {
// TODO: think about lazy initialization of python
enforce!OdoodException(
loadPyLib,
"Cannot load python as library!",
);
import bindbc.loader;
import std.algorithm: map;
import std.string: fromStringz, join;

auto err_count_start = bindbc.loader.errorCount;
bool load_status = loadPyLib;
if (!load_status) {
auto errors = bindbc.loader.errors[err_count_start .. bindbc.loader.errorCount]
.map!((e) => "%s: %s".format(e.error.fromStringz.idup, e.message.fromStringz.idup))
.join(",\n");
throw new OdoodException("Cannot load python as library! Errors: %s".format(errors));
}

Py_Initialize();
if (!PyEval_ThreadsInitialized())
Expand Down

0 comments on commit 34cca10

Please sign in to comment.