Merge pull request #2387 from carbotaniuman/magic-methods

use unsigned abs to avoid overflow
This commit is contained in:
Noah
2020-12-30 15:12:44 -06:00
committed by GitHub
2 changed files with 7 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
from testutils import assert_raises
import pickle
import sys
# new
assert bytearray([1, 2, 3])
@@ -753,4 +754,8 @@ b = pickle.loads(pickle.dumps(a, 4))
assert type(a) == type(b)
assert a.x == b.x
assert a.y == b.y
assert a == b
assert a == b
a = bytearray()
for i in range(-1, 2, 1):
assert_raises(IndexError, lambda: a[-sys.maxsize - i], _msg='bytearray index out of range')

View File

@@ -391,7 +391,7 @@ impl TryFromObject for SequenceIndex {
// Use PySliceableSequence::wrap_index for implementors
pub(crate) fn wrap_index(p: isize, len: usize) -> Option<usize> {
let neg = p.is_negative();
let p = p.abs().to_usize()?;
let p = p.wrapping_abs() as usize;
if neg {
len.checked_sub(p)
} else if p >= len {