Use sys::MAXSIZE when isize::MAX is used as a meaning of sys.maxsize

This commit is contained in:
Jeong YunWon
2021-10-06 05:16:01 +09:00
parent 397daaeea4
commit d105c2b2a5
8 changed files with 20 additions and 14 deletions

View File

@@ -11,6 +11,7 @@ use crate::{
AsMapping, Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp, SlotIterator,
Unhashable,
},
stdlib::sys,
utils::Either,
vm::{ReprGuard, VirtualMachine},
PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
@@ -282,7 +283,7 @@ impl PyList {
start = 0;
}
}
let mut stop = stop.into_option().unwrap_or(isize::MAX);
let mut stop = stop.into_option().unwrap_or(sys::MAXSIZE);
if stop < 0 {
stop += self.borrow_vec().len() as isize;
if stop < 0 {

View File

@@ -14,6 +14,7 @@ use crate::{
Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp, SlotConstructor,
SlotIterator,
},
stdlib::sys,
utils::Either,
IdProtocol, IntoPyObject, ItemProtocol, PyClassDef, PyClassImpl, PyComparisonValue, PyContext,
PyObjectRef, PyRef, PyResult, PyValue, TryIntoRef, TypeProtocol, VirtualMachine,
@@ -401,7 +402,7 @@ impl PyStr {
match self.kind {
PyStrKindData::Utf8(ref char_len) => {
let len = self.as_str().chars().count();
// len cannot be usize::MAX, since vec.capacity() < isize::MAX
// len cannot be usize::MAX, since vec.capacity() < sys.maxsize
char_len.store(len, atomic::Ordering::Relaxed);
len
}
@@ -477,7 +478,7 @@ impl PyStr {
ch if (ch as u32) < 0x10000 => 6, // \uHHHH
_ => 10, // \uHHHHHHHH
};
if out_len > (isize::MAX as usize) - incr {
if out_len > (sys::MAXSIZE as usize) - incr {
return Err(vm.new_overflow_error("string is too long to generate repr".to_owned()));
}
out_len += incr;

View File

@@ -9,6 +9,7 @@ use crate::{
AsMapping, Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp,
SlotConstructor, SlotIterator,
},
stdlib::sys,
utils::Either,
vm::{ReprGuard, VirtualMachine},
IdProtocol, IntoPyObject, PyArithmeticValue, PyClassDef, PyClassImpl, PyComparisonValue,
@@ -238,7 +239,7 @@ impl PyTuple {
start = 0;
}
}
let mut stop = stop.into_option().unwrap_or(isize::MAX);
let mut stop = stop.into_option().unwrap_or(sys::MAXSIZE);
if stop < 0 {
stop += self.as_slice().len() as isize;
if stop < 0 {

View File

@@ -16,6 +16,7 @@ mod _collections {
Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp, SlotConstructor,
SlotIterator, Unhashable,
},
stdlib::sys,
vm::ReprGuard,
PyComparisonValue, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
};
@@ -95,8 +96,8 @@ mod _collections {
let maxlen = value.as_bigint().to_usize().ok_or_else(|| {
vm.new_value_error("maxlen must be non-negative.".to_owned())
})?;
// Only succeeds for values for which 0 <= value <= isize::MAX
if maxlen > isize::MAX as usize {
// Only succeeds for values for which 0 <= value <= sys.maxsize
if maxlen > sys::MAXSIZE as usize {
return Err(vm.new_overflow_error(
"Python int too large to convert to Rust isize.".to_owned(),
));

View File

@@ -11,6 +11,7 @@ mod decl {
function::{ArgCallable, FuncArgs, OptionalArg, OptionalOption, PosArgs},
protocol::{PyIter, PyIterReturn},
slots::{IteratorIterable, SlotConstructor, SlotIterator},
stdlib::sys,
IdProtocol, IntoPyObject, PyObjectRef, PyRef, PyResult, PyValue, PyWeakRef, TypeProtocol,
VirtualMachine,
};
@@ -269,7 +270,7 @@ mod decl {
let times = match times.into_option() {
Some(int) => {
let val = int.as_bigint();
if *val > BigInt::from(isize::MAX) {
if *val > BigInt::from(sys::MAXSIZE) {
return Err(vm.new_overflow_error("Cannot fit in isize.".to_owned()));
}
// times always >= 0.
@@ -705,7 +706,7 @@ mod decl {
step: usize,
}
// Restrict obj to ints with value 0 <= val <= sys.maxsize (isize::MAX).
// Restrict obj to ints with value 0 <= val <= sys.maxsize
// On failure (out of range, non-int object) a ValueError is raised.
fn pyobject_to_opt_usize(
obj: PyObjectRef,
@@ -716,13 +717,13 @@ mod decl {
if is_int {
let value = int::get_value(&obj).to_usize();
if let Some(value) = value {
// Only succeeds for values for which 0 <= value <= isize::MAX
if value <= isize::MAX as usize {
// Only succeeds for values for which 0 <= value <= sys.maxsize
if value <= sys::MAXSIZE as usize {
return Ok(value);
}
}
}
// We don't have an int or value was < 0 or > maxsize (isize::MAX)
// We don't have an int or value was < 0 or > sys.maxsize
return Err(vm.new_value_error(format!(
"{} argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.",
name

View File

@@ -10,6 +10,7 @@ mod _sre {
function::{ArgCallable, OptionalArg, PosArgs},
protocol::PyBuffer,
slots::{Comparable, Hashable},
stdlib::sys,
IntoPyObject, ItemProtocol, PyComparisonValue, PyObjectRef, PyRef, PyResult, PyValue,
TryFromBorrowedObject, TryFromObject, VirtualMachine,
};
@@ -95,7 +96,7 @@ mod _sre {
string: PyObjectRef,
#[pyarg(any, default = "0")]
pos: usize,
#[pyarg(any, default = "isize::MAX as usize")]
#[pyarg(any, default = "sys::MAXSIZE as usize")]
endpos: usize,
}

View File

@@ -19,7 +19,7 @@ use std::{env, mem, path};
/*
* The magic sys module.
*/
const MAXSIZE: usize = isize::MAX as usize;
pub(crate) const MAXSIZE: isize = isize::MAX;
const MAXUNICODE: u32 = std::char::MAX as u32;
fn argv(vm: &VirtualMachine) -> PyObjectRef {

View File

@@ -1986,7 +1986,7 @@ impl VirtualMachine {
/// index as a usize for sequences to be able to use immediately.
pub fn check_repeat_or_memory_error(&self, length: usize, n: isize) -> PyResult<usize> {
let n = n.to_usize().unwrap_or(0);
if n > 0 && length > isize::MAX as usize / n {
if n > 0 && length > stdlib::sys::MAXSIZE as usize / n {
// Empty message is currently used in CPython.
Err(self.new_memory_error("".to_owned()))
} else {