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

Method slot_new

crates/vm/src/builtins/tuple.rs:221–252  ·  view source on GitHub ↗
(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

219 type Args = Vec<PyObjectRef>;
220
221 fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
222 let iterable: OptionalArg<PyObjectRef> = args.bind(vm)?;
223
224 // Optimizations for exact tuple type
225 if cls.is(vm.ctx.types.tuple_type) {
226 // Return exact tuple as-is
227 if let OptionalArg::Present(ref input) = iterable
228 && let Ok(tuple) = input.clone().downcast_exact::<PyTuple>(vm)
229 {
230 return Ok(tuple.into_pyref().into());
231 }
232
233 // Return empty tuple singleton
234 if iterable.is_missing() {
235 return Ok(vm.ctx.empty_tuple.clone().into());
236 }
237 }
238
239 let elements = if let OptionalArg::Present(iterable) = iterable {
240 iterable.try_to_value(vm)?
241 } else {
242 vec![]
243 };
244
245 // Return empty tuple singleton for exact tuple types (when iterable was empty)
246 if elements.is_empty() && cls.is(vm.ctx.types.tuple_type) {
247 return Ok(vm.ctx.empty_tuple.clone().into());
248 }
249
250 let payload = Self::py_new(&cls, elements, vm)?;
251 payload.into_ref_with_type(vm, cls).map(Into::into)
252 }
253
254 fn py_new(_cls: &Py<PyType>, elements: Self::Args, _vm: &VirtualMachine) -> PyResult<Self> {
255 Ok(Self {

Callers

nothing calls this directly

Calls 8

isMethod · 0.80
into_pyrefMethod · 0.80
try_to_valueMethod · 0.80
into_ref_with_typeMethod · 0.80
bindMethod · 0.45
cloneMethod · 0.45
is_emptyMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected