Skip to content

Commit 17449b5

Browse files
tpoliawdavidhewitt
andauthored
fix: Replace macro use in ordered_float tests (#5602)
* fix: Require macros feature for ordered_float tests The tests make use of the py_run macro which is only available with the macros feature enabled. * Allow ordered_float tests to run without macros Replace the py_run macro with a local function offering a simplified but sufficient functionality * Restore cast to f64 in ordered_float tests * Format ordered float tests * Add newsfragments entry * Remove unnecessary all in cfg(all(test)) attribute * Delete newsfragments/5602.misc.md * Use &CStr instead of requiring format!() --------- Co-authored-by: David Hewitt <[email protected]>
1 parent 7c8ad5f commit 17449b5

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

src/conversions/ordered_float.rs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,19 @@ float_conversions!(NotNan, f64, |val| NotNan::new(val)
101101
#[cfg(test)]
102102
mod test_ordered_float {
103103
use super::*;
104-
use crate::py_run;
104+
use crate::types::dict::IntoPyDict;
105105
use crate::types::PyAnyMethods;
106+
use std::ffi::CStr;
107+
use std::ffi::CString;
106108

107109
#[cfg(not(target_arch = "wasm32"))]
108110
use proptest::prelude::*;
109111

112+
fn py_run<'py>(py: Python<'py>, script: &CStr, locals: impl IntoPyDict<'py>) {
113+
py.run(script, None, Some(&locals.into_py_dict(py).unwrap()))
114+
.unwrap()
115+
}
116+
110117
macro_rules! float_roundtrip_tests {
111118
($wrapper:ident, $float_type:ty, $constructor:expr, $standard_test:ident, $wasm_test:ident, $infinity_test:ident, $zero_test:ident) => {
112119
#[cfg(not(target_arch = "wasm32"))]
@@ -118,15 +125,11 @@ mod test_ordered_float {
118125
Python::attach(|py| {
119126
let f_py: Bound<'_, PyFloat> = f.into_pyobject(py).unwrap();
120127

121-
py_run!(
122-
py,
123-
f_py,
124-
&format!(
128+
py_run(py, &CString::new(format!(
125129
"import math\nassert math.isclose(f_py, {})",
126130
inner_f as f64 // Always interpret the literal rs float value as f64
127131
// so that it's comparable with the python float
128-
)
129-
);
132+
)).unwrap(), [("f_py", &f_py)]);
130133

131134
let roundtripped_f: $wrapper<$float_type> = f_py.extract().unwrap();
132135

@@ -142,16 +145,17 @@ mod test_ordered_float {
142145
let f = $constructor(inner_f);
143146

144147
Python::attach(|py| {
145-
let f_py: Bound<'_, PyFloat> = f.into_pyobject(py).unwrap();
148+
let f_py: Bound<'_, PyFloat> = f.into_pyobject(py).unwrap();
146149

147-
py_run!(
150+
py_run(
148151
py,
149-
f_py,
150-
&format!(
152+
&CString::new(format!(
151153
"import math\nassert math.isclose(f_py, {})",
152154
inner_f as f64 // Always interpret the literal rs float value as f64
153155
// so that it's comparable with the python float
154-
)
156+
))
157+
.unwrap(),
158+
[("f_py", &f_py)],
155159
);
156160

157161
let roundtripped_f: $wrapper<$float_type> = f_py.extract().unwrap();
@@ -169,15 +173,15 @@ mod test_ordered_float {
169173
let ninf = $constructor(inner_ninf);
170174

171175
Python::attach(|py| {
172-
let pinf_py: Bound<'_, PyFloat> = pinf.into_pyobject(py).unwrap();
173-
let ninf_py: Bound<'_, PyFloat> = ninf.into_pyobject(py).unwrap();
176+
let pinf_py: Bound<'_, PyFloat> = pinf.into_pyobject(py).unwrap();
177+
let ninf_py: Bound<'_, PyFloat> = ninf.into_pyobject(py).unwrap();
174178

175-
py_run!(
179+
py_run(
176180
py,
177-
pinf_py ninf_py,
178-
"\
181+
c"\
179182
assert pinf_py == float('inf')\n\
180-
assert ninf_py == float('-inf')"
183+
assert ninf_py == float('-inf')",
184+
[("pinf_py", &pinf_py), ("ninf_py", &ninf_py)],
181185
);
182186

183187
let roundtripped_pinf: $wrapper<$float_type> = pinf_py.extract().unwrap();
@@ -197,20 +201,20 @@ mod test_ordered_float {
197201
let nzero = $constructor(inner_nzero);
198202

199203
Python::attach(|py| {
200-
let pzero_py: Bound<'_, PyFloat> = pzero.into_pyobject(py).unwrap();
201-
let nzero_py: Bound<'_, PyFloat> = nzero.into_pyobject(py).unwrap();
204+
let pzero_py: Bound<'_, PyFloat> = pzero.into_pyobject(py).unwrap();
205+
let nzero_py: Bound<'_, PyFloat> = nzero.into_pyobject(py).unwrap();
202206

203207
// This python script verifies that the values are 0.0 in magnitude
204208
// and that the signs are correct(+0.0 vs -0.0)
205-
py_run!(
209+
py_run(
206210
py,
207-
pzero_py nzero_py,
208-
"\
211+
c"\
209212
import math\n\
210213
assert pzero_py == 0.0\n\
211214
assert math.copysign(1.0, pzero_py) > 0.0\n\
212215
assert nzero_py == 0.0\n\
213-
assert math.copysign(1.0, nzero_py) < 0.0"
216+
assert math.copysign(1.0, nzero_py) < 0.0",
217+
[("pzero_py", &pzero_py), ("nzero_py", &nzero_py)],
214218
);
215219

216220
let roundtripped_pzero: $wrapper<$float_type> = pzero_py.extract().unwrap();
@@ -271,12 +275,10 @@ mod test_ordered_float {
271275
Python::attach(|py| {
272276
let nan_py: Bound<'_, PyFloat> = nan.into_pyobject(py).unwrap();
273277

274-
py_run!(
278+
py_run(
275279
py,
276-
nan_py,
277-
"\
278-
import math\n\
279-
assert math.isnan(nan_py)"
280+
c"import math\nassert math.isnan(nan_py)",
281+
[("nan_py", &nan_py)],
280282
);
281283

282284
let roundtripped_nan: OrderedFloat<$float_type> = nan_py.extract().unwrap();

0 commit comments

Comments
 (0)