Fix new bytes with overflow error

This commit is contained in:
Jeong YunWon committed 2020-04-11 10:56:24 +09:00
1 parent c7467e444d
commit aa36a944bf
2 files changed
+13 -4

No files matched your search

-1
View File
@@ -157,7 +157,6 @@ class BaseBytesTest:
self.assertEqual(self.type2test(B(b"foobar")), b"foobar")
@unittest.skip("TODO: RUSTPYTHON")
def test_from_ssize(self):
self.assertEqual(self.type2test(0), b'')
self.assertEqual(self.type2test(1), b'\x00')
+13 -3
View File
@@ -82,9 +82,19 @@ impl ByteInnerNewOptions {
let value = if let OptionalArg::Present(ival) = self.val_option {
match_class!(match ival.clone() {
i @ PyInt => {
let size = objint::get_value(&i.into_object())
.to_usize()
.ok_or_else(|| vm.new_value_error("negative count".to_owned()))?;
let size =
objint::get_value(&i.into_object())
.to_isize()
.ok_or_else(|| {
vm.new_overflow_error(
"cannot fit 'int' into an index-sized integer".to_owned(),
)
})?;
let size = if size < 0 {
return Err(vm.new_value_error("negative count".to_owned()));
} else {
size as usize
};
Ok(vec![0; size])
}
_l @ PyString => {