Skip to content

Commit 5ec6526

Browse files
Add remaining float functions to c-apo
1 parent 8905b80 commit 5ec6526

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

crates/capi/src/floatobject.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::object::define_py_check;
22
use crate::{PyObject, pystate::with_vm};
33
use core::ffi::c_double;
4+
use core::ptr::NonNull;
5+
use rustpython_vm::AsObject;
46
use rustpython_vm::builtins::PyFloat;
57

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

29+
#[unsafe(no_mangle)]
30+
pub extern "C" fn PyFloat_GetMax() -> c_double {
31+
c_double::MAX
32+
}
33+
34+
#[unsafe(no_mangle)]
35+
pub extern "C" fn PyFloat_GetMin() -> c_double {
36+
c_double::MIN_POSITIVE
37+
}
38+
39+
#[unsafe(no_mangle)]
40+
pub extern "C" fn PyFloat_GetInfo() -> *mut PyObject {
41+
with_vm(|vm| {
42+
vm.sys_module
43+
.as_object()
44+
.get_attr("float_info", vm)
45+
.map(|obj| obj.into_raw().as_ptr())
46+
})
47+
}
48+
49+
#[unsafe(no_mangle)]
50+
pub unsafe extern "C" fn PyFloat_FromString(obj: *mut PyObject) -> *mut PyObject {
51+
with_vm(|vm| {
52+
let obj = NonNull::new(obj)
53+
.ok_or_else(|| vm.new_type_error("float() argument must be a string or a number"))?;
54+
let obj = unsafe { obj.as_ref() }.to_owned();
55+
let float = rustpython_vm::builtins::parse_float_from_string(obj, vm)?;
56+
Ok(vm.ctx.new_float(float))
57+
})
58+
}
59+
2760
#[cfg(false)]
2861
mod tests {
2962
use core::f64::consts::PI;

crates/vm/src/builtins/float.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl Constructor for PyFloat {
205205
}
206206
}
207207

208-
fn float_from_string(val: PyObjectRef, vm: &VirtualMachine) -> PyResult<f64> {
208+
pub fn float_from_string(val: PyObjectRef, vm: &VirtualMachine) -> PyResult<f64> {
209209
let (bytearray, buffer, buffer_lock, mapped_string);
210210
let b = if let Some(s) = val.downcast_ref::<PyStr>() {
211211
use crate::common::str::PyKindStr;

crates/vm/src/builtins/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ pub(crate) mod union_;
9898
pub use union_::{PyUnion, make_union};
9999
pub(crate) mod descriptor;
100100

101+
pub use float::float_from_string as parse_float_from_string;
101102
pub use float::try_to_bigint as try_f64_to_bigint;
102103
pub use int::try_to_float as try_bigint_to_f64;
103104

0 commit comments

Comments
 (0)