Fix panic when using negative int for bytes()

Fixes #1401
This commit is contained in:
HyeonGyu Lee (Vazrupe) committed 2019-09-26 13:24:00 +09:00
1 parent 51c3f71eed
commit 00823dd923
3 files changed
+10 -1

No files matched your search

Generated
+1
View File
@@ -1221,6 +1221,7 @@ dependencies = [
"cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)",
"js-sys 0.3.27 (registry+https://github.com/rust-lang/crates.io-index)",
"num-bigint 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"rustpython-compiler 0.1.0",
"rustpython-parser 0.1.0",
+6
View File
@@ -1,3 +1,6 @@
from testutils import assert_raises
# Spec: https://docs.python.org/2/library/types.html
print(None)
# TypeType
@@ -36,6 +39,9 @@ try:
except TypeError:
pass
with assert_raises(ValueError):
bytes(-1)
a = bytearray([1, 2, 3])
# assert a[1] == 2
+3 -1
View File
@@ -117,7 +117,9 @@ 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().unwrap();
let size = objint::get_value(&i.into_object())
.to_usize()
.ok_or_else(|| vm.new_value_error("negative count".to_string()))?;
Ok(vec![0; size])
}
_l @ PyString => {