From 166e69e731ccddb22794f8688a006111708a9a58 Mon Sep 17 00:00:00 2001 From: jfh Date: Sat, 11 Sep 2021 16:29:06 +0300 Subject: [PATCH] Use associated constants instead of module constants. --- common/src/float_ops.rs | 2 +- vm/src/anystr.rs | 4 +-- vm/src/builtins/pystr.rs | 2 +- vm/src/bytesinner.rs | 4 +-- vm/src/stdlib/cmath.rs | 4 +-- vm/src/stdlib/math.rs | 8 ++--- vm/src/stdlib/sre.rs | 68 ++++++++++++++++++---------------------- vm/src/sysmodule.rs | 2 +- 8 files changed, 44 insertions(+), 50 deletions(-) diff --git a/common/src/float_ops.rs b/common/src/float_ops.rs index ea99cb8098..5287076680 100644 --- a/common/src/float_ops.rs +++ b/common/src/float_ops.rs @@ -97,7 +97,7 @@ pub fn parse_str(literal: &str) -> Option { } pub fn is_integer(v: f64) -> bool { - (v - v.round()).abs() < std::f64::EPSILON + (v - v.round()).abs() < f64::EPSILON } #[derive(Debug)] diff --git a/vm/src/anystr.rs b/vm/src/anystr.rs index ba2652109b..43e89a6e10 100644 --- a/vm/src/anystr.rs +++ b/vm/src/anystr.rs @@ -69,9 +69,9 @@ fn saturate_to_isize(py_int: PyIntRef) -> isize { let big = py_int.as_bigint(); big.to_isize().unwrap_or_else(|| { if big.is_negative() { - std::isize::MIN + isize::MIN } else { - std::isize::MAX + isize::MAX } }) } diff --git a/vm/src/builtins/pystr.rs b/vm/src/builtins/pystr.rs index 94f57ce21e..5b376500b1 100644 --- a/vm/src/builtins/pystr.rs +++ b/vm/src/builtins/pystr.rs @@ -394,7 +394,7 @@ impl PyStr { ch if (ch as u32) < 0x10000 => 6, // \uHHHH _ => 10, // \uHHHHHHHH }; - if out_len > (std::isize::MAX as usize) - incr { + if out_len > (isize::MAX 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/bytesinner.rs b/vm/src/bytesinner.rs index 6408afdcdd..6790e1b61a 100644 --- a/vm/src/bytesinner.rs +++ b/vm/src/bytesinner.rs @@ -832,7 +832,7 @@ impl PyBytesInner { return self.elements.clone(); }; - let mut count = maxcount.unwrap_or(std::usize::MAX) - 1; + let mut count = maxcount.unwrap_or(usize::MAX) - 1; for offset in iter { new[offset..offset + len].clone_from_slice(to.elements.as_slice()); count -= 1; @@ -860,7 +860,7 @@ impl PyBytesInner { // result_len = self_len + count * (to_len-from_len) debug_assert!(count > 0); if to.len() as isize - from.len() as isize - > (std::isize::MAX - self.elements.len() as isize) / count as isize + > (isize::MAX - self.elements.len() as isize) / count as isize { return Err(vm.new_overflow_error("replace bytes is too long".to_owned())); } diff --git a/vm/src/stdlib/cmath.rs b/vm/src/stdlib/cmath.rs index 5de8b5eb66..a860856999 100644 --- a/vm/src/stdlib/cmath.rs +++ b/vm/src/stdlib/cmath.rs @@ -10,9 +10,9 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef { "pi" => ctx.new_float(std::f64::consts::PI), "e" => ctx.new_float(std::f64::consts::E), "tau" => ctx.new_float(2.0 * std::f64::consts::PI), - "inf" => ctx.new_float(std::f64::INFINITY), + "inf" => ctx.new_float(f64::INFINITY), "infj" => ctx.new_complex(num_complex::Complex64::new(0., std::f64::INFINITY)), - "nan" => ctx.new_float(std::f64::NAN), + "nan" => ctx.new_float(f64::NAN), "nanj" => ctx.new_complex(num_complex::Complex64::new(0., std::f64::NAN)), }); diff --git a/vm/src/stdlib/math.rs b/vm/src/stdlib/math.rs index bc5da2f98f..00597bed12 100644 --- a/vm/src/stdlib/math.rs +++ b/vm/src/stdlib/math.rs @@ -327,7 +327,7 @@ fn math_gamma(x: IntoPyFloat) -> f64 { } else if x.is_nan() || x.is_sign_positive() { x } else { - std::f64::NAN + f64::NAN } } @@ -338,7 +338,7 @@ fn math_lgamma(x: IntoPyFloat) -> f64 { } else if x.is_nan() { x } else { - std::f64::INFINITY + f64::INFINITY } } @@ -822,7 +822,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { "pi" => ctx.new_float(std::f64::consts::PI), // 3.14159... "e" => ctx.new_float(std::f64::consts::E), // 2.71.. "tau" => ctx.new_float(2.0 * std::f64::consts::PI), - "inf" => ctx.new_float(std::f64::INFINITY), - "nan" => ctx.new_float(std::f64::NAN) + "inf" => ctx.new_float(f64::INFINITY), + "nan" => ctx.new_float(f64::NAN) }) } diff --git a/vm/src/stdlib/sre.rs b/vm/src/stdlib/sre.rs index 514cb879f0..31d9308388 100644 --- a/vm/src/stdlib/sre.rs +++ b/vm/src/stdlib/sre.rs @@ -97,7 +97,7 @@ mod _sre { string: PyObjectRef, #[pyarg(any, default = "0")] pos: usize, - #[pyarg(any, default = "std::isize::MAX as usize")] + #[pyarg(any, default = "isize::MAX as usize")] endpos: usize, } @@ -340,48 +340,42 @@ mod _sre { split_args: SplitArgs, vm: &VirtualMachine, ) -> PyResult { - zelf.with_state( - split_args.string.clone(), - 0, - std::usize::MAX, - vm, - |mut state| { - let mut splitlist: Vec = Vec::new(); + zelf.with_state(split_args.string.clone(), 0, usize::MAX, vm, |mut state| { + let mut splitlist: Vec = Vec::new(); - let mut n = 0; - let mut last = 0; - while split_args.maxsplit == 0 || n < split_args.maxsplit { - state = state.search(); - if !state.has_matched { - break; - } + let mut n = 0; + let mut last = 0; + while split_args.maxsplit == 0 || n < split_args.maxsplit { + state = state.search(); + if !state.has_matched { + break; + } - /* get segment before this match */ - splitlist.push(slice_drive(&state.string, last, state.start, vm)); + /* get segment before this match */ + splitlist.push(slice_drive(&state.string, last, state.start, vm)); - let m = Match::new(&state, zelf.clone(), split_args.string.clone()); + let m = Match::new(&state, zelf.clone(), split_args.string.clone()); - // add groups (if any) - for i in 1..zelf.groups + 1 { - splitlist.push( - m.get_slice(i, state.string, vm) - .unwrap_or_else(|| vm.ctx.none()), - ); - } - - n += 1; - state.must_advance = state.string_position == state.start; - last = state.string_position; - state.start = state.string_position; - state.reset(); + // add groups (if any) + for i in 1..zelf.groups + 1 { + splitlist.push( + m.get_slice(i, state.string, vm) + .unwrap_or_else(|| vm.ctx.none()), + ); } - // get segment following last match (even if empty) - splitlist.push(slice_drive(&state.string, last, state.string.count(), vm)); + n += 1; + state.must_advance = state.string_position == state.start; + last = state.string_position; + state.start = state.string_position; + state.reset(); + } - Ok(PyList::from(splitlist).into_ref(vm)) - }, - ) + // get segment following last match (even if empty) + splitlist.push(slice_drive(&state.string, last, state.string.count(), vm)); + + Ok(PyList::from(splitlist).into_ref(vm)) + }) } #[pymethod(magic)] @@ -477,7 +471,7 @@ mod _sre { } }; - zelf.with_state(string.clone(), 0, std::usize::MAX, vm, |mut state| { + zelf.with_state(string.clone(), 0, usize::MAX, vm, |mut state| { let mut sublist: Vec = Vec::new(); let mut n = 0; let mut last_pos = 0; diff --git a/vm/src/sysmodule.rs b/vm/src/sysmodule.rs index 7075c03a6a..8ddf516cff 100644 --- a/vm/src/sysmodule.rs +++ b/vm/src/sysmodule.rs @@ -14,7 +14,7 @@ use crate::{ /* * The magic sys module. */ -const MAXSIZE: usize = std::isize::MAX as usize; +const MAXSIZE: usize = isize::MAX as usize; const MAXUNICODE: u32 = std::char::MAX as u32; fn argv(vm: &VirtualMachine) -> PyObjectRef {