|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +// you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +const std = @import("std"); |
| 14 | +const py = @import("../pydust.zig"); |
| 15 | +const PyObjectMixin = @import("./obj.zig").PyObjectMixin; |
| 16 | + |
| 17 | +const ffi = py.ffi; |
| 18 | + |
| 19 | +/// Wrapper for Python PyFrame. |
| 20 | +/// See: https://docs.python.org/3/c-api/frame.html |
| 21 | +pub const PyFrame = extern struct { |
| 22 | + obj: py.PyObject, |
| 23 | + |
| 24 | + pub fn get() ?PyFrame { |
| 25 | + const frame = ffi.PyEval_GetFrame(); |
| 26 | + return if (frame) |f| .{ .obj = .{ .py = objPtr(f) } } else null; |
| 27 | + } |
| 28 | + |
| 29 | + pub fn code(self: PyFrame) py.PyCode { |
| 30 | + const codeObj = ffi.PyFrame_GetCode(framePtr(self.obj.py)); |
| 31 | + return .{ .obj = .{ .py = @alignCast(@ptrCast(codeObj)) } }; |
| 32 | + } |
| 33 | + |
| 34 | + pub inline fn lineNumber(self: PyFrame) u32 { |
| 35 | + return @intCast(ffi.PyFrame_GetLineNumber(framePtr(self.obj.py))); |
| 36 | + } |
| 37 | + |
| 38 | + inline fn framePtr(obj: *ffi.PyObject) *ffi.PyFrameObject { |
| 39 | + return @alignCast(@ptrCast(obj)); |
| 40 | + } |
| 41 | + |
| 42 | + inline fn objPtr(obj: *ffi.PyFrameObject) *ffi.PyObject { |
| 43 | + return @alignCast(@ptrCast(obj)); |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +test "PyFrame" { |
| 48 | + py.initialize(); |
| 49 | + defer py.finalize(); |
| 50 | + |
| 51 | + const pf = PyFrame.get(); |
| 52 | + try std.testing.expectEqual(@as(?PyFrame, null), pf); |
| 53 | +} |
0 commit comments