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

Method get_bytearray_inner

crates/vm/src/bytes_inner.rs:93–136  ·  view source on GitHub ↗
(self, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

91 }
92
93 pub fn get_bytearray_inner(self, vm: &VirtualMachine) -> PyResult<PyBytesInner> {
94 match (self.source, self.encoding, self.errors) {
95 (OptionalArg::Present(obj), OptionalArg::Missing, OptionalArg::Missing) => {
96 // Try __index__ first to handle int-like objects that might raise custom exceptions
97 if let Some(index_result) = obj.try_index_opt(vm) {
98 match index_result {
99 Ok(index) => Self::get_value_from_size(index, vm),
100 Err(e) => {
101 // Only propagate non-TypeError exceptions
102 // TypeError means the object doesn't support __index__, so fall back
103 if e.fast_isinstance(vm.ctx.exceptions.type_error) {
104 // Fall back to treating as buffer-like object
105 Self::handle_object_fallback(obj, vm)
106 } else {
107 // Propagate other exceptions (e.g., ZeroDivisionError)
108 Err(e)
109 }
110 }
111 }
112 } else {
113 Self::handle_object_fallback(obj, vm)
114 }
115 }
116 (OptionalArg::Present(obj), OptionalArg::Present(encoding), errors) => {
117 if let Ok(s) = obj.downcast::<PyStr>() {
118 Self::get_value_from_string(s, encoding, errors, vm)
119 } else {
120 Err(vm.new_type_error(ENCODING_WITHOUT_STRING.to_owned()))
121 }
122 }
123 (OptionalArg::Missing, OptionalArg::Missing, OptionalArg::Missing) => {
124 Ok(PyBytesInner::default())
125 }
126 (OptionalArg::Missing, OptionalArg::Present(_), _) => {
127 Err(vm.new_type_error(ENCODING_WITHOUT_STRING.to_owned()))
128 }
129 (OptionalArg::Missing, _, OptionalArg::Present(_)) => {
130 Err(vm.new_type_error("errors without a string argument"))
131 }
132 (OptionalArg::Present(_), OptionalArg::Missing, OptionalArg::Present(_)) => {
133 Err(vm.new_type_error(STRING_WITHOUT_ENCODING.to_owned()))
134 }
135 }
136 }
137}
138
139#[derive(FromArgs)]

Callers 2

initMethod · 0.80
slot_newMethod · 0.80

Calls 4

try_index_optMethod · 0.80
fast_isinstanceMethod · 0.80
ErrClass · 0.50
to_ownedMethod · 0.45

Tested by

no test coverage detected