mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Merge pull request #3038 from DimitrisJim/use_associated_consts
Use associated constants instead of module constants.
This commit is contained in:
@@ -97,7 +97,7 @@ pub fn parse_str(literal: &str) -> Option<f64> {
|
||||
}
|
||||
|
||||
pub fn is_integer(v: f64) -> bool {
|
||||
(v - v.round()).abs() < std::f64::EPSILON
|
||||
(v - v.round()).abs() < f64::EPSILON
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
||||
@@ -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
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
|
||||
@@ -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)),
|
||||
});
|
||||
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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<PyListRef> {
|
||||
zelf.with_state(
|
||||
split_args.string.clone(),
|
||||
0,
|
||||
std::usize::MAX,
|
||||
vm,
|
||||
|mut state| {
|
||||
let mut splitlist: Vec<PyObjectRef> = Vec::new();
|
||||
zelf.with_state(split_args.string.clone(), 0, usize::MAX, vm, |mut state| {
|
||||
let mut splitlist: Vec<PyObjectRef> = 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;
|
||||
}
|
||||
|
||||
/* 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());
|
||||
|
||||
// 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();
|
||||
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 following last match (even if empty)
|
||||
splitlist.push(slice_drive(&state.string, last, state.string.count(), vm));
|
||||
/* get segment before this match */
|
||||
splitlist.push(slice_drive(&state.string, last, state.start, vm));
|
||||
|
||||
Ok(PyList::from(splitlist).into_ref(vm))
|
||||
},
|
||||
)
|
||||
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();
|
||||
}
|
||||
|
||||
// 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<PyObjectRef> = Vec::new();
|
||||
let mut n = 0;
|
||||
let mut last_pos = 0;
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user