From 6b147565424c4dad99f6421bae7e6f8948198f1a Mon Sep 17 00:00:00 2001 From: ChJR Date: Wed, 20 Oct 2021 02:08:45 +0900 Subject: [PATCH 1/2] Relocate vm.obj_len to obj.length --- stdlib/src/bisect.rs | 4 ++-- vm/src/builtins/enumerate.rs | 2 +- vm/src/builtins/iter.rs | 2 +- vm/src/stdlib/builtins.rs | 4 ++-- vm/src/stdlib/io.rs | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/stdlib/src/bisect.rs b/stdlib/src/bisect.rs index 351e3cb89..67b3d1290 100644 --- a/stdlib/src/bisect.rs +++ b/stdlib/src/bisect.rs @@ -72,7 +72,7 @@ mod _bisect { BisectArgs { a, x, lo, hi }: BisectArgs, vm: &VirtualMachine, ) -> PyResult { - 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 { - 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. diff --git a/vm/src/builtins/enumerate.rs b/vm/src/builtins/enumerate.rs index 41a146faa..e8495a254 100644 --- a/vm/src/builtins/enumerate.rs +++ b/vm/src/builtins/enumerate.rs @@ -88,7 +88,7 @@ impl PyReverseSequenceIterator { fn length_hint(&self, vm: &VirtualMachine) -> PyResult { 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); } } diff --git a/vm/src/builtins/iter.rs b/vm/src/builtins/iter.rs index 30f410c08..c30764368 100644 --- a/vm/src/builtins/iter.rs +++ b/vm/src/builtins/iter.rs @@ -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 { diff --git a/vm/src/stdlib/builtins.rs b/vm/src/stdlib/builtins.rs index 763f272d1..d88de55a5 100644 --- a/vm/src/stdlib/builtins.rs +++ b/vm/src/stdlib/builtins.rs @@ -424,7 +424,7 @@ mod builtins { #[pyfunction] fn len(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { - 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)) } diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index 4f7f436a0..a6befda11 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -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::::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 { From 32e693f2345accd5fb89b47f9af3487e07cab786 Mon Sep 17 00:00:00 2001 From: ChJR Date: Wed, 20 Oct 2021 02:33:46 +0900 Subject: [PATCH 2/2] Replace vm.obj_len to obj.length --- vm/src/protocol/object.rs | 7 ++++++- vm/src/vm.rs | 9 --------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/vm/src/protocol/object.rs b/vm/src/protocol/object.rs index 940512d0e..b2e989076 100644 --- a/vm/src/protocol/object.rs +++ b/vm/src/protocol/object.rs @@ -151,7 +151,12 @@ impl PyObjectRef { // int PyObject_TypeCheck(PyObject *o, PyTypeObject *type) pub fn length(&self, vm: &VirtualMachine) -> PyResult { - 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( diff --git a/vm/src/vm.rs b/vm/src/vm.rs index bb60bd7e0..3b97bfc80 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -1920,15 +1920,6 @@ impl VirtualMachine { }) } - pub fn obj_len(&self, obj: &PyObjectRef) -> PyResult { - 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> { if let Some(len) = self.obj_len_opt(&iter) { match len {