Skip to content

Commit b0991e2

Browse files
committed
Replace puruspe to pymath
Might be lower quality, but better compatibility
1 parent 1807464 commit b0991e2

File tree

6 files changed

+27
-41
lines changed

6 files changed

+27
-41
lines changed

.cspell.dict/rust-more.txt

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ powf
5151
powi
5252
prepended
5353
punct
54-
puruspe
5554
replacen
5655
rmatch
5756
rposition

Cargo.lock

+6-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ once_cell = "1.20.3"
184184
parking_lot = "0.12.3"
185185
paste = "1.0.15"
186186
proc-macro2 = "1.0.93"
187+
pymath = "0.0.2"
187188
quote = "1.0.38"
188189
rand = "0.9"
189190
rand_core = { version = "0.9", features = ["os_rng"] }

Lib/test/test_math.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,7 @@ def test_sumprod_accuracy(self):
13741374
self.assertEqual(sumprod([True, False] * 10, [0.1] * 20), 1.0)
13751375
self.assertEqual(sumprod([1.0, 10E100, 1.0, -10E100], [1.0]*4), 2.0)
13761376

1377+
@unittest.skip("TODO: RUSTPYTHON, Taking a few minutes.")
13771378
@support.requires_resource('cpu')
13781379
def test_sumprod_stress(self):
13791380
sumprod = math.sumprod
@@ -2079,7 +2080,7 @@ def test_testfile(self):
20792080
self.fail('Failures in test_testfile:\n ' +
20802081
'\n '.join(failures))
20812082

2082-
@unittest.skip("TODO: RUSTPYTHON, Currently hangs. Function never finishes.")
2083+
@unittest.skip("TODO: RUSTPYTHON, Taking a few minutes.")
20832084
@requires_IEEE_754
20842085
def test_mtestfile(self):
20852086
fail_fmt = "{}: {}({!r}): {}"

stdlib/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ memchr = { workspace = true }
4545
base64 = "0.22"
4646
csv-core = "0.1.11"
4747
dyn-clone = "1.0.10"
48-
puruspe = "0.4.0"
48+
pymath = { workspace = true }
4949
xml-rs = "0.8.14"
5050

5151
# random

stdlib/src/math.rs

+17-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
pub(crate) use math::make_module;
22

3+
use crate::{builtins::PyBaseExceptionRef, vm::VirtualMachine};
4+
35
#[pymodule]
46
mod math {
57
use crate::vm::{
@@ -17,6 +19,8 @@ mod math {
1719
// Constants
1820
#[pyattr]
1921
use std::f64::consts::{E as e, PI as pi, TAU as tau};
22+
23+
use super::pymath_error_to_exception;
2024
#[pyattr(name = "inf")]
2125
const INF: f64 = f64::INFINITY;
2226
#[pyattr(name = "nan")]
@@ -475,38 +479,22 @@ mod math {
475479
// Special functions:
476480
#[pyfunction]
477481
fn erf(x: ArgIntoFloat) -> f64 {
478-
let x = *x;
479-
if x.is_nan() { x } else { puruspe::erf(x) }
482+
pymath::erf(*x)
480483
}
481484

482485
#[pyfunction]
483486
fn erfc(x: ArgIntoFloat) -> f64 {
484-
let x = *x;
485-
if x.is_nan() { x } else { puruspe::erfc(x) }
487+
pymath::erfc(*x)
486488
}
487489

488490
#[pyfunction]
489-
fn gamma(x: ArgIntoFloat) -> f64 {
490-
let x = *x;
491-
if x.is_finite() {
492-
puruspe::gamma(x)
493-
} else if x.is_nan() || x.is_sign_positive() {
494-
x
495-
} else {
496-
f64::NAN
497-
}
491+
fn gamma(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
492+
pymath::gamma(*x).map_err(|err| pymath_error_to_exception(err, vm))
498493
}
499494

500495
#[pyfunction]
501-
fn lgamma(x: ArgIntoFloat) -> f64 {
502-
let x = *x;
503-
if x.is_finite() {
504-
puruspe::ln_gamma(x)
505-
} else if x.is_nan() {
506-
x
507-
} else {
508-
f64::INFINITY
509-
}
496+
fn lgamma(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
497+
pymath::lgamma(*x).map_err(|err| pymath_error_to_exception(err, vm))
510498
}
511499

512500
fn try_magic_method(
@@ -1000,3 +988,10 @@ mod math {
1000988
Ok(result)
1001989
}
1002990
}
991+
992+
fn pymath_error_to_exception(err: pymath::Error, vm: &VirtualMachine) -> PyBaseExceptionRef {
993+
match err {
994+
pymath::Error::EDOM => vm.new_value_error("math domain error".to_owned()),
995+
pymath::Error::ERANGE => vm.new_overflow_error("math range error".to_owned()),
996+
}
997+
}

0 commit comments

Comments
 (0)