Skip to content

Commit 22b1692

Browse files
authored
Merge pull request RustPython#1642 from youknowone/pyreturn
Suggestion: PyReturn to keep result type when it can be a special type like NotImplemented
2 parents f93d976 + 6bba9ff commit 22b1692

File tree

6 files changed

+114
-55
lines changed

6 files changed

+114
-55
lines changed

Cargo.lock

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ arr_macro = "0.1.2"
6969
csv = "1.1.1"
7070
paste = "0.1"
7171
base64 = "0.11"
72+
is-macro = "0.1"
73+
result-like = "^0.2.1"
7274

7375
flame = { version = "0.2", optional = true }
7476
flamer = { version = "0.3", optional = true }

vm/src/obj/objbool.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fn bool_or(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult
146146
let rhs = get_value(&rhs);
147147
(lhs || rhs).into_pyobject(vm)
148148
} else {
149-
Ok(get_py_int(&lhs).or(rhs.clone(), vm))
149+
get_py_int(&lhs).or(rhs.clone(), vm).into_pyobject(vm)
150150
}
151151
}
152152

@@ -158,7 +158,7 @@ fn bool_and(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult
158158
let rhs = get_value(&rhs);
159159
(lhs && rhs).into_pyobject(vm)
160160
} else {
161-
Ok(get_py_int(&lhs).and(rhs.clone(), vm))
161+
get_py_int(&lhs).and(rhs.clone(), vm).into_pyobject(vm)
162162
}
163163
}
164164

@@ -170,7 +170,7 @@ fn bool_xor(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult
170170
let rhs = get_value(&rhs);
171171
(lhs ^ rhs).into_pyobject(vm)
172172
} else {
173-
Ok(get_py_int(&lhs).xor(rhs.clone(), vm))
173+
get_py_int(&lhs).xor(rhs.clone(), vm).into_pyobject(vm)
174174
}
175175
}
176176

vm/src/obj/objfloat.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use crate::format::FormatSpec;
1212
use crate::function::{OptionalArg, OptionalOption};
1313
use crate::pyhash;
1414
use crate::pyobject::{
15-
IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
16-
TypeProtocol,
15+
IntoPyObject, PyArithmaticValue::*, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef,
16+
PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
1717
};
1818
use crate::vm::VirtualMachine;
1919

@@ -196,19 +196,18 @@ impl PyFloat {
196196
float_op: F,
197197
int_op: G,
198198
vm: &VirtualMachine,
199-
) -> PyObjectRef
199+
) -> PyComparisonValue
200200
where
201201
F: Fn(f64, f64) -> bool,
202202
G: Fn(f64, &BigInt) -> bool,
203203
{
204-
let result = if let Some(other) = other.payload_if_subclass::<PyFloat>(vm) {
205-
float_op(self.value, other.value)
204+
if let Some(other) = other.payload_if_subclass::<PyFloat>(vm) {
205+
Implemented(float_op(self.value, other.value))
206206
} else if let Some(other) = other.payload_if_subclass::<PyInt>(vm) {
207-
int_op(self.value, other.as_bigint())
207+
Implemented(int_op(self.value, other.as_bigint()))
208208
} else {
209-
return vm.ctx.not_implemented();
210-
};
211-
vm.ctx.new_bool(result)
209+
NotImplemented
210+
}
212211
}
213212

214213
#[pymethod(name = "__format__")]
@@ -222,22 +221,22 @@ impl PyFloat {
222221
}
223222

224223
#[pymethod(name = "__eq__")]
225-
fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
226-
self.cmp(other, |a, b| a == b, |a, b| int_eq(a, b), vm)
224+
fn eq(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue {
225+
self.cmp(other, |a, b| a == b, int_eq, vm)
227226
}
228227

229228
#[pymethod(name = "__ne__")]
230-
fn ne(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
231-
self.cmp(other, |a, b| a != b, |a, b| !int_eq(a, b), vm)
229+
fn ne(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue {
230+
self.eq(other, vm).map(|v| !v)
232231
}
233232

234233
#[pymethod(name = "__lt__")]
235-
fn lt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
236-
self.cmp(other, |a, b| a < b, |a, b| inner_lt_int(a, b), vm)
234+
fn lt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue {
235+
self.cmp(other, |a, b| a < b, inner_lt_int, vm)
237236
}
238237

239238
#[pymethod(name = "__le__")]
240-
fn le(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
239+
fn le(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue {
241240
self.cmp(
242241
other,
243242
|a, b| a <= b,
@@ -253,12 +252,12 @@ impl PyFloat {
253252
}
254253

255254
#[pymethod(name = "__gt__")]
256-
fn gt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
257-
self.cmp(other, |a, b| a > b, |a, b| inner_gt_int(a, b), vm)
255+
fn gt(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue {
256+
self.cmp(other, |a, b| a > b, inner_gt_int, vm)
258257
}
259258

260259
#[pymethod(name = "__ge__")]
261-
fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
260+
fn ge(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyComparisonValue {
262261
self.cmp(
263262
other,
264263
|a, b| a >= b,

0 commit comments

Comments
 (0)