(&self, iter: PyObjectRef)
| 110 | } |
| 111 | |
| 112 | pub fn length_hint_opt(&self, iter: PyObjectRef) -> PyResult<Option<usize>> { |
| 113 | match iter.length(self) { |
| 114 | Ok(len) => return Ok(Some(len)), |
| 115 | Err(e) => { |
| 116 | if !e.fast_isinstance(self.ctx.exceptions.type_error) { |
| 117 | return Err(e); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | let hint = match self.get_method(iter, identifier!(self, __length_hint__)) { |
| 122 | Some(hint) => hint?, |
| 123 | None => return Ok(None), |
| 124 | }; |
| 125 | let result = match hint.call((), self) { |
| 126 | Ok(res) => { |
| 127 | if res.is(&self.ctx.not_implemented) { |
| 128 | return Ok(None); |
| 129 | } |
| 130 | res |
| 131 | } |
| 132 | Err(e) => { |
| 133 | return if e.fast_isinstance(self.ctx.exceptions.type_error) { |
| 134 | Ok(None) |
| 135 | } else { |
| 136 | Err(e) |
| 137 | }; |
| 138 | } |
| 139 | }; |
| 140 | let hint = result |
| 141 | .downcast_ref::<PyInt>() |
| 142 | .ok_or_else(|| { |
| 143 | self.new_type_error(format!( |
| 144 | "'{}' object cannot be interpreted as an integer", |
| 145 | result.class().name() |
| 146 | )) |
| 147 | })? |
| 148 | .try_to_primitive::<isize>(self)?; |
| 149 | if hint.is_negative() { |
| 150 | Err(self.new_value_error("__length_hint__() should return >= 0")) |
| 151 | } else { |
| 152 | Ok(Some(hint as usize)) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /// Checks that the multiplication is able to be performed. On Ok returns the |
| 157 | /// index as a usize for sequences to be able to use immediately. |
no test coverage detected