Skip to content

Commit 1e2d986

Browse files
committed
Fix bytes constructor
1 parent f0e7427 commit 1e2d986

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

Lib/test/test_bytes.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,6 @@ def test_constructor_overflow(self):
209209
except (OverflowError, MemoryError):
210210
pass
211211

212-
# TODO: RUSTPYTHON
213-
@unittest.expectedFailure
214212
def test_constructor_exceptions(self):
215213
# Issue #34974: bytes and bytearray constructors replace unexpected
216214
# exceptions.

vm/src/bytes_inner.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,35 +118,44 @@ impl ByteInnerNewOptions {
118118

119119
match (self.source, self.encoding, self.errors) {
120120
(OptionalArg::Present(obj), OptionalArg::Missing, OptionalArg::Missing) => {
121-
match_class!(match obj {
122-
i @ PyInt => {
123-
Ok(Self::get_value_from_size(i, vm)?)
121+
// Try __index__ first to handle int-like objects that might raise custom exceptions
122+
if let Some(index_result) = obj.try_index_opt(vm) {
123+
match index_result {
124+
Ok(index) => Self::get_value_from_size(index, vm),
125+
Err(e) => Err(e), // Propagate the original exception (e.g., ZeroDivisionError)
124126
}
125-
_s @ PyStr => Err(STRING_WITHOUT_ENCODING),
126-
obj => {
127-
Ok(Self::get_value_from_source(obj, vm)?)
128-
}
129-
})
127+
} else {
128+
match_class!(match obj {
129+
i @ PyInt => {
130+
Self::get_value_from_size(i, vm)
131+
}
132+
_s @ PyStr => Err(vm.new_type_error(STRING_WITHOUT_ENCODING.to_owned())),
133+
obj => {
134+
Self::get_value_from_source(obj, vm)
135+
}
136+
})
137+
}
130138
}
131139
(OptionalArg::Present(obj), OptionalArg::Present(encoding), errors) => {
132140
if let Ok(s) = obj.downcast::<PyStr>() {
133-
Ok(Self::get_value_from_string(s, encoding, errors, vm)?)
141+
Self::get_value_from_string(s, encoding, errors, vm)
134142
} else {
135-
Err(ENCODING_WITHOUT_STRING)
143+
Err(vm.new_type_error(ENCODING_WITHOUT_STRING.to_owned()))
136144
}
137145
}
138146
(OptionalArg::Missing, OptionalArg::Missing, OptionalArg::Missing) => {
139147
Ok(PyBytesInner::default())
140148
}
141-
(OptionalArg::Missing, OptionalArg::Present(_), _) => Err(ENCODING_WITHOUT_STRING),
149+
(OptionalArg::Missing, OptionalArg::Present(_), _) => {
150+
Err(vm.new_type_error(ENCODING_WITHOUT_STRING.to_owned()))
151+
}
142152
(OptionalArg::Missing, _, OptionalArg::Present(_)) => {
143-
Err("errors without a string argument")
153+
Err(vm.new_type_error("errors without a string argument".to_owned()))
144154
}
145155
(OptionalArg::Present(_), OptionalArg::Missing, OptionalArg::Present(_)) => {
146-
Err(STRING_WITHOUT_ENCODING)
156+
Err(vm.new_type_error(STRING_WITHOUT_ENCODING.to_owned()))
147157
}
148158
}
149-
.map_err(|e| vm.new_type_error(e.to_owned()))
150159
}
151160
}
152161

0 commit comments

Comments
 (0)