Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix py.lift for root module #211

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions pydust/src/builtins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,23 @@ fn lift(comptime PydustStruct: type) !py.PyObject {
// Grab the qualified name, importing the root module first.
comptime var qualName = State.getIdentifier(PydustStruct).qualifiedName;

const root = try import(qualName[0]);
defer root.decref();
var mod = try import(qualName[0]);

// Recursively resolve submodules / nested classes
var mod = root;
inline for (qualName[1 .. qualName.len - 1]) |part| {
mod = try mod.get(part);
defer mod.decref(); // Inline loop so this runs at the end of the function.
if (comptime qualName.len > 1) {
inline for (qualName[1 .. qualName.len - 1]) |part| {
const prev_mod = mod;
mod = try mod.get(part);
prev_mod.decref();
}

const prev_mod = mod;
mod = try mod.get(qualName[qualName.len - 1]);
prev_mod.decref();
}

// Grab the attribute using the final part of the qualified name.
return mod.get(qualName[qualName.len - 1]);
return mod;
}

const testing = std.testing;
Expand Down