Skip to content

Commit 6d494fb

Browse files
committed
Merge branch 'master' into iter_lazy
2 parents 6c8ec39 + aaf0eab commit 6d494fb

File tree

5 files changed

+77
-107
lines changed

5 files changed

+77
-107
lines changed

tests/snippets/math.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@
1818
assert +a == 4
1919

2020
# import math
21-
# print(math.cos(1.2))
21+
# assert(math.exp(2) == math.exp(2.0))
22+
# assert(math.exp(True) == math.exp(1.0))
23+
#
24+
# class Conversible():
25+
# def __float__(self):
26+
# print("Converting to float now!")
27+
# return 1.1111
28+
#
29+
# assert math.log(1.1111) == math.log(Conversible())

vm/src/obj/objobject.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,19 @@ fn object_getattribute(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
151151
Ok(obj_attr)
152152
} else if let Some(attr) = cls.get_attr(&name) {
153153
vm.call_get_descriptor(attr, obj.clone())
154+
} else if let Some(getter) = cls.get_attr("__getattr__") {
155+
vm.invoke(
156+
getter,
157+
PyFuncArgs {
158+
args: vec![cls, name_str.clone()],
159+
kwargs: vec![],
160+
},
161+
)
154162
} else {
155-
if let Some(getter) = cls.get_attr("__getattr__") {
156-
vm.invoke(
157-
getter,
158-
PyFuncArgs {
159-
args: vec![cls, name_str.clone()],
160-
kwargs: vec![],
161-
},
162-
)
163-
} else {
164-
let attribute_error = vm.context().exceptions.attribute_error.clone();
165-
Err(vm.new_exception(
166-
attribute_error,
167-
format!("{} has no attribute '{}'", obj.borrow(), name),
168-
))
169-
}
163+
let attribute_error = vm.context().exceptions.attribute_error.clone();
164+
Err(vm.new_exception(
165+
attribute_error,
166+
format!("{} has no attribute '{}'", obj.borrow(), name),
167+
))
170168
}
171169
}

vm/src/obj/objstr.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,8 @@ fn str_isidentifier(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
450450
&& !value.chars().nth(0).unwrap().is_digit(10)
451451
{
452452
for c in value.chars() {
453-
if c != "_".chars().nth(0).unwrap() {
454-
if !c.is_digit(10) {
455-
if !c.is_alphabetic() {
456-
is_identifier = false;
457-
}
458-
}
453+
if c != "_".chars().nth(0).unwrap() && !c.is_digit(10) && !c.is_alphabetic() {
454+
is_identifier = false;
459455
}
460456
}
461457
} else {

vm/src/obj/objtype.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,20 @@ pub fn type_getattribute(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult
187187
Ok(cls_attr)
188188
} else if let Some(attr) = mcl.get_attr(&name) {
189189
vm.call_get_descriptor(attr, cls.clone())
190+
} else if let Some(getter) = cls.get_attr("__getattr__") {
191+
vm.invoke(
192+
getter,
193+
PyFuncArgs {
194+
args: vec![mcl, name_str.clone()],
195+
kwargs: vec![],
196+
},
197+
)
190198
} else {
191-
if let Some(getter) = cls.get_attr("__getattr__") {
192-
vm.invoke(
193-
getter,
194-
PyFuncArgs {
195-
args: vec![mcl, name_str.clone()],
196-
kwargs: vec![],
197-
},
198-
)
199-
} else {
200-
let attribute_error = vm.context().exceptions.attribute_error.clone();
201-
Err(vm.new_exception(
202-
attribute_error,
203-
format!("{} has no attribute '{}'", cls.borrow(), name),
204-
))
205-
}
199+
let attribute_error = vm.context().exceptions.attribute_error.clone();
200+
Err(vm.new_exception(
201+
attribute_error,
202+
format!("{} has no attribute '{}'", cls.borrow(), name),
203+
))
206204
}
207205
}
208206

vm/src/stdlib/math.rs

Lines changed: 40 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use std;
1414
macro_rules! make_math_func {
1515
( $fname:ident, $fun:ident ) => {
1616
fn $fname(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
17-
arg_check!(vm, args, required = [(value, Some(vm.ctx.float_type()))]);
18-
let value = objfloat::get_value(value);
17+
arg_check!(vm, args, required = [(value, None)]);
18+
let value = objfloat::make_float(vm, value)?;
1919
let value = value.$fun();
2020
let value = vm.ctx.new_float(value);
2121
Ok(value)
@@ -27,20 +27,20 @@ macro_rules! make_math_func {
2727
make_math_func!(math_fabs, abs);
2828

2929
fn math_isfinite(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
30-
arg_check!(vm, args, required = [(value, Some(vm.ctx.float_type()))]);
31-
let value = objfloat::get_value(value).is_finite();
30+
arg_check!(vm, args, required = [(value, None)]);
31+
let value = objfloat::make_float(vm, value)?.is_finite();
3232
Ok(vm.ctx.new_bool(value))
3333
}
3434

3535
fn math_isinf(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
36-
arg_check!(vm, args, required = [(value, Some(vm.ctx.float_type()))]);
37-
let value = objfloat::get_value(value).is_infinite();
36+
arg_check!(vm, args, required = [(value, None)]);
37+
let value = objfloat::make_float(vm, value)?.is_infinite();
3838
Ok(vm.ctx.new_bool(value))
3939
}
4040

4141
fn math_isnan(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
42-
arg_check!(vm, args, required = [(value, Some(vm.ctx.float_type()))]);
43-
let value = objfloat::get_value(value).is_nan();
42+
arg_check!(vm, args, required = [(value, None)]);
43+
let value = objfloat::make_float(vm, value)?.is_nan();
4444
Ok(vm.ctx.new_bool(value))
4545
}
4646

@@ -49,42 +49,30 @@ make_math_func!(math_exp, exp);
4949
make_math_func!(math_expm1, exp_m1);
5050

5151
fn math_log(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
52-
arg_check!(
53-
vm,
54-
args,
55-
required = [(x, Some(vm.ctx.float_type()))],
56-
optional = [(base, Some(vm.ctx.float_type()))]
57-
);
58-
let x = objfloat::get_value(x);
52+
arg_check!(vm, args, required = [(x, None)], optional = [(base, None)]);
53+
let x = objfloat::make_float(vm, x)?;
5954
match base {
6055
None => Ok(vm.ctx.new_float(x.ln())),
6156
Some(base) => {
62-
let base = objfloat::get_value(base);
57+
let base = objfloat::make_float(vm, base)?;
6358
Ok(vm.ctx.new_float(x.log(base)))
6459
}
6560
}
6661
}
6762

6863
fn math_log1p(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
69-
arg_check!(vm, args, required = [(x, Some(vm.ctx.float_type()))]);
70-
let x = objfloat::get_value(x);
64+
arg_check!(vm, args, required = [(x, None)]);
65+
let x = objfloat::make_float(vm, x)?;
7166
Ok(vm.ctx.new_float((x + 1.0).ln()))
7267
}
7368

7469
make_math_func!(math_log2, log2);
7570
make_math_func!(math_log10, log10);
7671

7772
fn math_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
78-
arg_check!(
79-
vm,
80-
args,
81-
required = [
82-
(x, Some(vm.ctx.float_type())),
83-
(y, Some(vm.ctx.float_type()))
84-
]
85-
);
86-
let x = objfloat::get_value(x);
87-
let y = objfloat::get_value(y);
73+
arg_check!(vm, args, required = [(x, None), (y, None)]);
74+
let x = objfloat::make_float(vm, x)?;
75+
let y = objfloat::make_float(vm, y)?;
8876
Ok(vm.ctx.new_float(x.powf(y)))
8977
}
9078

@@ -96,47 +84,33 @@ make_math_func!(math_asin, asin);
9684
make_math_func!(math_atan, atan);
9785

9886
fn math_atan2(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
99-
arg_check!(
100-
vm,
101-
args,
102-
required = [
103-
(y, Some(vm.ctx.float_type())),
104-
(x, Some(vm.ctx.float_type()))
105-
]
106-
);
107-
let y = objfloat::get_value(y);
108-
let x = objfloat::get_value(x);
87+
arg_check!(vm, args, required = [(y, None), (x, None)]);
88+
let y = objfloat::make_float(vm, y)?;
89+
let x = objfloat::make_float(vm, x)?;
10990
Ok(vm.ctx.new_float(y.atan2(x)))
11091
}
11192

11293
make_math_func!(math_cos, cos);
11394

11495
fn math_hypot(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
115-
arg_check!(
116-
vm,
117-
args,
118-
required = [
119-
(x, Some(vm.ctx.float_type())),
120-
(y, Some(vm.ctx.float_type()))
121-
]
122-
);
123-
let x = objfloat::get_value(x);
124-
let y = objfloat::get_value(y);
96+
arg_check!(vm, args, required = [(x, None), (y, None)]);
97+
let x = objfloat::make_float(vm, x)?;
98+
let y = objfloat::make_float(vm, y)?;
12599
Ok(vm.ctx.new_float(x.hypot(y)))
126100
}
127101

128102
make_math_func!(math_sin, sin);
129103
make_math_func!(math_tan, tan);
130104

131105
fn math_degrees(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
132-
arg_check!(vm, args, required = [(value, Some(vm.ctx.float_type()))]);
133-
let x = objfloat::get_value(value);
106+
arg_check!(vm, args, required = [(value, None)]);
107+
let x = objfloat::make_float(vm, value)?;
134108
Ok(vm.ctx.new_float(x * (180.0 / std::f64::consts::PI)))
135109
}
136110

137111
fn math_radians(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
138-
arg_check!(vm, args, required = [(value, Some(vm.ctx.float_type()))]);
139-
let x = objfloat::get_value(value);
112+
arg_check!(vm, args, required = [(value, None)]);
113+
let x = objfloat::make_float(vm, value)?;
140114
Ok(vm.ctx.new_float(x * (std::f64::consts::PI / 180.0)))
141115
}
142116

@@ -150,8 +124,8 @@ make_math_func!(math_tanh, tanh);
150124

151125
// Special functions:
152126
fn math_erf(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
153-
arg_check!(vm, args, required = [(value, Some(vm.ctx.float_type()))]);
154-
let x = objfloat::get_value(value);
127+
arg_check!(vm, args, required = [(value, None)]);
128+
let x = objfloat::make_float(vm, value)?;
155129

156130
if x.is_nan() {
157131
Ok(vm.ctx.new_float(x))
@@ -161,8 +135,8 @@ fn math_erf(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
161135
}
162136

163137
fn math_erfc(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
164-
arg_check!(vm, args, required = [(value, Some(vm.ctx.float_type()))]);
165-
let x = objfloat::get_value(value);
138+
arg_check!(vm, args, required = [(value, None)]);
139+
let x = objfloat::make_float(vm, value)?;
166140

167141
if x.is_nan() {
168142
Ok(vm.ctx.new_float(x))
@@ -172,32 +146,28 @@ fn math_erfc(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
172146
}
173147

174148
fn math_gamma(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
175-
arg_check!(vm, args, required = [(value, Some(vm.ctx.float_type()))]);
176-
let x = objfloat::get_value(value);
149+
arg_check!(vm, args, required = [(value, None)]);
150+
let x = objfloat::make_float(vm, value)?;
177151

178152
if x.is_finite() {
179153
Ok(vm.ctx.new_float(gamma(x)))
154+
} else if x.is_nan() || x.is_sign_positive() {
155+
Ok(vm.ctx.new_float(x))
180156
} else {
181-
if x.is_nan() || x.is_sign_positive() {
182-
Ok(vm.ctx.new_float(x))
183-
} else {
184-
Ok(vm.ctx.new_float(std::f64::NAN))
185-
}
157+
Ok(vm.ctx.new_float(std::f64::NAN))
186158
}
187159
}
188160

189161
fn math_lgamma(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
190-
arg_check!(vm, args, required = [(value, Some(vm.ctx.float_type()))]);
191-
let x = objfloat::get_value(value);
162+
arg_check!(vm, args, required = [(value, None)]);
163+
let x = objfloat::make_float(vm, value)?;
192164

193165
if x.is_finite() {
194166
Ok(vm.ctx.new_float(ln_gamma(x)))
167+
} else if x.is_nan() {
168+
Ok(vm.ctx.new_float(x))
195169
} else {
196-
if x.is_nan() {
197-
Ok(vm.ctx.new_float(x))
198-
} else {
199-
Ok(vm.ctx.new_float(std::f64::INFINITY))
200-
}
170+
Ok(vm.ctx.new_float(std::f64::INFINITY))
201171
}
202172
}
203173

0 commit comments

Comments
 (0)