From 2930c109793044bdc2f8aeea82dc549a0dc90ac5 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Tue, 15 Oct 2019 22:22:05 +0300 Subject: [PATCH 1/2] Convert random to new arg style --- vm/src/stdlib/random.rs | 31 +++++-------------------------- 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/vm/src/stdlib/random.rs b/vm/src/stdlib/random.rs index 38f3f78ba5..78074e90ed 100644 --- a/vm/src/stdlib/random.rs +++ b/vm/src/stdlib/random.rs @@ -3,8 +3,6 @@ use rand::distributions::Distribution; use rand_distr::Normal; -use crate::function::PyFuncArgs; -use crate::obj::objfloat; use crate::pyobject::{PyObjectRef, PyResult}; use crate::vm::VirtualMachine; @@ -12,29 +10,14 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { let ctx = &vm.ctx; py_module!(vm, "random", { - "guass" => ctx.new_rustfunc(random_gauss), + "guass" => ctx.new_rustfunc(random_normalvariate), // TODO: is this the same? "normalvariate" => ctx.new_rustfunc(random_normalvariate), "random" => ctx.new_rustfunc(random_random), // "weibull", ctx.new_rustfunc(random_weibullvariate), }) } -fn random_gauss(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - // TODO: is this the same? - random_normalvariate(vm, args) -} - -fn random_normalvariate(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - arg_check!( - vm, - args, - required = [ - (mu, Some(vm.ctx.float_type())), - (sigma, Some(vm.ctx.float_type())) - ] - ); - let mu = objfloat::get_value(mu); - let sigma = objfloat::get_value(sigma); +fn random_normalvariate(mu: f64, sigma: f64, vm: &VirtualMachine) -> PyResult { let normal = Normal::new(mu, sigma).map_err(|rand_err| { vm.new_exception( vm.ctx.exceptions.arithmetic_error.clone(), @@ -42,15 +25,11 @@ fn random_normalvariate(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { ) })?; let value = normal.sample(&mut rand::thread_rng()); - let py_value = vm.ctx.new_float(value); - Ok(py_value) + Ok(value) } -fn random_random(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - arg_check!(vm, args); - let value = rand::random::(); - let py_value = vm.ctx.new_float(value); - Ok(py_value) +fn random_random(_vm: &VirtualMachine) -> f64 { + rand::random::() } /* From cd96d2b29230f814f4830156147c75823c1b900f Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Thu, 17 Oct 2019 12:08:04 +0300 Subject: [PATCH 2/2] Fix typo --- vm/src/stdlib/random.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vm/src/stdlib/random.rs b/vm/src/stdlib/random.rs index 78074e90ed..398ce749b7 100644 --- a/vm/src/stdlib/random.rs +++ b/vm/src/stdlib/random.rs @@ -10,7 +10,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { let ctx = &vm.ctx; py_module!(vm, "random", { - "guass" => ctx.new_rustfunc(random_normalvariate), // TODO: is this the same? + "gauss" => ctx.new_rustfunc(random_normalvariate), // TODO: is this the same? "normalvariate" => ctx.new_rustfunc(random_normalvariate), "random" => ctx.new_rustfunc(random_random), // "weibull", ctx.new_rustfunc(random_weibullvariate), @@ -29,7 +29,7 @@ fn random_normalvariate(mu: f64, sigma: f64, vm: &VirtualMachine) -> PyResult f64 { - rand::random::() + rand::random() } /*