Skip to content

Commit 850187f

Browse files
authored
Merge pull request RustPython#3343 from ChJR/feature/relocate_length
Relocate vm.obj_len to obj.length
2 parents fcb6551 + 32e693f commit 850187f

File tree

7 files changed

+14
-18
lines changed

7 files changed

+14
-18
lines changed

stdlib/src/bisect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ mod _bisect {
7272
BisectArgs { a, x, lo, hi }: BisectArgs,
7373
vm: &VirtualMachine,
7474
) -> PyResult<usize> {
75-
let (mut lo, mut hi) = as_usize(lo, hi, vm.obj_len(&a)?, vm)?;
75+
let (mut lo, mut hi) = as_usize(lo, hi, a.length(vm)?, vm)?;
7676

7777
while lo < hi {
7878
// Handles issue 13496.
@@ -100,7 +100,7 @@ mod _bisect {
100100
BisectArgs { a, x, lo, hi }: BisectArgs,
101101
vm: &VirtualMachine,
102102
) -> PyResult<usize> {
103-
let (mut lo, mut hi) = as_usize(lo, hi, vm.obj_len(&a)?, vm)?;
103+
let (mut lo, mut hi) = as_usize(lo, hi, a.length(vm)?, vm)?;
104104

105105
while lo < hi {
106106
// Handles issue 13496.

vm/src/builtins/enumerate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl PyReverseSequenceIterator {
8888
fn length_hint(&self, vm: &VirtualMachine) -> PyResult<usize> {
8989
let internal = self.internal.lock();
9090
if let IterStatus::Active(obj) = &internal.status {
91-
if internal.position <= vm.obj_len(obj)? {
91+
if internal.position <= obj.length(vm)? {
9292
return Ok(internal.position + 1);
9393
}
9494
}

vm/src/builtins/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl PySequenceIterator {
181181
fn length_hint(&self, vm: &VirtualMachine) -> PyObjectRef {
182182
let internal = self.internal.lock();
183183
if let IterStatus::Active(obj) = &internal.status {
184-
vm.obj_len(obj)
184+
obj.length(vm)
185185
.map(|x| PyInt::from(x).into_object(vm))
186186
.unwrap_or_else(|_| vm.ctx.not_implemented())
187187
} else {

vm/src/protocol/object.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,12 @@ impl PyObjectRef {
156156
// int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)
157157

158158
pub fn length(&self, vm: &VirtualMachine) -> PyResult<usize> {
159-
vm.obj_len(self)
159+
vm.obj_len_opt(self).unwrap_or_else(|| {
160+
Err(vm.new_type_error(format!(
161+
"object of type '{}' has no len()",
162+
self.class().name()
163+
)))
164+
})
160165
}
161166

162167
pub fn length_hint(

vm/src/stdlib/builtins.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ mod builtins {
424424

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

430430
#[pyfunction]
@@ -690,7 +690,7 @@ mod builtins {
690690
vm.get_method_or_type_error(obj.clone(), "__getitem__", || {
691691
"argument to reversed() must be a sequence".to_owned()
692692
})?;
693-
let len = vm.obj_len(&obj)?;
693+
let len = obj.length(vm)?;
694694
let obj_iterator = PyReverseSequenceIterator::new(obj, len);
695695
Ok(obj_iterator.into_object(vm))
696696
}

vm/src/stdlib/io.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,11 @@ mod _io {
453453
}
454454
let hint = hint as usize;
455455
let mut ret = Vec::new();
456-
let it = ArgIterable::try_from_object(vm, instance)?;
456+
let it = ArgIterable::<PyObjectRef>::try_from_object(vm, instance)?;
457457
let mut full_len = 0;
458458
for line in it.iter(vm)? {
459459
let line = line?;
460-
let line_len = vm.obj_len(&line)?;
460+
let line_len = line.length(vm)?;
461461
ret.push(line.clone());
462462
full_len += line_len;
463463
if full_len > hint {

vm/src/vm.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,15 +1907,6 @@ impl VirtualMachine {
19071907
})
19081908
}
19091909

1910-
pub fn obj_len(&self, obj: &PyObjectRef) -> PyResult<usize> {
1911-
self.obj_len_opt(obj).unwrap_or_else(|| {
1912-
Err(self.new_type_error(format!(
1913-
"object of type '{}' has no len()",
1914-
obj.class().name()
1915-
)))
1916-
})
1917-
}
1918-
19191910
pub fn length_hint(&self, iter: PyObjectRef) -> PyResult<Option<usize>> {
19201911
if let Some(len) = self.obj_len_opt(&iter) {
19211912
match len {

0 commit comments

Comments
 (0)