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

Function sum

crates/vm/src/stdlib/builtins.rs:1106–1132  ·  view source on GitHub ↗
(SumArgs { iterable, start }: SumArgs, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

1104
1105 #[pyfunction]
1106 fn sum(SumArgs { iterable, start }: SumArgs, vm: &VirtualMachine) -> PyResult {
1107 // Start with zero and add at will:
1108 let mut sum = start
1109 .into_option()
1110 .unwrap_or_else(|| vm.ctx.new_int(0).into());
1111
1112 match_class!(match sum {
1113 PyStr =>
1114 return Err(vm.new_type_error(
1115 "sum() can't sum strings [use ''.join(seq) instead]".to_owned()
1116 )),
1117 PyBytes =>
1118 return Err(vm.new_type_error(
1119 "sum() can't sum bytes [use b''.join(seq) instead]".to_owned()
1120 )),
1121 PyByteArray =>
1122 return Err(vm.new_type_error(
1123 "sum() can't sum bytearray [use b''.join(seq) instead]".to_owned()
1124 )),
1125 _ => (),
1126 });
1127
1128 for item in iterable.iter(vm)? {
1129 sum = vm._add(&sum, &*item?)?;
1130 }
1131 Ok(sum)
1132 }
1133
1134 #[derive(FromArgs)]
1135 struct ImportArgs {

Callers

nothing calls this directly

Calls 4

into_optionMethod · 0.80
new_intMethod · 0.45
iterMethod · 0.45
_addMethod · 0.45

Tested by

no test coverage detected