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

Method split

crates/vm/src/exception_group.rs:128–184  ·  view source on GitHub ↗
(
            zelf: PyRef<PyBaseException>,
            condition: PyObjectRef,
            vm: &VirtualMachine,
        )

Source from the content-addressed store, hash-verified

126
127 #[pymethod]
128 fn split(
129 zelf: PyRef<PyBaseException>,
130 condition: PyObjectRef,
131 vm: &VirtualMachine,
132 ) -> PyResult<PyTupleRef> {
133 let matcher = get_condition_matcher(&condition, vm)?;
134
135 // If self matches the condition entirely
136 let zelf_obj: PyObjectRef = zelf.clone().into();
137 if matcher.check(&zelf_obj, vm)? {
138 return Ok(vm.ctx.new_tuple(vec![zelf_obj, vm.ctx.none()]));
139 }
140
141 let exceptions = get_exceptions_tuple(&zelf, vm)?;
142 let mut matching: Vec<PyObjectRef> = Vec::new();
143 let mut rest: Vec<PyObjectRef> = Vec::new();
144
145 for exc in exceptions {
146 if is_base_exception_group(&exc, vm) {
147 let result = vm.call_method(&exc, "split", (condition.clone(),))?;
148 let result_tuple: PyTupleRef = result.try_into_value(vm)?;
149 let match_part = result_tuple
150 .first()
151 .cloned()
152 .unwrap_or_else(|| vm.ctx.none());
153 let rest_part = result_tuple
154 .get(1)
155 .cloned()
156 .unwrap_or_else(|| vm.ctx.none());
157
158 if !vm.is_none(&match_part) {
159 matching.push(match_part);
160 }
161 if !vm.is_none(&rest_part) {
162 rest.push(rest_part);
163 }
164 } else if matcher.check(&exc, vm)? {
165 matching.push(exc);
166 } else {
167 rest.push(exc);
168 }
169 }
170
171 let match_group = if matching.is_empty() {
172 vm.ctx.none()
173 } else {
174 derive_and_copy_attributes(&zelf, matching, vm)?
175 };
176
177 let rest_group = if rest.is_empty() {
178 vm.ctx.none()
179 } else {
180 derive_and_copy_attributes(&zelf, rest, vm)?
181 };
182
183 Ok(vm.ctx.new_tuple(vec![match_group, rest_group]))
184 }
185

Callers

nothing calls this directly

Calls 15

get_condition_matcherFunction · 0.85
get_exceptions_tupleFunction · 0.85
newFunction · 0.85
is_base_exception_groupFunction · 0.85
try_into_valueMethod · 0.80
noneMethod · 0.80
cloneMethod · 0.45
checkMethod · 0.45
new_tupleMethod · 0.45
call_methodMethod · 0.45
firstMethod · 0.45

Tested by

no test coverage detected