From ae74db4c0bc276cfff086cfeb3771e7df0526656 Mon Sep 17 00:00:00 2001 From: Nicholas Gates Date: Wed, 11 Oct 2023 16:12:37 +0100 Subject: [PATCH] Remove const from moduleState (#206) --- pydust/src/builtins.zig | 4 +++- pydust/src/types/obj.zig | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pydust/src/builtins.zig b/pydust/src/builtins.zig index ad50be56..d5223a31 100644 --- a/pydust/src/builtins.zig +++ b/pydust/src/builtins.zig @@ -210,7 +210,7 @@ pub fn len(object: anytype) !usize { } /// Return the runtime module state for a Pydust module definition. -pub fn moduleState(comptime Module: type) !*const Module { +pub fn moduleState(comptime Module: type) !*Module { if (State.getDefinition(Module).type != .module) { @compileError("Not a module definition: " ++ Module); } @@ -279,6 +279,8 @@ pub fn tuple(object: anytype) !py.PyTuple { return py.PyTuple.unchecked(.{ .py = pytuple }); } +/// Return the PyType object for a given Python object. +/// Returns a borrowed reference. pub fn type_(object: anytype) !py.PyType { return .{ .obj = .{ .py = @as( ?*ffi.PyObject, diff --git a/pydust/src/types/obj.zig b/pydust/src/types/obj.zig index ed35e899..b58e1924 100644 --- a/pydust/src/types/obj.zig +++ b/pydust/src/types/obj.zig @@ -110,7 +110,9 @@ pub fn PyObjectMixin(comptime name: []const u8, comptime prefix: []const u8, com /// Checked conversion from a PyObject. pub fn checked(obj: py.PyObject) !Self { if (PyCheck(obj.py) == 0) { - return py.TypeError.raiseFmt("expected {s}, found {s}", .{ name, try (try py.str(try py.type_(obj))).asSlice() }); + const typeName = try py.str(try py.type_(obj)); + defer typeName.decref(); + return py.TypeError.raiseFmt("expected {s}, found {s}", .{ name, try typeName.asSlice() }); } return .{ .obj = obj }; }