mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Remove wrong Deref (#6620)
This commit is contained in:
@@ -565,11 +565,11 @@ mod array {
|
||||
}
|
||||
|
||||
fn f32_try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<f32> {
|
||||
ArgIntoFloat::try_from_object(vm, obj).map(|x| *x as f32)
|
||||
ArgIntoFloat::try_from_object(vm, obj).map(|x| x.into_float() as f32)
|
||||
}
|
||||
|
||||
fn f64_try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<f64> {
|
||||
ArgIntoFloat::try_from_object(vm, obj).map(Into::into)
|
||||
ArgIntoFloat::try_from_object(vm, obj).map(|x| x.into_float())
|
||||
}
|
||||
|
||||
fn pyfloat_from_f32(value: f32) -> PyFloat {
|
||||
|
||||
@@ -24,7 +24,7 @@ mod _bisect {
|
||||
#[inline]
|
||||
fn handle_default(arg: OptionalArg<ArgIndex>, vm: &VirtualMachine) -> PyResult<Option<isize>> {
|
||||
arg.into_option()
|
||||
.map(|v| v.try_to_primitive(vm))
|
||||
.map(|v| v.into_int_ref().try_to_primitive(vm))
|
||||
.transpose()
|
||||
}
|
||||
|
||||
|
||||
@@ -23,64 +23,64 @@ mod cmath {
|
||||
|
||||
#[pyfunction]
|
||||
fn phase(z: ArgIntoComplex) -> f64 {
|
||||
z.arg()
|
||||
z.into_complex().arg()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn polar(x: ArgIntoComplex) -> (f64, f64) {
|
||||
x.to_polar()
|
||||
x.into_complex().to_polar()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn rect(r: ArgIntoFloat, phi: ArgIntoFloat) -> Complex64 {
|
||||
Complex64::from_polar(*r, *phi)
|
||||
Complex64::from_polar(r.into_float(), phi.into_float())
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn isinf(z: ArgIntoComplex) -> bool {
|
||||
let Complex64 { re, im } = *z;
|
||||
let Complex64 { re, im } = z.into_complex();
|
||||
re.is_infinite() || im.is_infinite()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn isfinite(z: ArgIntoComplex) -> bool {
|
||||
z.is_finite()
|
||||
z.into_complex().is_finite()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn isnan(z: ArgIntoComplex) -> bool {
|
||||
z.is_nan()
|
||||
z.into_complex().is_nan()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn exp(z: ArgIntoComplex, vm: &VirtualMachine) -> PyResult<Complex64> {
|
||||
let z = *z;
|
||||
let z = z.into_complex();
|
||||
result_or_overflow(z, z.exp(), vm)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn sqrt(z: ArgIntoComplex) -> Complex64 {
|
||||
z.sqrt()
|
||||
z.into_complex().sqrt()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn sin(z: ArgIntoComplex) -> Complex64 {
|
||||
z.sin()
|
||||
z.into_complex().sin()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn asin(z: ArgIntoComplex) -> Complex64 {
|
||||
z.asin()
|
||||
z.into_complex().asin()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn cos(z: ArgIntoComplex) -> Complex64 {
|
||||
z.cos()
|
||||
z.into_complex().cos()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn acos(z: ArgIntoComplex) -> Complex64 {
|
||||
z.acos()
|
||||
z.into_complex().acos()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
@@ -90,56 +90,56 @@ mod cmath {
|
||||
// which returns NaN when base is negative.
|
||||
// log10(z) / log10(base) yields correct results but division
|
||||
// doesn't handle pos/neg zero nicely. (i.e log(1, 0.5))
|
||||
z.log(
|
||||
z.into_complex().log(
|
||||
base.into_option()
|
||||
.map(|base| base.re)
|
||||
.map(|base| base.into_complex().re)
|
||||
.unwrap_or(core::f64::consts::E),
|
||||
)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn log10(z: ArgIntoComplex) -> Complex64 {
|
||||
z.log(10.0)
|
||||
z.into_complex().log(10.0)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn acosh(z: ArgIntoComplex) -> Complex64 {
|
||||
z.acosh()
|
||||
z.into_complex().acosh()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn atan(z: ArgIntoComplex) -> Complex64 {
|
||||
z.atan()
|
||||
z.into_complex().atan()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn atanh(z: ArgIntoComplex) -> Complex64 {
|
||||
z.atanh()
|
||||
z.into_complex().atanh()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn tan(z: ArgIntoComplex) -> Complex64 {
|
||||
z.tan()
|
||||
z.into_complex().tan()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn tanh(z: ArgIntoComplex) -> Complex64 {
|
||||
z.tanh()
|
||||
z.into_complex().tanh()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn sinh(z: ArgIntoComplex) -> Complex64 {
|
||||
z.sinh()
|
||||
z.into_complex().sinh()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn cosh(z: ArgIntoComplex) -> Complex64 {
|
||||
z.cosh()
|
||||
z.into_complex().cosh()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn asinh(z: ArgIntoComplex) -> Complex64 {
|
||||
z.asinh()
|
||||
z.into_complex().asinh()
|
||||
}
|
||||
|
||||
#[derive(FromArgs)]
|
||||
@@ -156,10 +156,10 @@ mod cmath {
|
||||
|
||||
#[pyfunction]
|
||||
fn isclose(args: IsCloseArgs, vm: &VirtualMachine) -> PyResult<bool> {
|
||||
let a = *args.a;
|
||||
let b = *args.b;
|
||||
let rel_tol = args.rel_tol.map_or(1e-09, Into::into);
|
||||
let abs_tol = args.abs_tol.map_or(0.0, Into::into);
|
||||
let a = args.a.into_complex();
|
||||
let b = args.b.into_complex();
|
||||
let rel_tol = args.rel_tol.map_or(1e-09, |v| v.into_float());
|
||||
let abs_tol = args.abs_tol.map_or(0.0, |v| v.into_float());
|
||||
|
||||
if rel_tol < 0.0 || abs_tol < 0.0 {
|
||||
return Err(vm.new_value_error("tolerances must be non-negative"));
|
||||
|
||||
@@ -29,7 +29,7 @@ mod math {
|
||||
// Helper macro:
|
||||
macro_rules! call_math_func {
|
||||
( $fun:ident, $name:ident, $vm:ident ) => {{
|
||||
let value = *$name;
|
||||
let value = $name.into_float();
|
||||
let result = value.$fun();
|
||||
result_or_overflow(value, result, $vm)
|
||||
}};
|
||||
@@ -54,17 +54,17 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn isfinite(x: ArgIntoFloat) -> bool {
|
||||
x.is_finite()
|
||||
x.into_float().is_finite()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn isinf(x: ArgIntoFloat) -> bool {
|
||||
x.is_infinite()
|
||||
x.into_float().is_infinite()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn isnan(x: ArgIntoFloat) -> bool {
|
||||
x.is_nan()
|
||||
x.into_float().is_nan()
|
||||
}
|
||||
|
||||
#[derive(FromArgs)]
|
||||
@@ -81,10 +81,10 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn isclose(args: IsCloseArgs, vm: &VirtualMachine) -> PyResult<bool> {
|
||||
let a = *args.a;
|
||||
let b = *args.b;
|
||||
let rel_tol = args.rel_tol.map_or(1e-09, |value| value.into());
|
||||
let abs_tol = args.abs_tol.map_or(0.0, |value| value.into());
|
||||
let a = args.a.into_float();
|
||||
let b = args.b.into_float();
|
||||
let rel_tol = args.rel_tol.map_or(1e-09, |v| v.into_float());
|
||||
let abs_tol = args.abs_tol.map_or(0.0, |v| v.into_float());
|
||||
|
||||
if rel_tol < 0.0 || abs_tol < 0.0 {
|
||||
return Err(vm.new_value_error("tolerances must be non-negative"));
|
||||
@@ -115,7 +115,7 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn copysign(x: ArgIntoFloat, y: ArgIntoFloat) -> f64 {
|
||||
x.copysign(*y)
|
||||
x.into_float().copysign(y.into_float())
|
||||
}
|
||||
|
||||
// Power and logarithmic functions:
|
||||
@@ -136,7 +136,7 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn log(x: PyObjectRef, base: OptionalArg<ArgIntoFloat>, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
let base = base.map(|b| *b).unwrap_or(core::f64::consts::E);
|
||||
let base: f64 = base.map(Into::into).unwrap_or(core::f64::consts::E);
|
||||
if base.is_sign_negative() {
|
||||
return Err(vm.new_value_error("math domain error"));
|
||||
}
|
||||
@@ -145,7 +145,7 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn log1p(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
let x = *x;
|
||||
let x = x.into_float();
|
||||
if x.is_nan() || x > -1.0_f64 {
|
||||
Ok(x.ln_1p())
|
||||
} else {
|
||||
@@ -197,8 +197,8 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn pow(x: ArgIntoFloat, y: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
let x = *x;
|
||||
let y = *y;
|
||||
let x = x.into_float();
|
||||
let y = y.into_float();
|
||||
|
||||
if x < 0.0 && x.is_finite() && y.fract() != 0.0 && y.is_finite()
|
||||
|| x == 0.0 && y < 0.0 && y != f64::NEG_INFINITY
|
||||
@@ -217,7 +217,7 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn sqrt(value: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
let value = *value;
|
||||
let value = value.into_float();
|
||||
if value.is_nan() {
|
||||
return Ok(value);
|
||||
}
|
||||
@@ -232,6 +232,7 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn isqrt(x: ArgIndex, vm: &VirtualMachine) -> PyResult<BigInt> {
|
||||
let x = x.into_int_ref();
|
||||
let value = x.as_bigint();
|
||||
|
||||
if value.is_negative() {
|
||||
@@ -243,7 +244,7 @@ mod math {
|
||||
// Trigonometric functions:
|
||||
#[pyfunction]
|
||||
fn acos(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
let x = *x;
|
||||
let x = x.into_float();
|
||||
if x.is_nan() || (-1.0_f64..=1.0_f64).contains(&x) {
|
||||
Ok(x.acos())
|
||||
} else {
|
||||
@@ -253,7 +254,7 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn asin(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
let x = *x;
|
||||
let x = x.into_float();
|
||||
if x.is_nan() || (-1.0_f64..=1.0_f64).contains(&x) {
|
||||
Ok(x.asin())
|
||||
} else {
|
||||
@@ -268,15 +269,16 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn atan2(y: ArgIntoFloat, x: ArgIntoFloat) -> f64 {
|
||||
y.atan2(*x)
|
||||
y.into_float().atan2(x.into())
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn cos(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
let x = x.into_float();
|
||||
if x.is_infinite() {
|
||||
return Err(vm.new_value_error("math domain error"));
|
||||
}
|
||||
call_math_func!(cos, x, vm)
|
||||
result_or_overflow(x, x.cos(), vm)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
@@ -408,35 +410,37 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn sin(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
let x = x.into_float();
|
||||
if x.is_infinite() {
|
||||
return Err(vm.new_value_error("math domain error"));
|
||||
}
|
||||
call_math_func!(sin, x, vm)
|
||||
result_or_overflow(x, x.sin(), vm)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn tan(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
let x = x.into_float();
|
||||
if x.is_infinite() {
|
||||
return Err(vm.new_value_error("math domain error"));
|
||||
}
|
||||
call_math_func!(tan, x, vm)
|
||||
result_or_overflow(x, x.tan(), vm)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn degrees(x: ArgIntoFloat) -> f64 {
|
||||
*x * (180.0 / core::f64::consts::PI)
|
||||
x.into_float() * (180.0 / core::f64::consts::PI)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn radians(x: ArgIntoFloat) -> f64 {
|
||||
*x * (core::f64::consts::PI / 180.0)
|
||||
x.into_float() * (core::f64::consts::PI / 180.0)
|
||||
}
|
||||
|
||||
// Hyperbolic functions:
|
||||
|
||||
#[pyfunction]
|
||||
fn acosh(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
let x = *x;
|
||||
let x = x.into_float();
|
||||
if x.is_sign_negative() || x.is_zero() {
|
||||
Err(vm.new_value_error("math domain error"))
|
||||
} else {
|
||||
@@ -451,7 +455,7 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn atanh(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
let x = *x;
|
||||
let x = x.into_float();
|
||||
if x >= 1.0_f64 || x <= -1.0_f64 {
|
||||
Err(vm.new_value_error("math domain error"))
|
||||
} else {
|
||||
@@ -477,22 +481,22 @@ mod math {
|
||||
// Special functions:
|
||||
#[pyfunction]
|
||||
fn erf(x: ArgIntoFloat) -> f64 {
|
||||
pymath::erf(*x)
|
||||
pymath::erf(x.into())
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn erfc(x: ArgIntoFloat) -> f64 {
|
||||
pymath::erfc(*x)
|
||||
pymath::erfc(x.into())
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn gamma(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
pymath::gamma(*x).map_err(|err| pymath_error_to_exception(err, vm))
|
||||
pymath::gamma(x.into()).map_err(|err| pymath_error_to_exception(err, vm))
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn lgamma(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
pymath::lgamma(*x).map_err(|err| pymath_error_to_exception(err, vm))
|
||||
pymath::lgamma(x.into()).map_err(|err| pymath_error_to_exception(err, vm))
|
||||
}
|
||||
|
||||
fn try_magic_method(
|
||||
@@ -541,7 +545,7 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn frexp(x: ArgIntoFloat) -> (f64, i32) {
|
||||
let value = *x;
|
||||
let value: f64 = x.into();
|
||||
if value.is_finite() {
|
||||
let (m, exp) = float_ops::decompose_float(value);
|
||||
(m * value.signum(), exp)
|
||||
@@ -641,12 +645,12 @@ mod math {
|
||||
if arg_vec.is_empty() {
|
||||
return default;
|
||||
} else if arg_vec.len() == 1 {
|
||||
return op(arg_vec[0].as_bigint(), &arg_vec[0]);
|
||||
return op(arg_vec[0].as_ref().as_bigint(), arg_vec[0].as_ref());
|
||||
}
|
||||
|
||||
let mut res = arg_vec[0].as_bigint().clone();
|
||||
let mut res = arg_vec[0].as_ref().as_bigint().clone();
|
||||
for num in &arg_vec[1..] {
|
||||
res = op(&res, num)
|
||||
res = op(&res, num.as_ref())
|
||||
}
|
||||
res
|
||||
}
|
||||
@@ -665,7 +669,7 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn cbrt(x: ArgIntoFloat) -> f64 {
|
||||
x.cbrt()
|
||||
x.into_float().cbrt()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
@@ -675,7 +679,7 @@ mod math {
|
||||
let mut inf_sum = 0.0;
|
||||
|
||||
for obj in seq.iter(vm)? {
|
||||
let mut x = *obj?;
|
||||
let mut x = obj?.into_float();
|
||||
|
||||
let xsave = x;
|
||||
let mut i = 0;
|
||||
@@ -790,11 +794,12 @@ mod math {
|
||||
k: OptionalArg<Option<ArgIndex>>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<BigInt> {
|
||||
let n = n.into_int_ref();
|
||||
let n = n.as_bigint();
|
||||
let k_ref;
|
||||
let v = match k.flatten() {
|
||||
Some(k) => {
|
||||
k_ref = k;
|
||||
k_ref = k.into_int_ref();
|
||||
k_ref.as_bigint()
|
||||
}
|
||||
None => n,
|
||||
@@ -818,7 +823,9 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn comb(n: ArgIndex, k: ArgIndex, vm: &VirtualMachine) -> PyResult<BigInt> {
|
||||
let k = k.into_int_ref();
|
||||
let mut k = k.as_bigint();
|
||||
let n = n.into_int_ref();
|
||||
let n = n.as_bigint();
|
||||
let one = BigInt::one();
|
||||
let zero = BigInt::zero();
|
||||
@@ -856,7 +863,7 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn modf(x: ArgIntoFloat) -> (f64, f64) {
|
||||
let x = *x;
|
||||
let x = x.into_float();
|
||||
if !x.is_finite() {
|
||||
if x.is_infinite() {
|
||||
return (0.0_f64.copysign(x), x);
|
||||
@@ -882,27 +889,25 @@ mod math {
|
||||
fn nextafter(arg: NextAfterArgs, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
let steps: Option<i64> = arg
|
||||
.steps
|
||||
.map(|v| v.try_to_primitive(vm))
|
||||
.map(|v| v.into_int_ref().try_to_primitive(vm))
|
||||
.transpose()?
|
||||
.into_option();
|
||||
let x: f64 = arg.x.into();
|
||||
let y: f64 = arg.y.into();
|
||||
match steps {
|
||||
Some(steps) => {
|
||||
if steps < 0 {
|
||||
return Err(vm.new_value_error("steps must be a non-negative integer"));
|
||||
}
|
||||
Ok(float_ops::nextafter_with_steps(
|
||||
*arg.x,
|
||||
*arg.y,
|
||||
steps as u64,
|
||||
))
|
||||
Ok(float_ops::nextafter_with_steps(x, y, steps as u64))
|
||||
}
|
||||
None => Ok(float_ops::nextafter(*arg.x, *arg.y)),
|
||||
None => Ok(float_ops::nextafter(x, y)),
|
||||
}
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn ulp(x: ArgIntoFloat) -> f64 {
|
||||
float_ops::ulp(*x)
|
||||
float_ops::ulp(x.into())
|
||||
}
|
||||
|
||||
fn fmod(x: f64, y: f64) -> f64 {
|
||||
@@ -915,8 +920,8 @@ mod math {
|
||||
|
||||
#[pyfunction(name = "fmod")]
|
||||
fn py_fmod(x: ArgIntoFloat, y: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
let x = *x;
|
||||
let y = *y;
|
||||
let x = x.into_float();
|
||||
let y = y.into_float();
|
||||
|
||||
let r = fmod(x, y);
|
||||
|
||||
@@ -929,8 +934,8 @@ mod math {
|
||||
|
||||
#[pyfunction]
|
||||
fn remainder(x: ArgIntoFloat, y: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
let x = *x;
|
||||
let y = *y;
|
||||
let x = x.into_float();
|
||||
let y = y.into_float();
|
||||
|
||||
if x.is_finite() && y.is_finite() {
|
||||
if y == 0.0 {
|
||||
@@ -1025,7 +1030,10 @@ mod math {
|
||||
z: ArgIntoFloat,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<f64> {
|
||||
let result = (*x).mul_add(*y, *z);
|
||||
let x = x.into_float();
|
||||
let y = y.into_float();
|
||||
let z = z.into_float();
|
||||
let result = x.mul_add(y, z);
|
||||
|
||||
if result.is_finite() {
|
||||
return Ok(result);
|
||||
|
||||
@@ -126,7 +126,7 @@ mod _statistics {
|
||||
sigma: ArgIntoFloat,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<f64> {
|
||||
normal_dist_inv_cdf(*p, *mu, *sigma)
|
||||
normal_dist_inv_cdf(p.into_float(), mu.into_float(), sigma.into_float())
|
||||
.ok_or_else(|| vm.new_value_error("inv_cdf undefined for these parameters"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -603,7 +603,7 @@ macro_rules! make_pack_float {
|
||||
arg: PyObjectRef,
|
||||
data: &mut [u8],
|
||||
) -> PyResult<()> {
|
||||
let f = *ArgIntoFloat::try_from_object(vm, arg)? as $T;
|
||||
let f = ArgIntoFloat::try_from_object(vm, arg)?.into_float() as $T;
|
||||
f.to_bits().pack_int::<E>(data);
|
||||
Ok(())
|
||||
}
|
||||
@@ -621,7 +621,7 @@ make_pack_float!(f64);
|
||||
|
||||
impl Packable for f16 {
|
||||
fn pack<E: ByteOrder>(vm: &VirtualMachine, arg: PyObjectRef, data: &mut [u8]) -> PyResult<()> {
|
||||
let f_64 = *ArgIntoFloat::try_from_object(vm, arg)?;
|
||||
let f_64 = ArgIntoFloat::try_from_object(vm, arg)?.into_float();
|
||||
// "from_f64 should be preferred in any non-`const` context" except it gives the wrong result :/
|
||||
let f_16 = Self::from_f64_const(f_64);
|
||||
if f_16.is_infinite() != f_64.is_infinite() {
|
||||
@@ -649,7 +649,7 @@ impl Packable for *mut raw::c_void {
|
||||
|
||||
impl Packable for bool {
|
||||
fn pack<E: ByteOrder>(vm: &VirtualMachine, arg: PyObjectRef, data: &mut [u8]) -> PyResult<()> {
|
||||
let v = *ArgIntoBool::try_from_object(vm, arg)? as u8;
|
||||
let v = ArgIntoBool::try_from_object(vm, arg)?.into_bool() as u8;
|
||||
v.pack_int::<E>(data);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -515,7 +515,7 @@ impl PyBytes {
|
||||
#[pymethod(name = "__rmul__")]
|
||||
#[pymethod]
|
||||
fn __mul__(zelf: PyRef<Self>, value: ArgIndex, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
|
||||
zelf.repeat(value.try_to_primitive(vm)?, vm)
|
||||
zelf.repeat(value.into_int_ref().try_to_primitive(vm)?, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__mod__")]
|
||||
|
||||
@@ -122,7 +122,9 @@ impl PyMappingProxy {
|
||||
MappingProxyInner::Class(class) => Ok(key
|
||||
.as_interned_str(vm)
|
||||
.is_some_and(|key| class.attributes.read().contains_key(key))),
|
||||
MappingProxyInner::Mapping(mapping) => mapping.sequence_unchecked().contains(key, vm),
|
||||
MappingProxyInner::Mapping(mapping) => {
|
||||
mapping.obj().sequence_unchecked().contains(key, vm)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,7 +163,9 @@ impl PyMappingProxy {
|
||||
#[pymethod]
|
||||
pub fn copy(&self, vm: &VirtualMachine) -> PyResult {
|
||||
match &self.mapping {
|
||||
MappingProxyInner::Mapping(d) => vm.call_method(d, identifier!(vm, copy).as_str(), ()),
|
||||
MappingProxyInner::Mapping(d) => {
|
||||
vm.call_method(d.obj(), identifier!(vm, copy).as_str(), ())
|
||||
}
|
||||
MappingProxyInner::Class(c) => {
|
||||
Ok(PyDict::from_attributes(c.attributes.read().clone(), vm)?.to_pyobject(vm))
|
||||
}
|
||||
|
||||
@@ -174,6 +174,7 @@ impl PySlice {
|
||||
|
||||
#[pymethod]
|
||||
fn indices(&self, length: ArgIndex, vm: &VirtualMachine) -> PyResult<PyTupleRef> {
|
||||
let length = length.into_int_ref();
|
||||
let length = length.as_bigint();
|
||||
if length.is_negative() {
|
||||
return Err(vm.new_value_error("length should not be negative."));
|
||||
|
||||
@@ -20,17 +20,16 @@ pub struct ArgIntoComplex {
|
||||
value: Complex64,
|
||||
}
|
||||
|
||||
impl From<ArgIntoComplex> for Complex64 {
|
||||
fn from(arg: ArgIntoComplex) -> Self {
|
||||
arg.value
|
||||
impl ArgIntoComplex {
|
||||
#[inline]
|
||||
pub fn into_complex(self) -> Complex64 {
|
||||
self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for ArgIntoComplex {
|
||||
type Target = Complex64;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.value
|
||||
impl From<ArgIntoComplex> for Complex64 {
|
||||
fn from(arg: ArgIntoComplex) -> Self {
|
||||
arg.value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +59,11 @@ pub struct ArgIntoFloat {
|
||||
}
|
||||
|
||||
impl ArgIntoFloat {
|
||||
#[inline]
|
||||
pub fn into_float(self) -> f64 {
|
||||
self.value
|
||||
}
|
||||
|
||||
pub fn vec_into_f64(v: Vec<Self>) -> Vec<f64> {
|
||||
// TODO: Vec::into_raw_parts once stabilized
|
||||
let mut v = core::mem::ManuallyDrop::new(v);
|
||||
@@ -75,13 +79,6 @@ impl From<ArgIntoFloat> for f64 {
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for ArgIntoFloat {
|
||||
type Target = f64;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFromObject for ArgIntoFloat {
|
||||
// Equivalent to PyFloat_AsDouble.
|
||||
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
|
||||
@@ -106,6 +103,11 @@ pub struct ArgIntoBool {
|
||||
impl ArgIntoBool {
|
||||
pub const TRUE: Self = Self { value: true };
|
||||
pub const FALSE: Self = Self { value: false };
|
||||
|
||||
#[inline]
|
||||
pub fn into_bool(self) -> bool {
|
||||
self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ArgIntoBool> for bool {
|
||||
@@ -114,13 +116,6 @@ impl From<ArgIntoBool> for bool {
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for ArgIntoBool {
|
||||
type Target = bool;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFromObject for ArgIntoBool {
|
||||
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
|
||||
Ok(Self {
|
||||
@@ -136,20 +131,25 @@ pub struct ArgIndex {
|
||||
value: PyIntRef,
|
||||
}
|
||||
|
||||
impl From<ArgIndex> for PyIntRef {
|
||||
fn from(arg: ArgIndex) -> Self {
|
||||
arg.value
|
||||
impl ArgIndex {
|
||||
#[inline]
|
||||
pub fn into_int_ref(self) -> PyIntRef {
|
||||
self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for ArgIndex {
|
||||
type Target = PyIntRef;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
impl AsRef<PyIntRef> for ArgIndex {
|
||||
fn as_ref(&self) -> &PyIntRef {
|
||||
&self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ArgIndex> for PyIntRef {
|
||||
fn from(arg: ArgIndex) -> Self {
|
||||
arg.value
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFromObject for ArgIndex {
|
||||
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
|
||||
Ok(Self {
|
||||
|
||||
@@ -7,7 +7,7 @@ use crate::{
|
||||
protocol::{PyIter, PyIterIter, PyMapping},
|
||||
types::GenericMethod,
|
||||
};
|
||||
use core::{borrow::Borrow, marker::PhantomData, ops::Deref};
|
||||
use core::{borrow::Borrow, marker::PhantomData};
|
||||
|
||||
#[derive(Clone, Traverse)]
|
||||
pub struct ArgCallable {
|
||||
@@ -133,6 +133,11 @@ impl ArgMapping {
|
||||
Self { obj: dict.into() }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn obj(&self) -> &PyObject {
|
||||
&self.obj
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn mapping(&self) -> PyMapping<'_> {
|
||||
self.obj.mapping_unchecked()
|
||||
@@ -153,14 +158,6 @@ impl AsRef<PyObject> for ArgMapping {
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for ArgMapping {
|
||||
type Target = PyObject;
|
||||
#[inline(always)]
|
||||
fn deref(&self) -> &PyObject {
|
||||
&self.obj
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ArgMapping> for PyObjectRef {
|
||||
#[inline(always)]
|
||||
fn from(value: ArgMapping) -> Self {
|
||||
|
||||
@@ -45,7 +45,7 @@ mod builtins {
|
||||
#[pyfunction]
|
||||
fn all(iterable: ArgIterable<ArgIntoBool>, vm: &VirtualMachine) -> PyResult<bool> {
|
||||
for item in iterable.iter(vm)? {
|
||||
if !*item? {
|
||||
if !item?.into_bool() {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
@@ -55,7 +55,7 @@ mod builtins {
|
||||
#[pyfunction]
|
||||
fn any(iterable: ArgIterable<ArgIntoBool>, vm: &VirtualMachine) -> PyResult<bool> {
|
||||
for item in iterable.iter(vm)? {
|
||||
if *item? {
|
||||
if item?.into_bool() {
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
@@ -451,6 +451,7 @@ mod builtins {
|
||||
|
||||
#[pyfunction]
|
||||
fn hex(number: ArgIndex) -> String {
|
||||
let number = number.into_int_ref();
|
||||
let n = number.as_bigint();
|
||||
format!("{n:#x}")
|
||||
}
|
||||
@@ -687,6 +688,7 @@ mod builtins {
|
||||
|
||||
#[pyfunction]
|
||||
fn oct(number: ArgIndex, vm: &VirtualMachine) -> PyResult {
|
||||
let number = number.into_int_ref();
|
||||
let n = number.as_bigint();
|
||||
let s = if n.is_negative() {
|
||||
format!("-0o{:o}", n.abs())
|
||||
@@ -786,7 +788,7 @@ mod builtins {
|
||||
.unwrap_or_else(|| PyStr::from("\n").into_ref(&vm.ctx));
|
||||
write(end)?;
|
||||
|
||||
if *options.flush {
|
||||
if options.flush.into() {
|
||||
vm.call_method(&file, "flush", ())?;
|
||||
}
|
||||
|
||||
|
||||
@@ -561,7 +561,7 @@ mod decl {
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<()> {
|
||||
if let Ok(obj) = ArgIntoBool::try_from_object(vm, state) {
|
||||
zelf.stop_flag.store(*obj);
|
||||
zelf.stop_flag.store(obj.into_bool());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -648,7 +648,7 @@ mod decl {
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<()> {
|
||||
if let Ok(obj) = ArgIntoBool::try_from_object(vm, state) {
|
||||
zelf.start_flag.store(*obj);
|
||||
zelf.start_flag.store(obj.into_bool());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user