forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjframe.rs
More file actions
55 lines (46 loc) · 1.87 KB
/
objframe.rs
File metadata and controls
55 lines (46 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*! The python `frame` type.
*/
use super::super::frame::Frame;
use super::super::pyobject::{
PyContext, PyFuncArgs, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
};
use super::super::vm::VirtualMachine;
use super::objtype;
pub fn init(context: &PyContext) {
let frame_type = &context.frame_type;
context.set_attr(&frame_type, "__new__", context.new_rustfunc(frame_new));
context.set_attr(&frame_type, "__repr__", context.new_rustfunc(frame_repr));
context.set_attr(&frame_type, "f_locals", context.new_property(frame_flocals));
context.set_attr(&frame_type, "f_code", context.new_property(frame_fcode));
}
fn frame_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(_cls, None)]);
Err(vm.new_type_error("Cannot directly create frame object".to_string()))
}
fn frame_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(_frame, Some(vm.ctx.frame_type()))]);
let repr = "<frame object at .. >".to_string();
Ok(vm.new_str(repr))
}
fn frame_flocals(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(frame, Some(vm.ctx.frame_type()))]);
let frame = get_value(frame);
let py_scope = frame.locals.clone();
let py_scope = py_scope.borrow();
if let PyObjectPayload::Scope { scope } = &py_scope.payload {
Ok(scope.locals.clone())
} else {
panic!("The scope isn't a scope!");
}
}
fn frame_fcode(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(frame, Some(vm.ctx.frame_type()))]);
Ok(vm.ctx.new_code_object(get_value(frame).code))
}
pub fn get_value(obj: &PyObjectRef) -> Frame {
if let PyObjectPayload::Frame { frame } = &obj.borrow().payload {
frame.clone()
} else {
panic!("Inner error getting int {:?}", obj);
}
}