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

py.self qualified imports #201

Merged
merged 5 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
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
42 changes: 31 additions & 11 deletions pydust/src/builtins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//! See https://docs.python.org/3/library/functions.html for full reference.
const std = @import("std");
const py = @import("./pydust.zig");
const pytypes = @import("./pytypes.zig");
const State = @import("./discovery.zig").State;
const ffi = @import("./ffi.zig");
const PyError = @import("./errors.zig").PyError;
Expand Down Expand Up @@ -57,11 +58,11 @@ pub inline fn True() py.PyBool {
return py.PyBool.true_();
}

pub fn decref(value: anytype) void {
pub inline fn decref(value: anytype) void {
py.object(value).decref();
}

pub fn incref(value: anytype) void {
pub inline fn incref(value: anytype) void {
py.object(value).incref();
}

Expand Down Expand Up @@ -171,6 +172,19 @@ pub fn import(module_name: [:0]const u8) !py.PyObject {
return (try py.PyModule.import(module_name)).obj;
}

/// Instantiate a class defined in Pydust.
pub inline fn init(comptime Cls: type, args: Cls) PyError!*Cls {
const pytype = try self(Cls);

// Alloc the class
// NOTE(ngates): we currently don't allow users to override tp_alloc, therefore we can shortcut
// using ffi.PyType_GetSlot(tp_alloc) since we know it will always return ffi.PyType_GenericAlloc
const pyobj: *pytypes.PyTypeStruct(Cls) = @alignCast(@ptrCast(ffi.PyType_GenericAlloc(@ptrCast(pytype.obj.py), 0) orelse return PyError.PyRaised));
pyobj.state = args;

return &pyobj.state;
}

/// Check if object is an instance of cls.
pub fn isinstance(object: anytype, cls: anytype) !bool {
const pyobj = py.object(object);
Expand Down Expand Up @@ -248,20 +262,26 @@ pub fn type_(object: anytype) !py.PyType {
) orelse return PyError.PyRaised } };
}

// TODO(ngates): What's the easiest / cheapest way to do this?
// For now, we just check the name
pub fn self(comptime PydustType: type) !py.PyObject {
/// Returns the PyType object representing the given Pydust class.
pub fn self(comptime PydustType: type) !py.PyType {
if (@typeInfo(PydustType) != .Struct) {
@compileError("py.self should be called on a Pydust struct type. e.g. py.self(MyClass)");
}

const clsName = State.getIdentifier(PydustType).name;
const mod = State.getContaining(PydustType, .module);
const modName = State.getIdentifier(mod).name;
// Grab the qualified name, importing the root module first.
comptime var qualName = State.getIdentifier(PydustType).qualifiedName;

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

// Recursively resolve submodules / nested classes
var mod = root;
inline for (qualName[1 .. qualName.len - 1]) |part| {
mod = try mod.get(part);
}

const pymod = try import(modName);
defer pymod.decref();
return pymod.get(clsName);
// Grab the class using the final part of the qualified name.
return mod.getAs(py.PyType, qualName[qualName.len - 1]);
}

const testing = std.testing;
Expand Down
8 changes: 7 additions & 1 deletion pydust/src/discovery.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const DefinitionType = enum { module, class, attribute, property };
/// Captures the name of and relationships between Pydust objects.
const Identifier = struct {
name: [:0]const u8,
qualifiedName: []const [:0]const u8,
definition: type,
parent: type,
};
Expand Down Expand Up @@ -61,7 +62,12 @@ pub const State = blk: {
comptime name: [:0]const u8,
comptime parent: type,
) void {
identifiers[identifiersSize] = .{ .name = name, .definition = definition, .parent = parent };
identifiers[identifiersSize] = .{
.name = name,
.qualifiedName = if (parent == definition) &.{name} else getIdentifier(parent).qualifiedName ++ .{name},
.definition = definition,
.parent = parent,
};
identifiersSize += 1;
}

Expand Down
16 changes: 0 additions & 16 deletions pydust/src/pydust.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,6 @@ pub fn finalize() void {
ffi.Py_Finalize();
}

/// Instantiate a class defined in Pydust.
pub fn init(comptime Cls: type, args: Cls) PyError!*Cls {
const moduleDefinition = State.getContaining(Cls, .module);
const imported = try types.PyModule.import(State.getIdentifier(moduleDefinition).name);
defer imported.decref();
const pytype = try imported.obj.get(State.getIdentifier(Cls).name);

// Alloc the class
// NOTE(ngates): we currently don't allow users to override tp_alloc, therefore we can shortcut
// using ffi.PyType_GetSlot(tp_alloc) since we know it will always return ffi.PyType_GenericAlloc
const pyobj: *pytypes.PyTypeStruct(Cls) = @alignCast(@ptrCast(ffi.PyType_GenericAlloc(@ptrCast(pytype.py), 0) orelse return PyError.PyRaised));
pyobj.state = args;

return &pyobj.state;
}

/// Register the root Pydust module
pub fn rootmodule(comptime definition: type) void {
if (!State.isEmpty()) {
Expand Down