diff --git a/Cargo.lock b/Cargo.lock index 98a6eafd7..69378c986 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -411,7 +411,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "num-bigint" -version = "0.2.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", @@ -625,7 +625,7 @@ dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", - "num-bigint 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num-bigint 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "num-complex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -949,7 +949,7 @@ dependencies = [ "checksum new_debug_unreachable 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0cdc457076c78ab54d5e0d6fa7c47981757f1e34dc39ff92787f217dede586c4" "checksum nix 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d37e713a259ff641624b6cb20e3b12b2952313ba36b6823c0f16e6cfd9e5de17" "checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2" -"checksum num-bigint 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3eceac7784c5dc97c2d6edf30259b4e153e6e2b42b3c85e9a6e9f45d06caef6e" +"checksum num-bigint 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "10b8423ea72ec64751198856a853e07b37087cfc9b53a87ecb19bff67b6d1320" "checksum num-complex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "107b9be86cd2481930688277b675b0114578227f034674726605b8a482d8baf8" "checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea" "checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1" diff --git a/tests/snippets/builtin_bin.py b/tests/snippets/builtin_bin.py index c81bec5c6..97f57b2f1 100644 --- a/tests/snippets/builtin_bin.py +++ b/tests/snippets/builtin_bin.py @@ -6,8 +6,7 @@ assert bin(2**24-1) == '0b' + '1' * 24 assert bin(-(2**24)) == '-0b1' + '0' * 24 assert bin(-(2**24-1)) == '-0b' + '1' * 24 -# TODO: change to 2**65 when we have support for pow in bigint -a = 2**20 * 2**20 * 2**25 +a = 2 ** 65 assert bin(a) == '0b1' + '0' * 65 assert bin(a-1) == '0b' + '1' * 65 assert bin(-(a)) == '-0b1' + '0' * 65 diff --git a/tests/snippets/whats_left_to_implement.py b/tests/snippets/whats_left_to_implement.py index d357282d8..2c5c7b95a 100644 --- a/tests/snippets/whats_left_to_implement.py +++ b/tests/snippets/whats_left_to_implement.py @@ -873,12 +873,11 @@ for method in bytes_expected_methods: not_implemented.append(("bytes", method)) for method in complex_expected_methods: -# TODO: uncomment this when complex is implemented -# try: -# if not hasattr(complex, method): -# not_implemented.append(("complex", method)) -# except NameError: - not_implemented.append(("complex", method)) + try: + if not hasattr(complex, method): + not_implemented.append(("complex", method)) + except NameError: + not_implemented.append(("complex", method)) for method in dict_expected_methods: try: diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 5effc29c3..84a95c5ff 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -6,7 +6,7 @@ authors = ["Shing Lyu "] [dependencies] bitflags = "1.0.4" num-complex = "0.2" -num-bigint = "0.2" +num-bigint = "0.2.1" num-traits = "0.2" log = "0.3" rustpython_parser = {path = "../parser"} diff --git a/vm/src/obj/objcomplex.rs b/vm/src/obj/objcomplex.rs index 464d60ede..520655915 100644 --- a/vm/src/obj/objcomplex.rs +++ b/vm/src/obj/objcomplex.rs @@ -12,6 +12,7 @@ pub fn init(context: &PyContext) { complex_type.set_attr("__add__", context.new_rustfunc(complex_add)); complex_type.set_attr("__new__", context.new_rustfunc(complex_new)); complex_type.set_attr("__repr__", context.new_rustfunc(complex_repr)); + complex_type.set_attr("conjugate", context.new_rustfunc(complex_conjugate)); } pub fn get_value(obj: &PyObjectRef) -> Complex64 { @@ -64,6 +65,13 @@ fn complex_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { } } +fn complex_conjugate(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!(vm, args, required = [(i, Some(vm.ctx.complex_type()))]); + + let v1 = get_value(i); + Ok(vm.ctx.new_complex(v1.conj())) +} + fn complex_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(obj, Some(vm.ctx.complex_type()))]); let v = get_value(obj); diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index da66f23b2..03d303e71 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -7,7 +7,7 @@ use super::objfloat; use super::objstr; use super::objtype; use num_bigint::{BigInt, ToBigInt}; -use num_traits::{Signed, ToPrimitive, Zero}; +use num_traits::{Pow, Signed, ToPrimitive, Zero}; // This proxy allows for easy switching between types. // TODO: maybe this is a good idea: @@ -296,10 +296,7 @@ fn int_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let v1 = get_value(i); if objtype::isinstance(i2, &vm.ctx.int_type()) { let v2 = get_value(i2).to_u32().unwrap(); - // TODO: look for bigint pow method - Ok(vm - .ctx - .new_int(v1.to_i32().unwrap().pow(v2).to_bigint().unwrap())) + Ok(vm.ctx.new_int(v1.pow(v2).to_bigint().unwrap())) } else if objtype::isinstance(i2, &vm.ctx.float_type()) { let v2 = objfloat::get_value(i2); Ok(vm.ctx.new_float((v1.to_f64().unwrap()).powf(v2)))