forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloatobject.rs
More file actions
41 lines (35 loc) · 1.07 KB
/
Copy pathfloatobject.rs
File metadata and controls
41 lines (35 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use crate::object::define_py_check;
use crate::{PyObject, pystate::with_vm};
use core::ffi::c_double;
use rustpython_vm::builtins::PyFloat;
define_py_check!(fn PyFloat_Check, types.float_type);
define_py_check!(exact fn PyFloat_CheckExact, types.float_type);
#[unsafe(no_mangle)]
pub extern "C" fn PyFloat_FromDouble(value: c_double) -> *mut PyObject {
with_vm(|vm| vm.ctx.new_float(value))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyFloat_AsDouble(obj: *mut PyObject) -> c_double {
with_vm(|vm| {
let obj_ref = unsafe { &*obj };
let float_obj = obj_ref
.to_owned()
.try_downcast::<PyFloat>(vm)
.or_else(|_| obj_ref.try_float(vm))?;
Ok(float_obj.to_f64())
})
}
#[cfg(false)]
mod tests {
use core::f64::consts::PI;
use pyo3::prelude::*;
use pyo3::types::PyFloat;
#[test]
fn test_py_float() {
Python::attach(|py| {
let pi = PyFloat::new(py, PI);
assert!(pi.is_instance_of::<PyFloat>());
assert_eq!(pi.extract::<f64>().unwrap(), PI);
})
}
}