Skip to content

Commit 2c1afc4

Browse files
Merge pull request RustPython#513 from RustPython/py_module_macro
Py module macro
2 parents 1d3db31 + 55148d7 commit 2c1afc4

File tree

3 files changed

+69
-64
lines changed

3 files changed

+69
-64
lines changed

vm/src/macros.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,15 @@ macro_rules! no_kwargs {
105105
}
106106
};
107107
}
108+
109+
macro_rules! py_module {
110+
( $ctx:expr, $module_name:expr, { $($name:expr => $value:expr),* $(,)* }) => {
111+
{
112+
let py_mod = $ctx.new_module($module_name, $ctx.new_scope(None));
113+
$(
114+
$ctx.set_attr(&py_mod, $name, $value);
115+
)*
116+
py_mod
117+
}
118+
}
119+
}

vm/src/stdlib/math.rs

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -172,57 +172,55 @@ fn math_lgamma(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
172172
}
173173

174174
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
175-
let py_mod = ctx.new_module(&"math".to_string(), ctx.new_scope(None));
176-
177-
// Number theory functions:
178-
ctx.set_attr(&py_mod, "fabs", ctx.new_rustfunc(math_fabs));
179-
ctx.set_attr(&py_mod, "isfinite", ctx.new_rustfunc(math_isfinite));
180-
ctx.set_attr(&py_mod, "isinf", ctx.new_rustfunc(math_isinf));
181-
ctx.set_attr(&py_mod, "isnan", ctx.new_rustfunc(math_isnan));
182-
183-
// Power and logarithmic functions:
184-
ctx.set_attr(&py_mod, "exp", ctx.new_rustfunc(math_exp));
185-
ctx.set_attr(&py_mod, "expm1", ctx.new_rustfunc(math_expm1));
186-
ctx.set_attr(&py_mod, "log", ctx.new_rustfunc(math_log));
187-
ctx.set_attr(&py_mod, "log1p", ctx.new_rustfunc(math_log1p));
188-
ctx.set_attr(&py_mod, "log2", ctx.new_rustfunc(math_log2));
189-
ctx.set_attr(&py_mod, "log10", ctx.new_rustfunc(math_log10));
190-
ctx.set_attr(&py_mod, "pow", ctx.new_rustfunc(math_pow));
191-
ctx.set_attr(&py_mod, "sqrt", ctx.new_rustfunc(math_sqrt));
192-
193-
// Trigonometric functions:
194-
ctx.set_attr(&py_mod, "acos", ctx.new_rustfunc(math_acos));
195-
ctx.set_attr(&py_mod, "asin", ctx.new_rustfunc(math_asin));
196-
ctx.set_attr(&py_mod, "atan", ctx.new_rustfunc(math_atan));
197-
ctx.set_attr(&py_mod, "atan2", ctx.new_rustfunc(math_atan2));
198-
ctx.set_attr(&py_mod, "cos", ctx.new_rustfunc(math_cos));
199-
ctx.set_attr(&py_mod, "hypot", ctx.new_rustfunc(math_hypot));
200-
ctx.set_attr(&py_mod, "sin", ctx.new_rustfunc(math_sin));
201-
ctx.set_attr(&py_mod, "tan", ctx.new_rustfunc(math_tan));
202-
203-
ctx.set_attr(&py_mod, "degrees", ctx.new_rustfunc(math_degrees));
204-
ctx.set_attr(&py_mod, "radians", ctx.new_rustfunc(math_radians));
205-
206-
// Hyperbolic functions:
207-
ctx.set_attr(&py_mod, "acosh", ctx.new_rustfunc(math_acosh));
208-
ctx.set_attr(&py_mod, "asinh", ctx.new_rustfunc(math_asinh));
209-
ctx.set_attr(&py_mod, "atanh", ctx.new_rustfunc(math_atanh));
210-
ctx.set_attr(&py_mod, "cosh", ctx.new_rustfunc(math_cosh));
211-
ctx.set_attr(&py_mod, "sinh", ctx.new_rustfunc(math_sinh));
212-
ctx.set_attr(&py_mod, "tanh", ctx.new_rustfunc(math_tanh));
213-
214-
// Special functions:
215-
ctx.set_attr(&py_mod, "erf", ctx.new_rustfunc(math_erf));
216-
ctx.set_attr(&py_mod, "erfc", ctx.new_rustfunc(math_erfc));
217-
ctx.set_attr(&py_mod, "gamma", ctx.new_rustfunc(math_gamma));
218-
ctx.set_attr(&py_mod, "lgamma", ctx.new_rustfunc(math_lgamma));
219-
220-
// Constants:
221-
ctx.set_attr(&py_mod, "pi", ctx.new_float(std::f64::consts::PI)); // 3.14159...
222-
ctx.set_attr(&py_mod, "e", ctx.new_float(std::f64::consts::E)); // 2.71..
223-
ctx.set_attr(&py_mod, "tau", ctx.new_float(2.0 * std::f64::consts::PI));
224-
ctx.set_attr(&py_mod, "inf", ctx.new_float(std::f64::INFINITY));
225-
ctx.set_attr(&py_mod, "nan", ctx.new_float(std::f64::NAN));
226-
227-
py_mod
175+
py_module!(ctx, "math", {
176+
// Number theory functions:
177+
"fabs" => ctx.new_rustfunc(math_fabs),
178+
"isfinite" => ctx.new_rustfunc(math_isfinite),
179+
"isinf" => ctx.new_rustfunc(math_isinf),
180+
"isnan" => ctx.new_rustfunc(math_isnan),
181+
182+
// Power and logarithmic functions:
183+
"exp" => ctx.new_rustfunc(math_exp),
184+
"expm1" => ctx.new_rustfunc(math_expm1),
185+
"log" => ctx.new_rustfunc(math_log),
186+
"log1p" => ctx.new_rustfunc(math_log1p),
187+
"log2" => ctx.new_rustfunc(math_log2),
188+
"log10" => ctx.new_rustfunc(math_log10),
189+
"pow" => ctx.new_rustfunc(math_pow),
190+
"sqrt" => ctx.new_rustfunc(math_sqrt),
191+
192+
// Trigonometric functions:
193+
"acos" => ctx.new_rustfunc(math_acos),
194+
"asin" => ctx.new_rustfunc(math_asin),
195+
"atan" => ctx.new_rustfunc(math_atan),
196+
"atan2" => ctx.new_rustfunc(math_atan2),
197+
"cos" => ctx.new_rustfunc(math_cos),
198+
"hypot" => ctx.new_rustfunc(math_hypot),
199+
"sin" => ctx.new_rustfunc(math_sin),
200+
"tan" => ctx.new_rustfunc(math_tan),
201+
202+
"degrees" => ctx.new_rustfunc(math_degrees),
203+
"radians" => ctx.new_rustfunc(math_radians),
204+
205+
// Hyperbolic functions:
206+
"acosh" => ctx.new_rustfunc(math_acosh),
207+
"asinh" => ctx.new_rustfunc(math_asinh),
208+
"atanh" => ctx.new_rustfunc(math_atanh),
209+
"cosh" => ctx.new_rustfunc(math_cosh),
210+
"sinh" => ctx.new_rustfunc(math_sinh),
211+
"tanh" => ctx.new_rustfunc(math_tanh),
212+
213+
// Special functions:
214+
"erf" => ctx.new_rustfunc(math_erf),
215+
"erfc" => ctx.new_rustfunc(math_erfc),
216+
"gamma" => ctx.new_rustfunc(math_gamma),
217+
"lgamma" => ctx.new_rustfunc(math_lgamma),
218+
219+
// Constants:
220+
"pi" => ctx.new_float(std::f64::consts::PI), // 3.14159...
221+
"e" => ctx.new_float(std::f64::consts::E), // 2.71..
222+
"tau" => ctx.new_float(2.0 * std::f64::consts::PI),
223+
"inf" => ctx.new_float(std::f64::INFINITY),
224+
"nan" => ctx.new_float(std::f64::NAN)
225+
})
228226
}

vm/src/stdlib/random.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@ use crate::stdlib::random::rand::distributions::{Distribution, Normal};
88
use crate::VirtualMachine;
99

1010
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
11-
let py_mod = ctx.new_module(&"random".to_string(), ctx.new_scope(None));
12-
ctx.set_attr(&py_mod, "gauss", ctx.new_rustfunc(random_gauss));
13-
ctx.set_attr(
14-
&py_mod,
15-
"normalvariate",
16-
ctx.new_rustfunc(random_normalvariate),
17-
);
18-
ctx.set_attr(&py_mod, "random", ctx.new_rustfunc(random_random));
19-
// py_mod.set_attr("weibull", ctx.new_rustfunc(random_weibullvariate));
20-
// TODO: implement more random functions.
21-
py_mod
11+
py_module!(ctx, "random", {
12+
"guass" => ctx.new_rustfunc(random_gauss),
13+
"normalvariate" => ctx.new_rustfunc(random_normalvariate),
14+
"random" => ctx.new_rustfunc(random_random),
15+
// "weibull", ctx.new_rustfunc(random_weibullvariate),
16+
})
2217
}
2318

2419
fn random_gauss(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

0 commit comments

Comments
 (0)