Skip to content

Commit

Permalink
Allow state in root modules
Browse files Browse the repository at this point in the history
  • Loading branch information
gatesn committed Oct 13, 2023
1 parent ae74db4 commit 7abc775
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions pydust/src/builtins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,21 @@ 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| {
defer mod.decref();
mod = try mod.get(part);
}

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

// 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

0 comments on commit 7abc775

Please sign in to comment.