Skip to content

Commit 4db7a23

Browse files
committed
Add/rename vm.obj_len, obj_len_opt
1 parent 3d19d97 commit 4db7a23

4 files changed

Lines changed: 13 additions & 11 deletions

File tree

vm/src/builtins/iter.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ impl PySequenceIterator {
7070
let hint = if self.reversed {
7171
pos + 1
7272
} else {
73-
let len = vm._len(&self.obj).unwrap_or_else(|| {
74-
Err(vm.new_type_error("sequence has no __len__ method".to_owned()))
75-
})?;
73+
let len = vm.obj_len(&self.obj)?;
7674
len as isize - pos
7775
};
7876
Ok(hint)

vm/src/builtins/make_module.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,7 @@ mod decl {
440440

441441
#[pyfunction]
442442
fn len(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
443-
vm._len(&obj).unwrap_or_else(|| {
444-
Err(vm.new_type_error(format!(
445-
"object of type '{}' has no len()",
446-
obj.class().name
447-
)))
448-
})
443+
vm.obj_len(&obj)
449444
}
450445

451446
#[pyfunction]

vm/src/iterator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn stop_iter_value(vm: &VirtualMachine, exc: &PyBaseExceptionRef) -> PyResul
108108
}
109109

110110
pub fn length_hint(vm: &VirtualMachine, iter: PyObjectRef) -> PyResult<Option<usize>> {
111-
if let Some(len) = vm._len(&iter) {
111+
if let Some(len) = vm.obj_len_opt(&iter) {
112112
match len {
113113
Ok(len) => return Ok(Some(len)),
114114
Err(e) => {

vm/src/vm.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ impl VirtualMachine {
15251525
hash(&obj, self)
15261526
}
15271527

1528-
pub fn _len(&self, obj: &PyObjectRef) -> Option<PyResult<usize>> {
1528+
pub fn obj_len_opt(&self, obj: &PyObjectRef) -> Option<PyResult<usize>> {
15291529
self.get_method(obj.clone(), "__len__").map(|len| {
15301530
let len = self.invoke(&len?, ())?;
15311531
let len = len
@@ -1547,6 +1547,15 @@ impl VirtualMachine {
15471547
})
15481548
}
15491549

1550+
pub fn obj_len(&self, obj: &PyObjectRef) -> PyResult<usize> {
1551+
self.obj_len_opt(obj).unwrap_or_else(|| {
1552+
Err(self.new_type_error(format!(
1553+
"object of type '{}' has no len()",
1554+
obj.class().name
1555+
)))
1556+
})
1557+
}
1558+
15501559
// https://docs.python.org/3/reference/expressions.html#membership-test-operations
15511560
fn _membership_iter_search(&self, haystack: PyObjectRef, needle: PyObjectRef) -> PyResult {
15521561
let iter = iterator::get_iter(self, &haystack)?;

0 commit comments

Comments
 (0)