Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions stdlib/src/bisect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ mod _bisect {
BisectArgs { a, x, lo, hi }: BisectArgs,
vm: &VirtualMachine,
) -> PyResult<usize> {
let (mut lo, mut hi) = as_usize(lo, hi, vm.obj_len(&a)?, vm)?;
let (mut lo, mut hi) = as_usize(lo, hi, a.length(vm)?, vm)?;

while lo < hi {
// Handles issue 13496.
Expand Down Expand Up @@ -100,7 +100,7 @@ mod _bisect {
BisectArgs { a, x, lo, hi }: BisectArgs,
vm: &VirtualMachine,
) -> PyResult<usize> {
let (mut lo, mut hi) = as_usize(lo, hi, vm.obj_len(&a)?, vm)?;
let (mut lo, mut hi) = as_usize(lo, hi, a.length(vm)?, vm)?;

while lo < hi {
// Handles issue 13496.
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl PyReverseSequenceIterator {
fn length_hint(&self, vm: &VirtualMachine) -> PyResult<usize> {
let internal = self.internal.lock();
if let IterStatus::Active(obj) = &internal.status {
if internal.position <= vm.obj_len(obj)? {
if internal.position <= obj.length(vm)? {
return Ok(internal.position + 1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl PySequenceIterator {
fn length_hint(&self, vm: &VirtualMachine) -> PyObjectRef {
let internal = self.internal.lock();
if let IterStatus::Active(obj) = &internal.status {
vm.obj_len(obj)
obj.length(vm)
.map(|x| PyInt::from(x).into_object(vm))
.unwrap_or_else(|_| vm.ctx.not_implemented())
} else {
Expand Down
7 changes: 6 additions & 1 deletion vm/src/protocol/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,12 @@ impl PyObjectRef {
// int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)

pub fn length(&self, vm: &VirtualMachine) -> PyResult<usize> {
vm.obj_len(self)
vm.obj_len_opt(self).unwrap_or_else(|| {
Err(vm.new_type_error(format!(
"object of type '{}' has no len()",
self.class().name()
)))
})
}

pub fn length_hint(
Expand Down
4 changes: 2 additions & 2 deletions vm/src/stdlib/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ mod builtins {

#[pyfunction]
fn len(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
vm.obj_len(&obj)
obj.length(vm)
}

#[pyfunction]
Expand Down Expand Up @@ -690,7 +690,7 @@ mod builtins {
vm.get_method_or_type_error(obj.clone(), "__getitem__", || {
"argument to reversed() must be a sequence".to_owned()
})?;
let len = vm.obj_len(&obj)?;
let len = obj.length(vm)?;
let obj_iterator = PyReverseSequenceIterator::new(obj, len);
Ok(obj_iterator.into_object(vm))
}
Expand Down
4 changes: 2 additions & 2 deletions vm/src/stdlib/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,11 @@ mod _io {
}
let hint = hint as usize;
let mut ret = Vec::new();
let it = ArgIterable::try_from_object(vm, instance)?;
let it = ArgIterable::<PyObjectRef>::try_from_object(vm, instance)?;
let mut full_len = 0;
for line in it.iter(vm)? {
let line = line?;
let line_len = vm.obj_len(&line)?;
let line_len = line.length(vm)?;
ret.push(line.clone());
full_len += line_len;
if full_len > hint {
Expand Down
9 changes: 0 additions & 9 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1920,15 +1920,6 @@ impl VirtualMachine {
})
}

pub fn obj_len(&self, obj: &PyObjectRef) -> PyResult<usize> {
self.obj_len_opt(obj).unwrap_or_else(|| {
Err(self.new_type_error(format!(
"object of type '{}' has no len()",
obj.class().name()
)))
})
}

pub fn length_hint(&self, iter: PyObjectRef) -> PyResult<Option<usize>> {
if let Some(len) = self.obj_len_opt(&iter) {
match len {
Expand Down