Skip to content

Commit 84ef6e3

Browse files
committed
Add Frame.f_code.
1 parent a1b7c61 commit 84ef6e3

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

vm/src/obj/objframe.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ pub fn init(context: &PyContext) {
1313
let ref frame_type = context.frame_type;
1414
frame_type.set_attr("__new__", context.new_rustfunc(frame_new));
1515
frame_type.set_attr("__repr__", context.new_rustfunc(frame_repr));
16-
frame_type.set_attr("f_locals", context.new_property(frame_locals));
16+
frame_type.set_attr("f_locals", context.new_property(frame_flocals));
17+
frame_type.set_attr("f_code", context.new_property(frame_fcode));
1718
}
1819

1920
fn frame_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -27,7 +28,7 @@ fn frame_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2728
Ok(vm.new_str(repr))
2829
}
2930

30-
fn frame_locals(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
31+
fn frame_flocals(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3132
arg_check!(vm, args, required = [(frame, Some(vm.ctx.frame_type()))]);
3233
let frame = get_value(frame);
3334
let py_scope = frame.locals.clone();
@@ -40,6 +41,11 @@ fn frame_locals(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
4041
}
4142
}
4243

44+
fn frame_fcode(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
45+
arg_check!(vm, args, required = [(frame, Some(vm.ctx.frame_type()))]);
46+
Ok(vm.ctx.new_code_object(get_value(frame).code))
47+
}
48+
4349
pub fn get_value(obj: &PyObjectRef) -> Frame {
4450
if let PyObjectKind::Frame { frame } = &obj.borrow().kind {
4551
frame.clone()

vm/src/pyobject.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,10 @@ impl PyContext {
475475
py_obj
476476
}
477477

478+
pub fn new_code_object(&self, code: bytecode::CodeObject) -> PyObjectRef {
479+
PyObject::new(PyObjectKind::Code { code }, self.code_type())
480+
}
481+
478482
pub fn new_function(
479483
&self,
480484
code_obj: PyObjectRef,

0 commit comments

Comments
 (0)