Skip to content

Commit 07e2416

Browse files
committed
nonnull
1 parent 06d6ce1 commit 07e2416

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/npyffi/random.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{ffi::c_void, ptr::NonNull};
22

3-
use pyo3::{prelude::*, types::PyCapsule};
3+
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyCapsule};
44

55
#[repr(C)]
66
#[derive(Debug, Clone, Copy)]
@@ -12,8 +12,9 @@ pub struct npy_bitgen {
1212
pub next_raw: NonNull<unsafe extern "C" fn(*mut c_void) -> super::npy_uint64>, //nogil
1313
}
1414

15-
pub fn get_bitgen_api<'py>(bitgen: Bound<'py, PyAny>) -> PyResult<*mut npy_bitgen> {
15+
pub fn get_bitgen_api<'py>(bitgen: Bound<'py, PyAny>) -> PyResult<NonNull<npy_bitgen>> {
1616
let capsule = bitgen.getattr("capsule")?.downcast_into::<PyCapsule>()?;
1717
assert_eq!(capsule.name()?, Some(c"BitGenerator"));
18-
Ok(capsule.pointer() as *mut npy_bitgen)
18+
let ptr = capsule.pointer() as *mut npy_bitgen;
19+
NonNull::new(ptr).ok_or_else(|| PyRuntimeError::new_err("Invalid BitGenerator capsule"))
1920
}

0 commit comments

Comments
 (0)