mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Add/rename vm.obj_len, obj_len_opt
This commit is contained in:
4 files changed
+13
-11
No files matched your search
@@ -70,9 +70,7 @@ impl PySequenceIterator {
|
||||
let hint = if self.reversed {
|
||||
pos + 1
|
||||
} else {
|
||||
let len = vm._len(&self.obj).unwrap_or_else(|| {
|
||||
Err(vm.new_type_error("sequence has no __len__ method".to_owned()))
|
||||
})?;
|
||||
let len = vm.obj_len(&self.obj)?;
|
||||
len as isize - pos
|
||||
};
|
||||
Ok(hint)
|
||||
|
||||
@@ -440,12 +440,7 @@ mod decl {
|
||||
|
||||
#[pyfunction]
|
||||
fn len(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
vm._len(&obj).unwrap_or_else(|| {
|
||||
Err(vm.new_type_error(format!(
|
||||
"object of type '{}' has no len()",
|
||||
obj.class().name
|
||||
)))
|
||||
})
|
||||
vm.obj_len(&obj)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
|
||||
+1
-1
@@ -108,7 +108,7 @@ pub fn stop_iter_value(vm: &VirtualMachine, exc: &PyBaseExceptionRef) -> PyResul
|
||||
}
|
||||
|
||||
pub fn length_hint(vm: &VirtualMachine, iter: PyObjectRef) -> PyResult<Option<usize>> {
|
||||
if let Some(len) = vm._len(&iter) {
|
||||
if let Some(len) = vm.obj_len_opt(&iter) {
|
||||
match len {
|
||||
Ok(len) => return Ok(Some(len)),
|
||||
Err(e) => {
|
||||
|
||||
+10
-1
@@ -1525,7 +1525,7 @@ impl VirtualMachine {
|
||||
hash(&obj, self)
|
||||
}
|
||||
|
||||
pub fn _len(&self, obj: &PyObjectRef) -> Option<PyResult<usize>> {
|
||||
pub fn obj_len_opt(&self, obj: &PyObjectRef) -> Option<PyResult<usize>> {
|
||||
self.get_method(obj.clone(), "__len__").map(|len| {
|
||||
let len = self.invoke(&len?, ())?;
|
||||
let len = len
|
||||
@@ -1547,6 +1547,15 @@ 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
|
||||
)))
|
||||
})
|
||||
}
|
||||
|
||||
// https://docs.python.org/3/reference/expressions.html#membership-test-operations
|
||||
fn _membership_iter_search(&self, haystack: PyObjectRef, needle: PyObjectRef) -> PyResult {
|
||||
let iter = iterator::get_iter(self, &haystack)?;
|
||||
|
||||
Reference in new issue
Block a user