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

Method binary_op1

crates/vm/src/vm/vm_ops.rs:177–232  ·  view source on GitHub ↗

Calling scheme used for binary operations: Order operations are tried until either a valid result or error: `b.rop(b,a)[*], a.op(a,b), b.rop(b,a)` `[*]` - only when Py_TYPE(a) != Py_TYPE(b) && Py_TYPE(b) is a subclass of Py_TYPE(a)

(&self, a: &PyObject, b: &PyObject, op_slot: PyNumberBinaryOp)

Source from the content-addressed store, hash-verified

175 ///
176 /// `[*]` - only when Py_TYPE(a) != Py_TYPE(b) && Py_TYPE(b) is a subclass of Py_TYPE(a)
177 pub fn binary_op1(&self, a: &PyObject, b: &PyObject, op_slot: PyNumberBinaryOp) -> PyResult {
178 let class_a = a.class();
179 let class_b = b.class();
180
181 // Number slots are inherited, direct access is O(1)
182 let slot_a = class_a.slots.as_number.left_binary_op(op_slot);
183 let slot_a_addr = slot_a.map(|x| x as usize);
184 let mut slot_b = None;
185 let left_b_addr;
186
187 if !class_a.is(class_b) {
188 let slot_bb = class_b.slots.as_number.right_binary_op(op_slot);
189 if slot_bb.map(|x| x as usize) != slot_a_addr {
190 slot_b = slot_bb;
191 }
192 left_b_addr = class_b
193 .slots
194 .as_number
195 .left_binary_op(op_slot)
196 .map(|x| x as usize);
197 } else {
198 left_b_addr = slot_a_addr;
199 }
200
201 if let Some(slot_a) = slot_a {
202 if let Some(slot_bb) = slot_b
203 && class_b.fast_issubclass(class_a)
204 && (slot_a_addr != left_b_addr
205 || method_is_overloaded(
206 class_a,
207 class_b,
208 op_slot.right_method_name(self),
209 self,
210 )?)
211 {
212 let ret = slot_bb(a, b, self)?;
213 if !ret.is(&self.ctx.not_implemented) {
214 return Ok(ret);
215 }
216 slot_b = None;
217 }
218 let ret = slot_a(a, b, self)?;
219 if !ret.is(&self.ctx.not_implemented) {
220 return Ok(ret);
221 }
222 }
223
224 if let Some(slot_b) = slot_b {
225 let ret = slot_b(a, b, self)?;
226 if !ret.is(&self.ctx.not_implemented) {
227 return Ok(ret);
228 }
229 }
230
231 Ok(self.ctx.not_implemented())
232 }
233
234 pub fn binary_op(

Callers 6

concatMethod · 0.80
repeatMethod · 0.80
binary_opMethod · 0.80
binary_iop1Method · 0.80
_addMethod · 0.80
_mulMethod · 0.80

Calls 9

method_is_overloadedFunction · 0.85
left_binary_opMethod · 0.80
isMethod · 0.80
right_binary_opMethod · 0.80
fast_issubclassMethod · 0.80
right_method_nameMethod · 0.80
classMethod · 0.45
mapMethod · 0.45
not_implementedMethod · 0.45

Tested by

no test coverage detected