From 2c0016a171714bc5b43caeaf44db0bc6550d4c93 Mon Sep 17 00:00:00 2001 From: Adam Kelly Date: Fri, 22 Feb 2019 09:55:10 +0000 Subject: [PATCH 1/3] Minimal definition of dis.dis. --- tests/snippets/dismod.py | 8 ++++---- vm/src/stdlib/dis.rs | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/snippets/dismod.py b/tests/snippets/dismod.py index cc9c1f63b8..5c3811cfa1 100644 --- a/tests/snippets/dismod.py +++ b/tests/snippets/dismod.py @@ -1,10 +1,10 @@ import dis -dis.disassemble(compile("5 + x + 5 or 2", "", "eval")) +dis.dis(compile("5 + x + 5 or 2", "", "eval")) print("\n") -dis.disassemble(compile("def f(x):\n return 1", "", "exec")) +dis.dis(compile("def f(x):\n return 1", "", "exec")) print("\n") -dis.disassemble(compile("if a:\n 1 or 2\nelif x == 'hello':\n 3\nelse:\n 4", "", "exec")) +dis.dis(compile("if a:\n 1 or 2\nelif x == 'hello':\n 3\nelse:\n 4", "", "exec")) print("\n") -dis.disassemble(compile("f(x=1, y=2)", "", "eval")) +dis.dis(compile("f(x=1, y=2)", "", "eval")) print("\n") diff --git a/vm/src/stdlib/dis.rs b/vm/src/stdlib/dis.rs index 5e1a30b033..f2a18a3f2c 100644 --- a/vm/src/stdlib/dis.rs +++ b/vm/src/stdlib/dis.rs @@ -2,6 +2,10 @@ use crate::obj::objcode; use crate::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol}; use crate::vm::VirtualMachine; +fn dis_dis(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + dis_disassemble(vm, args) +} + fn dis_disassemble(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(co, Some(vm.ctx.code_type()))]); @@ -12,6 +16,7 @@ fn dis_disassemble(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { pub fn mk_module(ctx: &PyContext) -> PyObjectRef { py_module!(ctx, "dis", { + "dis" => ctx.new_rustfunc(dis_dis), "disassemble" => ctx.new_rustfunc(dis_disassemble) }) } From 79f43ad76252eb17bca7cfc317be861453e3981c Mon Sep 17 00:00:00 2001 From: Adam Kelly Date: Fri, 22 Feb 2019 10:08:04 +0000 Subject: [PATCH 2/3] Add support for pulling __code__ out of a function (and a test that hits lots of instructions). --- tests/snippets/dismod.py | 10 ++++++++++ vm/src/stdlib/dis.rs | 9 ++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/snippets/dismod.py b/tests/snippets/dismod.py index 5c3811cfa1..2dfcd2ec3f 100644 --- a/tests/snippets/dismod.py +++ b/tests/snippets/dismod.py @@ -8,3 +8,13 @@ print("\n") dis.dis(compile("f(x=1, y=2)", "", "eval")) print("\n") + +def f(): + with g(): + try: + for a in {1: 4, 2: 5}: + yield [True and False or True, []] + except Exception: + raise not ValueError({1 for i in [1,2,3]}) + +dis.dis(f) diff --git a/vm/src/stdlib/dis.rs b/vm/src/stdlib/dis.rs index f2a18a3f2c..df08daf7c4 100644 --- a/vm/src/stdlib/dis.rs +++ b/vm/src/stdlib/dis.rs @@ -3,7 +3,14 @@ use crate::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol use crate::vm::VirtualMachine; fn dis_dis(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { - dis_disassemble(vm, args) + arg_check!(vm, args, required = [(obj, None)]); + let code_name = vm.new_str("__code__".to_string()); + let code = match vm.get_attribute(obj.clone(), code_name) { + Ok(co) => co, + Err(..) => obj.clone(), + }; + + dis_disassemble(vm, PyFuncArgs::new(vec![code], vec![])) } fn dis_disassemble(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { From 73a65a1c779a3c8f8ac30aaefb002852f6188530 Mon Sep 17 00:00:00 2001 From: Adam Kelly Date: Tue, 26 Feb 2019 09:40:48 +0000 Subject: [PATCH 3/3] dis.rs - small refactor dis_dis. --- tests/snippets/dismod.py | 14 ++++++++++++++ vm/src/stdlib/dis.rs | 11 ++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/tests/snippets/dismod.py b/tests/snippets/dismod.py index 2dfcd2ec3f..eac1d3f726 100644 --- a/tests/snippets/dismod.py +++ b/tests/snippets/dismod.py @@ -18,3 +18,17 @@ def f(): raise not ValueError({1 for i in [1,2,3]}) dis.dis(f) + +class A(object): + def f(): + x += 1 + pass + def g(): + for i in range(5): + if i: + continue + else: + break + +print("A.f\n") +dis.dis(A.f) diff --git a/vm/src/stdlib/dis.rs b/vm/src/stdlib/dis.rs index df08daf7c4..d726b71106 100644 --- a/vm/src/stdlib/dis.rs +++ b/vm/src/stdlib/dis.rs @@ -4,13 +4,14 @@ use crate::vm::VirtualMachine; fn dis_dis(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(obj, None)]); + + // Method or function: let code_name = vm.new_str("__code__".to_string()); - let code = match vm.get_attribute(obj.clone(), code_name) { - Ok(co) => co, - Err(..) => obj.clone(), - }; + if let Ok(co) = vm.get_attribute(obj.clone(), code_name) { + return dis_disassemble(vm, PyFuncArgs::new(vec![co], vec![])); + } - dis_disassemble(vm, PyFuncArgs::new(vec![code], vec![])) + dis_disassemble(vm, PyFuncArgs::new(vec![obj.clone()], vec![])) } fn dis_disassemble(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {