Skip to content

Commit 33808e6

Browse files
committed
Added support for strings to dis.dis
Currently, the dis module's dis function only accepts functions tp decompile. This adds support for disassembling strings. It does this by compiling the string, then disassembling compiled output.
1 parent b31dd64 commit 33808e6

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

vm/src/stdlib/dis.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
use crate::bytecode::CodeFlags;
22
use crate::obj::objcode::PyCodeRef;
3+
use crate::obj::objstr::PyStringRef;
34
use crate::pyobject::{ItemProtocol, PyObjectRef, PyResult, TryFromObject};
45
use crate::vm::VirtualMachine;
6+
use rustpython_compiler::compile;
57

68
fn dis_dis(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
79
// Method or function:
810
if let Ok(co) = vm.get_attribute(obj.clone(), "__code__") {
911
return dis_disassemble(co, vm);
1012
}
1113

14+
// String:
15+
if let Ok(co_str) = PyStringRef::try_from_object(vm, obj.clone()) {
16+
let code = vm
17+
.compile(co_str.as_str(), compile::Mode::Exec, "<string>".to_owned())
18+
.map_err(|err| vm.new_syntax_error(&err))?
19+
.into_object();
20+
return dis_disassemble(code, vm);
21+
}
22+
1223
dis_disassemble(obj, vm)
1324
}
1425

0 commit comments

Comments
 (0)