MCPcopy Index your code
hub / github.com/RustPython/RustPython / length_hint_opt

Method length_hint_opt

crates/vm/src/vm/vm_ops.rs:112–154  ·  view source on GitHub ↗
(&self, iter: PyObjectRef)

Source from the content-addressed store, hash-verified

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.

Callers 4

length_hintMethod · 0.80
iterMethod · 0.80
into_iterMethod · 0.80
map_py_iterMethod · 0.80

Calls 8

fast_isinstanceMethod · 0.80
isMethod · 0.80
ok_or_elseMethod · 0.80
SomeClass · 0.50
ErrClass · 0.50
lengthMethod · 0.45
get_methodMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected