Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions crates/capi/src/floatobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::object::define_py_check;
use crate::{PyObject, pystate::with_vm};
use core::ffi::c_double;
use core::ptr::NonNull;
use rustpython_vm::AsObject;
use rustpython_vm::builtins::PyFloat;

define_py_check!(fn PyFloat_Check, types.float_type);
Expand All @@ -24,6 +26,37 @@ pub unsafe extern "C" fn PyFloat_AsDouble(obj: *mut PyObject) -> c_double {
})
}

#[unsafe(no_mangle)]
pub extern "C" fn PyFloat_GetMax() -> c_double {
c_double::MAX
}

#[unsafe(no_mangle)]
pub extern "C" fn PyFloat_GetMin() -> c_double {
c_double::MIN_POSITIVE
}

#[unsafe(no_mangle)]
pub extern "C" fn PyFloat_GetInfo() -> *mut PyObject {
with_vm(|vm| {
vm.sys_module
.as_object()
.get_attr("float_info", vm)
.map(|obj| obj.into_raw().as_ptr())
})
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyFloat_FromString(obj: *mut PyObject) -> *mut PyObject {
with_vm(|vm| {
let obj = NonNull::new(obj)
.ok_or_else(|| vm.new_type_error("float() argument must be a string or a number"))?;
let obj = unsafe { obj.as_ref() }.to_owned();
let float = rustpython_vm::builtins::parse_float_from_string(obj, vm)?;
Ok(vm.ctx.new_float(float))
})
}

#[cfg(false)]
mod tests {
use core::f64::consts::PI;
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl Constructor for PyFloat {
}
}

fn float_from_string(val: PyObjectRef, vm: &VirtualMachine) -> PyResult<f64> {
pub fn float_from_string(val: PyObjectRef, vm: &VirtualMachine) -> PyResult<f64> {
let (bytearray, buffer, buffer_lock, mapped_string);
let b = if let Some(s) = val.downcast_ref::<PyStr>() {
use crate::common::str::PyKindStr;
Expand Down
1 change: 1 addition & 0 deletions crates/vm/src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub(crate) mod union_;
pub use union_::{PyUnion, make_union};
pub(crate) mod descriptor;

pub use float::float_from_string as parse_float_from_string;
pub use float::try_to_bigint as try_f64_to_bigint;
pub use int::try_to_float as try_bigint_to_f64;

Expand Down
Loading