From d105c2b2a59252ec841fc74833b5260a70bc5f5a Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Wed, 6 Oct 2021 05:16:01 +0900 Subject: [PATCH] Use sys::MAXSIZE when isize::MAX is used as a meaning of sys.maxsize --- vm/src/builtins/list.rs | 3 ++- vm/src/builtins/pystr.rs | 5 +++-- vm/src/builtins/tuple.rs | 3 ++- vm/src/stdlib/collections.rs | 5 +++-- vm/src/stdlib/itertools.rs | 11 ++++++----- vm/src/stdlib/sre.rs | 3 ++- vm/src/stdlib/sys.rs | 2 +- vm/src/vm.rs | 2 +- 8 files changed, 20 insertions(+), 14 deletions(-) diff --git a/vm/src/builtins/list.rs b/vm/src/builtins/list.rs index c417ac5985..e173299cf7 100644 --- a/vm/src/builtins/list.rs +++ b/vm/src/builtins/list.rs @@ -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 { diff --git a/vm/src/builtins/pystr.rs b/vm/src/builtins/pystr.rs index 26c1d45f3c..af7fa521bd 100644 --- a/vm/src/builtins/pystr.rs +++ b/vm/src/builtins/pystr.rs @@ -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; diff --git a/vm/src/builtins/tuple.rs b/vm/src/builtins/tuple.rs index 8b5f3ba99e..77bf3143ec 100644 --- a/vm/src/builtins/tuple.rs +++ b/vm/src/builtins/tuple.rs @@ -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 { diff --git a/vm/src/stdlib/collections.rs b/vm/src/stdlib/collections.rs index 9dc49cff83..7058d652ae 100644 --- a/vm/src/stdlib/collections.rs +++ b/vm/src/stdlib/collections.rs @@ -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(), )); diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 11ccbf540c..7d04824e7b 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -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 diff --git a/vm/src/stdlib/sre.rs b/vm/src/stdlib/sre.rs index 64bc77baf6..2b30c8b439 100644 --- a/vm/src/stdlib/sre.rs +++ b/vm/src/stdlib/sre.rs @@ -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, } diff --git a/vm/src/stdlib/sys.rs b/vm/src/stdlib/sys.rs index 9c0e59bf76..e7c1898776 100644 --- a/vm/src/stdlib/sys.rs +++ b/vm/src/stdlib/sys.rs @@ -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 { diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 8cc270c03b..a36ae02373 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -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 { 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 {