diff --git a/pydust/src/builtins.zig b/pydust/src/builtins.zig index d5223a31..d5e6f87a 100644 --- a/pydust/src/builtins.zig +++ b/pydust/src/builtins.zig @@ -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;