Skip to content

Commit dc27044

Browse files
committed
npyffi
1 parent fadd273 commit dc27044

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

src/npyffi/array.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::ptr::null_mut;
99

1010
use pyo3::ffi;
1111
use pyo3::ffi::{PyObject, PyTypeObject};
12-
use pyo3::{ObjectProtocol, PyModule, PyResult, Python};
12+
use pyo3::{ObjectProtocol, PyModule, PyResult, Python, ToPyPointer};
1313

1414
use npyffi::*;
1515

@@ -20,15 +20,15 @@ use npyffi::*;
2020
/// Some APIs (including most accessor) in the above URL are not implemented
2121
/// since they are defined as C macro, and cannot be called from rust.
2222
/// Some of these are implemented on the high-level interface as a Rust function.
23-
pub struct PyArrayModule {
24-
numpy: PyModule,
23+
pub struct PyArrayModule<'py> {
24+
numpy: &'py PyModule,
2525
api: *const *const c_void,
2626
}
2727

28-
impl Deref for PyArrayModule {
28+
impl<'py> Deref for PyArrayModule<'py> {
2929
type Target = PyModule;
3030
fn deref(&self) -> &Self::Target {
31-
&self.numpy
31+
self.numpy
3232
}
3333
}
3434

@@ -42,14 +42,13 @@ pub unsafe fn $fname(&self, $($arg : $t), *) $( -> $ret )* {
4242
}
4343
}} // pyarray_api!
4444

45-
impl PyArrayModule {
45+
impl<'py> PyArrayModule<'py> {
4646
/// Import `numpy.core.multiarray` to use Array API.
47-
pub fn import(py: Python) -> PyResult<Self> {
47+
pub fn import(py: Python<'py>) -> PyResult<Self> {
4848
let numpy = py.import("numpy.core.multiarray")?;
49-
let c_api = numpy.as_object().getattr(py, "_ARRAY_API")?;
49+
let c_api = numpy.getattr("_ARRAY_API")?;
5050
let api = unsafe {
51-
pyo3::ffi::PyCapsule_GetPointer(c_api.as_object().as_ptr(), null_mut())
52-
as *const *const c_void
51+
ffi::PyCapsule_GetPointer(c_api.as_ptr(), null_mut()) as *const *const c_void
5352
};
5453
Ok(Self {
5554
numpy: numpy,
@@ -322,7 +321,7 @@ macro_rules! impl_array_type {
322321
#[allow(non_camel_case_types)]
323322
#[repr(i32)]
324323
pub enum ArrayType { $($tname),* }
325-
impl PyArrayModule {
324+
impl<'py> PyArrayModule<'py> {
326325
pub unsafe fn get_type_object(&self, ty: ArrayType) -> *mut PyTypeObject {
327326
match ty {
328327
$( ArrayType::$tname => *(self.api.offset($offset)) as *mut PyTypeObject ),*
@@ -375,10 +374,10 @@ impl_array_type!(
375374

376375
#[allow(non_snake_case)]
377376
pub unsafe fn PyArray_Check(np: &PyArrayModule, op: *mut PyObject) -> c_int {
378-
pyo3::ffi::PyObject_TypeCheck(op, np.get_type_object(ArrayType::PyArray_Type))
377+
ffi::PyObject_TypeCheck(op, np.get_type_object(ArrayType::PyArray_Type))
379378
}
380379

381380
#[allow(non_snake_case)]
382381
pub unsafe fn PyArray_CheckExact(np: &PyArrayModule, op: *mut PyObject) -> c_int {
383-
(pyo3::ffi::Py_TYPE(op) == np.get_type_object(ArrayType::PyArray_Type)) as c_int
382+
(ffi::Py_TYPE(op) == np.get_type_object(ArrayType::PyArray_Type)) as c_int
384383
}

src/npyffi/ufunc.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::ptr::null_mut;
88

99
use pyo3::ffi;
1010
use pyo3::ffi::{PyObject, PyTypeObject};
11-
use pyo3::{ObjectProtocol, PyModule, PyResult, Python};
11+
use pyo3::{ObjectProtocol, PyModule, PyResult, Python, ToPyPointer};
1212

1313
use super::objects::*;
1414
use super::types::*;
@@ -17,15 +17,15 @@ use super::types::*;
1717
/// https://docs.scipy.org/doc/numpy/reference/c-api.ufunc.html
1818
///
1919
/// Most of UFunc API is exposed as the related function of this module object.
20-
pub struct PyUFuncModule {
21-
numpy: PyModule,
20+
pub struct PyUFuncModule<'py> {
21+
numpy: &'py PyModule,
2222
api: *const *const c_void,
2323
}
2424

25-
impl Deref for PyUFuncModule {
25+
impl<'py> Deref for PyUFuncModule<'py> {
2626
type Target = PyModule;
2727
fn deref(&self) -> &Self::Target {
28-
&self.numpy
28+
self.numpy
2929
}
3030
}
3131

@@ -39,14 +39,13 @@ pub unsafe fn $fname(&self, $($arg : $t), *) $( -> $ret )* {
3939
}
4040
}} // pyufunc_api!
4141

42-
impl PyUFuncModule {
42+
impl<'py> PyUFuncModule<'py> {
4343
/// Import `numpy.core.umath` to use UFunc API.
44-
pub fn import(py: Python) -> PyResult<Self> {
44+
pub fn import(py: Python<'py>) -> PyResult<Self> {
4545
let numpy = py.import("numpy.core.umath")?;
46-
let c_api = numpy.as_object().getattr(py, "_UFUNC_API")?;
46+
let c_api = numpy.getattr("_UFUNC_API")?;
4747
let api = unsafe {
48-
pyo3::ffi::PyCapsule_GetPointer(c_api.as_object().as_ptr(), null_mut())
49-
as *const *const c_void
48+
ffi::PyCapsule_GetPointer(c_api.as_ptr(), null_mut()) as *const *const c_void
5049
};
5150
Ok(Self {
5251
numpy: numpy,

0 commit comments

Comments
 (0)