From 4123e5eabc11caecc5e39cc61a8ca9cad4a5f0d9 Mon Sep 17 00:00:00 2001 From: jfh Date: Sun, 24 Oct 2021 12:04:39 +0300 Subject: [PATCH] vm.length_hint into vm.length_hint_opt --- vm/src/builtins/map.rs | 2 +- vm/src/protocol/iter.rs | 4 ++-- vm/src/protocol/object.rs | 8 ++------ vm/src/stdlib/operator.rs | 2 +- vm/src/vm.rs | 4 ++-- 5 files changed, 8 insertions(+), 12 deletions(-) diff --git a/vm/src/builtins/map.rs b/vm/src/builtins/map.rs index 834733686..b7261e33a 100644 --- a/vm/src/builtins/map.rs +++ b/vm/src/builtins/map.rs @@ -37,7 +37,7 @@ impl PyMap { #[pymethod(magic)] fn length_hint(&self, vm: &VirtualMachine) -> PyResult { self.iterators.iter().try_fold(0, |prev, cur| { - let cur = vm.length_hint(cur.as_ref().to_owned())?.unwrap_or(0); + let cur = cur.as_ref().to_owned().length_hint(0, vm)?; let max = std::cmp::max(prev, cur); Ok(max) }) diff --git a/vm/src/protocol/iter.rs b/vm/src/protocol/iter.rs index e991633c2..d671924c5 100644 --- a/vm/src/protocol/iter.rs +++ b/vm/src/protocol/iter.rs @@ -50,7 +50,7 @@ where &'b self, vm: &'a VirtualMachine, ) -> PyResult> { - let length_hint = vm.length_hint(self.as_ref().to_owned())?; + let length_hint = vm.length_hint_opt(self.as_ref().to_owned())?; Ok(PyIterIter::new(vm, self.0.borrow(), length_hint)) } @@ -65,7 +65,7 @@ where impl PyIter { /// Returns an iterator over this sequence of objects. pub fn into_iter(self, vm: &VirtualMachine) -> PyResult> { - let length_hint = vm.length_hint(self.as_object().to_owned())?; + let length_hint = vm.length_hint_opt(self.as_object().to_owned())?; Ok(PyIterIter::new(vm, self.0, length_hint)) } } diff --git a/vm/src/protocol/object.rs b/vm/src/protocol/object.rs index ee13c79da..8dedb4619 100644 --- a/vm/src/protocol/object.rs +++ b/vm/src/protocol/object.rs @@ -73,12 +73,8 @@ impl PyObjectRef { self.is_true(vm).map(|x| !x) } - pub fn length_hint( - self, - defaultvalue: Option, - vm: &VirtualMachine, - ) -> PyResult> { - Ok(vm.length_hint(self)?.or(defaultvalue)) + pub fn length_hint(self, defaultvalue: usize, vm: &VirtualMachine) -> PyResult { + Ok(vm.length_hint_opt(self)?.unwrap_or(defaultvalue)) } // item protocol diff --git a/vm/src/stdlib/operator.rs b/vm/src/stdlib/operator.rs index 500a7b0ae..db83c0a4b 100644 --- a/vm/src/stdlib/operator.rs +++ b/vm/src/stdlib/operator.rs @@ -282,7 +282,7 @@ mod _operator { v.payload::().unwrap().try_to_primitive(vm) }) .unwrap_or(Ok(0))?; - vm.length_hint(obj).map(|v| v.unwrap_or(default)) + obj.length_hint(default, vm) } // Inplace Operators diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 0250d177a..5f44c3ea1 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -1201,7 +1201,7 @@ impl VirtualMachine { F: FnMut(PyObjectRef) -> PyResult, { let iter = value.to_owned().get_iter(self)?; - let cap = match self.length_hint(value.to_owned()) { + let cap = match self.length_hint_opt(value.to_owned()) { Err(e) if e.class().is(&self.ctx.exceptions.runtime_error) => return Err(e), Ok(Some(value)) => Some(value), // Use a power of 2 as a default capacity. @@ -1681,7 +1681,7 @@ impl VirtualMachine { }) } - pub fn length_hint(&self, iter: PyObjectRef) -> PyResult> { + pub fn length_hint_opt(&self, iter: PyObjectRef) -> PyResult> { if let Some(len) = self.obj_len_opt(&iter) { match len { Ok(len) => return Ok(Some(len)),