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

Method slot_new

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

Source from the content-addressed store, hash-verified

95 type Args = Vec<u8>;
96
97 fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
98 let options: ByteInnerNewOptions = args.bind(vm)?;
99
100 // Optimizations for exact bytes type
101 if cls.is(vm.ctx.types.bytes_type) {
102 // Return empty bytes singleton
103 if options.source.is_missing()
104 && options.encoding.is_missing()
105 && options.errors.is_missing()
106 {
107 return Ok(vm.ctx.empty_bytes.clone().into());
108 }
109
110 // Return exact bytes as-is
111 if let OptionalArg::Present(ref obj) = options.source
112 && options.encoding.is_missing()
113 && options.errors.is_missing()
114 && let Ok(b) = obj.clone().downcast_exact::<PyBytes>(vm)
115 {
116 return Ok(b.into_pyref().into());
117 }
118 }
119
120 // Handle __bytes__ method - may return PyBytes directly
121 if let OptionalArg::Present(ref obj) = options.source
122 && options.encoding.is_missing()
123 && options.errors.is_missing()
124 && let Some(bytes_method) = vm.get_method(obj.clone(), identifier!(vm, __bytes__))
125 {
126 let bytes = bytes_method?.call((), vm)?;
127 // If exact bytes type and __bytes__ returns bytes, use it directly
128 if cls.is(vm.ctx.types.bytes_type)
129 && let Ok(b) = bytes.clone().downcast::<PyBytes>()
130 {
131 return Ok(b.into());
132 }
133 // Otherwise convert to Vec<u8>
134 let inner = PyBytesInner::try_from_borrowed_object(vm, &bytes)?;
135 let payload = Self::py_new(&cls, inner.elements, vm)?;
136 return payload.into_ref_with_type(vm, cls).map(Into::into);
137 }
138
139 // Fallback to get_bytearray_inner
140 let elements = options.get_bytearray_inner(vm)?.elements;
141
142 // Return empty bytes singleton for exact bytes types
143 if elements.is_empty() && cls.is(vm.ctx.types.bytes_type) {
144 return Ok(vm.ctx.empty_bytes.clone().into());
145 }
146
147 let payload = Self::py_new(&cls, elements, vm)?;
148 payload.into_ref_with_type(vm, cls).map(Into::into)
149 }
150
151 fn py_new(_cls: &Py<PyType>, elements: Self::Args, _vm: &VirtualMachine) -> PyResult<Self> {
152 Ok(Self::from(elements))

Callers

nothing calls this directly

Calls 10

isMethod · 0.80
into_pyrefMethod · 0.80
into_ref_with_typeMethod · 0.80
get_bytearray_innerMethod · 0.80
bindMethod · 0.45
cloneMethod · 0.45
get_methodMethod · 0.45
callMethod · 0.45
mapMethod · 0.45
is_emptyMethod · 0.45

Tested by

no test coverage detected