From a95747f161e852736cb2dfb162c9bd5f48af7706 Mon Sep 17 00:00:00 2001 From: Alexander Gude Date: Sun, 3 Feb 2019 11:18:23 -0800 Subject: [PATCH] Add .conjugate() method to int type Also add tests for for the int type, and a commented out one for the bool type. --- tests/snippets/basic_types.py | 2 ++ tests/snippets/bools.py | 1 + vm/src/obj/objint.rs | 6 ++++++ 3 files changed, 9 insertions(+) diff --git a/tests/snippets/basic_types.py b/tests/snippets/basic_types.py index 4ed164f30..1298c4ed3 100644 --- a/tests/snippets/basic_types.py +++ b/tests/snippets/basic_types.py @@ -45,6 +45,8 @@ a = complex(2, 4) assert type(a) is complex assert type(a + a) is complex +a = 1 +assert a.conjugate() == a a = 12345 diff --git a/tests/snippets/bools.py b/tests/snippets/bools.py index aeec25639..426b1605a 100644 --- a/tests/snippets/bools.py +++ b/tests/snippets/bools.py @@ -46,3 +46,4 @@ assert True + True == 2 assert False * 7 == 0 assert True > 0 assert int(True) == 1 +#assert True.conjugate() == 1 diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index 44569710f..9005a631b 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -489,6 +489,11 @@ fn int_bit_length(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { Ok(vm.ctx.new_int(bits.to_bigint().unwrap())) } +fn int_conjugate(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!(vm, args, required = [(i, Some(vm.ctx.int_type()))]); + Ok(i.clone()) +} + pub fn init(context: &PyContext) { let ref int_type = context.int_type; context.set_attr(&int_type, "__eq__", context.new_rustfunc(int_eq)); @@ -526,4 +531,5 @@ pub fn init(context: &PyContext) { "bit_length", context.new_rustfunc(int_bit_length), ); + context.set_attr(&int_type, "conjugate", context.new_rustfunc(int_conjugate)); }