forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdis.rs
More file actions
53 lines (49 loc) · 1.59 KB
/
dis.rs
File metadata and controls
53 lines (49 loc) · 1.59 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
pub(crate) use decl::make_module;
#[pymodule(name = "dis")]
mod decl {
use crate::builtins::code::PyCodeRef;
use crate::builtins::dict::PyDictRef;
use crate::builtins::pystr::PyStrRef;
use crate::bytecode::CodeFlags;
use crate::compile;
use crate::pyobject::{BorrowValue, ItemProtocol, PyObjectRef, PyResult, TryFromObject};
use crate::vm::VirtualMachine;
#[pyfunction]
fn dis(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
let co = if let Ok(co) = vm.get_attribute(obj.clone(), "__code__") {
// Method or function:
co
} else if let Ok(co_str) = PyStrRef::try_from_object(vm, obj.clone()) {
// String:
vm.compile(
co_str.borrow_value(),
compile::Mode::Exec,
"<dis>".to_owned(),
)
.map_err(|err| vm.new_syntax_error(&err))?
.into_object()
} else {
obj
};
disassemble(co, vm)
}
#[pyfunction]
fn disassemble(co: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
let code = &PyCodeRef::try_from_object(vm, co)?.code;
print!("{}", code);
Ok(())
}
#[pyattr(name = "COMPILER_FLAG_NAMES")]
fn compiler_flag_names(vm: &VirtualMachine) -> PyDictRef {
let dict = vm.ctx.new_dict();
for (name, flag) in CodeFlags::NAME_MAPPING {
dict.set_item(
vm.ctx.new_int(flag.bits()),
vm.ctx.new_str((*name).to_owned()),
vm,
)
.unwrap();
}
dict
}
}