Skip to content

Commit 79f43ad

Browse files
committed
Add support for pulling __code__ out of a function (and a test that hits lots of instructions).
1 parent 2c0016a commit 79f43ad

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

tests/snippets/dismod.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,13 @@
88
print("\n")
99
dis.dis(compile("f(x=1, y=2)", "", "eval"))
1010
print("\n")
11+
12+
def f():
13+
with g():
14+
try:
15+
for a in {1: 4, 2: 5}:
16+
yield [True and False or True, []]
17+
except Exception:
18+
raise not ValueError({1 for i in [1,2,3]})
19+
20+
dis.dis(f)

vm/src/stdlib/dis.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ use crate::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol
33
use crate::vm::VirtualMachine;
44

55
fn dis_dis(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
6-
dis_disassemble(vm, args)
6+
arg_check!(vm, args, required = [(obj, None)]);
7+
let code_name = vm.new_str("__code__".to_string());
8+
let code = match vm.get_attribute(obj.clone(), code_name) {
9+
Ok(co) => co,
10+
Err(..) => obj.clone(),
11+
};
12+
13+
dis_disassemble(vm, PyFuncArgs::new(vec![code], vec![]))
714
}
815

916
fn dis_disassemble(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

0 commit comments

Comments
 (0)