mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
Merge pull request #3343 from ChJR/feature/relocate_length
Relocate vm.obj_len to obj.length
This commit is contained in:
@@ -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.
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -156,7 +156,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(
|
||||
|
||||
@@ -424,7 +424,7 @@ mod builtins {
|
||||
|
||||
#[pyfunction]
|
||||
fn len(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
vm.obj_len(&obj)
|
||||
obj.length(vm)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -1907,15 +1907,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 {
|
||||
|
||||
Reference in New Issue
Block a user