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

Method try_to_primitive

crates/vm/src/builtins/int.rs:336–353  ·  view source on GitHub ↗
(&'a self, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

334 }
335
336 pub fn try_to_primitive<'a, I>(&'a self, vm: &VirtualMachine) -> PyResult<I>
337 where
338 I: PrimInt + TryFrom<&'a BigInt>,
339 {
340 // TODO: Python 3.14+: ValueError for negative int to unsigned type
341 // See stdlib_socket.py socket.htonl(-1)
342 //
343 // if I::min_value() == I::zero() && self.as_bigint().sign() == Sign::Minus {
344 // return Err(vm.new_value_error("Cannot convert negative int".to_owned()));
345 // }
346
347 I::try_from(self.as_bigint()).map_err(|_| {
348 vm.new_overflow_error(format!(
349 "Python int too large to convert to Rust {}",
350 core::any::type_name::<I>()
351 ))
352 })
353 }
354
355 #[inline]
356 fn int_op<F>(&self, other: PyObjectRef, op: F) -> PyArithmeticValue<BigInt>

Callers 15

tcsetattrFunction · 0.80
getrandbitsMethod · 0.80
lockfFunction · 0.80
argsMethod · 0.80
resizeMethod · 0.80
handle_defaultFunction · 0.80
nextafterFunction · 0.80
factorialFunction · 0.80
result_from_objectMethod · 0.80
get_int_or_indexFunction · 0.80
key_as_isizeMethod · 0.80

Calls 1

as_bigintMethod · 0.80

Tested by

no test coverage detected