Skip to content
Merged
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
1 change: 1 addition & 0 deletions crates/capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern crate alloc;

pub mod abstract_;
pub mod import;
pub mod longobject;
pub mod object;
pub mod pyerrors;
pub mod pylifecycle;
Expand Down
89 changes: 89 additions & 0 deletions crates/capi/src/longobject.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use crate::PyObject;
use crate::object::define_py_check;
use crate::pystate::with_vm;
use core::ffi::{c_long, c_longlong, c_ulong, c_ulonglong};
use rustpython_vm::PyResult;
use rustpython_vm::builtins::PyInt;

define_py_check!(fn PyLong_Check, types.int_type);
define_py_check!(exact fn PyLong_CheckExact, types.int_type);

#[unsafe(no_mangle)]
pub extern "C" fn PyLong_FromLong(value: c_long) -> *mut PyObject {
with_vm(|vm| vm.ctx.new_int(value))
}

#[unsafe(no_mangle)]
pub extern "C" fn PyLong_FromLongLong(value: c_longlong) -> *mut PyObject {
with_vm(|vm| vm.ctx.new_int(value))
}

#[unsafe(no_mangle)]
pub extern "C" fn PyLong_FromSsize_t(value: isize) -> *mut PyObject {
with_vm(|vm| vm.ctx.new_int(value))
}

#[unsafe(no_mangle)]
pub extern "C" fn PyLong_FromSize_t(value: usize) -> *mut PyObject {
with_vm(|vm| vm.ctx.new_int(value))
}

#[unsafe(no_mangle)]
pub extern "C" fn PyLong_FromUnsignedLong(value: c_ulong) -> *mut PyObject {
with_vm(|vm| vm.ctx.new_int(value))
}

#[unsafe(no_mangle)]
pub extern "C" fn PyLong_FromUnsignedLongLong(value: c_ulonglong) -> *mut PyObject {
with_vm(|vm| vm.ctx.new_int(value))
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyLong_AsLong(obj: *mut PyObject) -> c_long {
with_vm::<PyResult<c_long>, _>(|vm| {
unsafe { &*obj }
.to_owned()
.try_index(vm)?
.as_bigint()
.try_into()
.map_err(|_| vm.new_overflow_error("Python int too large to convert to C long"))
})
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyLong_AsUnsignedLongLong(obj: *mut PyObject) -> c_ulonglong {
with_vm::<PyResult<c_ulonglong>, _>(|vm| {
unsafe { &*obj }
.to_owned()
.try_downcast::<PyInt>(vm)?
.as_bigint()
.try_into()
.map_err(|_| {
vm.new_overflow_error("Python int too large to convert to C unsigned long long")
})
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

#[cfg(false)]
mod tests {
use pyo3::prelude::*;
use pyo3::types::PyInt;

#[test]
fn test_py_int_u32() {
Python::attach(|py| {
let number = PyInt::new(py, 123);
assert!(number.is_instance_of::<PyInt>());
assert_eq!(number.extract::<i32>().unwrap(), 123);
})
}

#[test]
fn test_py_int_u64() {
Python::attach(|py| {
let number = PyInt::new(py, 123u64);
assert!(number.is_instance_of::<PyInt>());
assert_eq!(number.extract::<u64>().unwrap(), 123);
})
}
}
1 change: 1 addition & 0 deletions crates/capi/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ macro_rules! define_py_check {
};
}

pub(crate) use define_py_check;
define_py_check!(fn PyType_Check, types.type_type);
define_py_check!(exact fn PyType_CheckExact, types.type_type);

Expand Down
10 changes: 9 additions & 1 deletion crates/capi/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::PyObject;
use core::convert::Infallible;
use core::ffi::{c_char, c_double, c_int, c_long, c_void};
use core::ffi::{c_char, c_double, c_int, c_long, c_ulonglong, c_void};
use rustpython_vm::{PyObjectRef, PyRef, PyResult, VirtualMachine};

pub(crate) trait FfiResult<Output = Self> {
Expand Down Expand Up @@ -93,6 +93,14 @@ impl FfiResult for c_long {
}
}

impl FfiResult for c_ulonglong {
const ERR_VALUE: Self = Self::MAX;

fn into_output(self, _vm: &VirtualMachine) -> Self {
self
}
}

impl FfiResult for c_double {
const ERR_VALUE: Self = -1.0;

Expand Down
Loading