Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix vm.to_index now use number protocol
  • Loading branch information
qingshi163 authored and youknowone committed May 29, 2022
commit 1ed18c012af12d0c9dd990ade8d42eb9dee74e56
2 changes: 0 additions & 2 deletions Lib/test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ def __index__(self):
self.assertIs(type(direct_index), int)
#self.assertIs(type(operator_index), int)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_index_returns_int_subclass(self):
class BadInt:
def __index__(self):
Expand Down
32 changes: 23 additions & 9 deletions vm/src/protocol/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ impl PyNumber<'_> {
),
1,
vm,
)?
)?;
Ok(vm.ctx.new_int(ret.as_bigint().clone()))
Comment thread
youknowone marked this conversation as resolved.
Outdated
} else {
Ok(ret)
}
Ok(ret)
} else if self.methods(vm).index.is_some() {
self.index(vm)
} else if let Ok(Ok(f)) =
Expand Down Expand Up @@ -173,9 +175,13 @@ impl PyNumber<'_> {
}
}

pub fn index(&self, vm: &VirtualMachine) -> PyResult<PyIntRef> {
pub fn index_opt(&self, vm: &VirtualMachine) -> PyResult<Option<PyIntRef>> {
if self.obj.class().is(PyInt::class(vm)) {
Ok(unsafe { self.obj.to_owned().downcast_unchecked::<PyInt>() })
Ok(Some(unsafe {
self.obj.to_owned().downcast_unchecked::<PyInt>()
}))
} else if let Some(i) = self.obj.downcast_ref::<PyInt>() {
Ok(Some(i.to_owned()))
} else if let Some(f) = self.methods(vm).index {
let ret = f(self, vm)?;
if !ret.class().is(PyInt::class(vm)) {
Expand All @@ -189,15 +195,23 @@ impl PyNumber<'_> {
),
1,
vm,
)?
)?;
Ok(Some(vm.ctx.new_int(ret.as_bigint().clone())))
Comment thread
youknowone marked this conversation as resolved.
Outdated
} else {
Ok(Some(ret))
}
Ok(ret)
} else {
Err(vm.new_type_error(format!(
Ok(None)
}
}

pub fn index(&self, vm: &VirtualMachine) -> PyResult<PyIntRef> {
self.index_opt(vm)?.ok_or_else(|| {
vm.new_type_error(format!(
"'{}' object cannot be interpreted as an integer",
self.obj.class()
)))
}
))
})
}

pub fn float_opt(&self, vm: &VirtualMachine) -> PyResult<Option<PyRef<PyFloat>>> {
Expand Down
24 changes: 3 additions & 21 deletions vm/src/vm/vm_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,18 @@ use crate::{
builtins::{PyInt, PyIntRef, PyStrInterned},
function::PyArithmeticValue,
object::{AsObject, PyObject, PyObjectRef, PyResult},
protocol::PyIterReturn,
protocol::{PyIterReturn, PyNumber},
types::PyComparisonOp,
};

/// Collection of operators
impl VirtualMachine {
pub fn to_index_opt(&self, obj: PyObjectRef) -> Option<PyResult<PyIntRef>> {
match obj.downcast() {
Ok(val) => Some(Ok(val)),
Err(obj) => self
.get_method(obj, identifier!(self, __index__))
.map(|index| {
// TODO: returning strict subclasses of int in __index__ is deprecated
self.invoke(&index?, ())?.downcast().map_err(|bad| {
self.new_type_error(format!(
"__index__ returned non-int (type {})",
bad.class().name()
))
})
}),
}
PyNumber::from(obj.as_ref()).index_opt(self).transpose()
}

pub fn to_index(&self, obj: &PyObject) -> PyResult<PyIntRef> {
self.to_index_opt(obj.to_owned()).unwrap_or_else(|| {
Err(self.new_type_error(format!(
"'{}' object cannot be interpreted as an integer",
obj.class().name()
)))
})
PyNumber::from(obj).index(self)
}

#[inline]
Expand Down