Skip to content

Commit 7a43a2e

Browse files
committed
PyObject::try_index
1 parent 2d1158b commit 7a43a2e

File tree

2 files changed

+25
-40
lines changed

2 files changed

+25
-40
lines changed

vm/src/builtins/int.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,7 @@ impl Constructor for PyInt {
266266
val
267267
};
268268

269-
val.to_number()
270-
.int(vm)
271-
.map(|x| x.as_bigint().clone())
269+
val.to_number().int(vm).map(|x| x.as_bigint().clone())
272270
}
273271
} else if let OptionalArg::Present(_) = options.base {
274272
Err(vm.new_type_error("int() missing string argument".to_owned()))

vm/src/protocol/number.rs

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,36 @@ type UnaryFunc<R = PyObjectRef> = AtomicCell<Option<fn(&PyNumber, &VirtualMachin
1313
type BinaryFunc<R = PyObjectRef> =
1414
AtomicCell<Option<fn(&PyNumber, &PyObject, &VirtualMachine) -> PyResult<R>>>;
1515

16-
1716
impl PyObject {
1817
#[inline]
1918
pub fn to_number(&self) -> PyNumber<'_> {
2019
PyNumber::from(self)
2120
}
2221

2322
pub fn try_index_opt(&self, vm: &VirtualMachine) -> Option<PyResult<PyIntRef>> {
24-
self.to_number().index_opt(vm).transpose()
25-
// Some(if let Some(i) = self.downcast_ref_if_exact::<PyInt>(vm) {
26-
// Ok(i.to_owned())
27-
// } else if let Some(i) = self.payload::<PyInt>() {
28-
// Ok(vm.ctx.new_bigint(i.as_bigint()))
29-
// } else if let Some(result) = self.to_number().index(vm).transpose() {
30-
// result
31-
// } else {
32-
// return None;
33-
// })
23+
#[allow(clippy::question_mark)]
24+
Some(if let Some(i) = self.downcast_ref_if_exact::<PyInt>(vm) {
25+
Ok(i.to_owned())
26+
} else if let Some(i) = self.payload::<PyInt>() {
27+
Ok(vm.ctx.new_bigint(i.as_bigint()))
28+
} else if let Some(i) = self.to_number().index(vm).transpose() {
29+
i
30+
} else {
31+
return None;
32+
})
3433
}
3534

35+
#[inline]
3636
pub fn try_index(&self, vm: &VirtualMachine) -> PyResult<PyIntRef> {
37-
self.to_number().index(vm)
38-
// self.try_index_opt(vm).transpose()?.ok_or_else(|| {
39-
// vm.new_type_error(format!(
40-
// "'{}' object cannot be interpreted as an integer",
41-
// self.class()
42-
// ))
43-
// })
37+
self.try_index_opt(vm).transpose()?.ok_or_else(|| {
38+
vm.new_type_error(format!(
39+
"'{}' object cannot be interpreted as an integer",
40+
self.class()
41+
))
42+
})
4443
}
4544
}
46-
45+
4746
#[derive(Default)]
4847
pub struct PyNumberMethods {
4948
/* Number implementations must check *both*
@@ -213,7 +212,7 @@ impl PyNumber<'_> {
213212
Ok(ret)
214213
}
215214
} else if self.methods().index.load().is_some() {
216-
self.index(vm)
215+
self.obj.try_index(vm)
217216
} else if let Ok(Ok(f)) =
218217
vm.get_special_method(self.obj.to_owned(), identifier!(vm, __trunc__))
219218
{
@@ -225,7 +224,7 @@ impl PyNumber<'_> {
225224
// vm,
226225
// )?;
227226
let ret = f.invoke((), vm)?;
228-
ret.to_number().index(vm).map_err(|_| {
227+
ret.try_index(vm).map_err(|_| {
229228
vm.new_type_error(format!(
230229
"__trunc__ returned non-Integral (type {})",
231230
ret.class()
@@ -248,12 +247,9 @@ impl PyNumber<'_> {
248247
}
249248
}
250249

251-
pub fn index_opt(&self, vm: &VirtualMachine) -> PyResult<Option<PyIntRef>> {
252-
if let Some(i) = self.obj.downcast_ref_if_exact::<PyInt>(vm) {
253-
Ok(Some(i.to_owned()))
254-
} else if let Some(i) = self.obj.payload::<PyInt>() {
255-
Ok(Some(vm.ctx.new_bigint(i.as_bigint())))
256-
} else if let Some(f) = self.methods().index.load() {
250+
#[inline]
251+
pub fn index(&self, vm: &VirtualMachine) -> PyResult<Option<PyIntRef>> {
252+
if let Some(f) = self.methods().index.load() {
257253
let ret = f(self, vm)?;
258254
if !ret.class().is(PyInt::class(vm)) {
259255
warnings::warn(
@@ -276,15 +272,6 @@ impl PyNumber<'_> {
276272
}
277273
}
278274

279-
pub fn index(&self, vm: &VirtualMachine) -> PyResult<PyIntRef> {
280-
self.index_opt(vm)?.ok_or_else(|| {
281-
vm.new_type_error(format!(
282-
"'{}' object cannot be interpreted as an integer",
283-
self.obj.class()
284-
))
285-
})
286-
}
287-
288275
pub fn float_opt(&self, vm: &VirtualMachine) -> PyResult<Option<PyRef<PyFloat>>> {
289276
if let Some(float) = self.obj.downcast_ref_if_exact::<PyFloat>(vm) {
290277
Ok(Some(float.to_owned()))
@@ -307,7 +294,7 @@ impl PyNumber<'_> {
307294
Ok(Some(ret))
308295
}
309296
} else if self.methods().index.load().is_some() {
310-
let i = self.index(vm)?;
297+
let i = self.obj.try_index(vm)?;
311298
let value = int::try_to_float(i.as_bigint(), vm)?;
312299
Ok(Some(vm.ctx.new_float(value)))
313300
} else if let Some(value) = self.obj.downcast_ref::<PyFloat>() {

0 commit comments

Comments
 (0)