Skip to content

Commit 8c0f427

Browse files
authored
Merge pull request #62 from kngwyu/remove-pyarraymodule
Remove PyArrayModule
2 parents e544705 + 0fa3bd1 commit 8c0f427

File tree

9 files changed

+600
-609
lines changed

9 files changed

+600
-609
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ numpy = "0.3"
4545
``` rust
4646
extern crate numpy;
4747
extern crate pyo3;
48-
use numpy::{IntoPyResult, PyArray, PyArrayModule};
48+
use numpy::{IntoPyResult, PyArray, get_array_module};
4949
use pyo3::prelude::{ObjectProtocol, PyDict, PyResult, Python};
5050

5151
fn main() -> Result<(), ()> {
@@ -59,9 +59,9 @@ fn main() -> Result<(), ()> {
5959
}
6060

6161
fn main_<'py>(py: Python<'py>) -> PyResult<()> {
62-
let np = PyArrayModule::import(py)?;
62+
let np = get_array_module(py)?;
6363
let dict = PyDict::new(py);
64-
dict.set_item("np", np.as_pymodule())?;
64+
dict.set_item("np", np)?;
6565
let pyarray: &PyArray<i32> = py
6666
.eval("np.array([1, 2, 3], dtype='int32')", Some(&dict), None)?
6767
.extract()?;
@@ -95,16 +95,11 @@ extern crate numpy;
9595
extern crate pyo3;
9696

9797
use ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};
98-
use numpy::{IntoPyArray, IntoPyResult, PyArray, PyArrayModule};
98+
use numpy::{IntoPyArray, IntoPyResult, PyArray};
9999
use pyo3::prelude::{pymodinit, PyModule, PyResult, Python};
100100

101101
#[pymodinit]
102-
fn rust_ext(py: Python, m: &PyModule) -> PyResult<()> {
103-
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
104-
// You **must** write this statement for the PyArray type checker to work correctly
105-
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
106-
let _np = PyArrayModule::import(py)?;
107-
102+
fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
108103
// immutable example
109104
fn axpy(a: f64, x: ArrayViewD<f64>, y: ArrayViewD<f64>) -> ArrayD<f64> {
110105
a * &x + &y
@@ -118,10 +113,9 @@ fn rust_ext(py: Python, m: &PyModule) -> PyResult<()> {
118113
// wrapper of `axpy`
119114
#[pyfn(m, "axpy")]
120115
fn axpy_py(py: Python, a: f64, x: &PyArray<f64>, y: &PyArray<f64>) -> PyResult<PyArray<f64>> {
121-
let np = PyArrayModule::import(py)?;
122116
let x = x.as_array().into_pyresult("x must be f64 array")?;
123117
let y = y.as_array().into_pyresult("y must be f64 array")?;
124-
Ok(axpy(a, x, y).into_pyarray(py, &np))
118+
Ok(axpy(a, x, y).into_pyarray(py).to_owned(py))
125119
}
126120

127121
// wrapper of `mult`
@@ -143,6 +137,12 @@ We need your feedback. Don't hesitate to open [issues](https://github.com/termos
143137

144138
Version
145139
--------
140+
- v0.4.0(Coming soon)
141+
- Duplicate `PyArrayModule` and import Numpy API automatically
142+
143+
- v0.3.1, v0.3.2
144+
- Just update dependencies
145+
146146
- v0.3.0
147147
- Breaking Change: Migrated to pyo3 from rust-cpython
148148
- Some api addition

example/extensions/src/lib.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@ extern crate numpy;
33
extern crate pyo3;
44

55
use ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};
6-
use numpy::{IntoPyArray, IntoPyResult, PyArray, PyArrayModule};
6+
use numpy::{IntoPyArray, IntoPyResult, PyArray};
77
use pyo3::prelude::{pymodinit, PyModule, PyResult, Python};
88

99
#[pymodinit]
10-
fn rust_ext(py: Python, m: &PyModule) -> PyResult<()> {
11-
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12-
// You **must** write this statement for the PyArray type checker to work correctly
13-
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
14-
let _np = PyArrayModule::import(py)?;
15-
10+
fn rust_ext(_py: Python, m: &PyModule) -> PyResult<()> {
1611
// immutable example
1712
fn axpy(a: f64, x: ArrayViewD<f64>, y: ArrayViewD<f64>) -> ArrayD<f64> {
1813
a * &x + &y
@@ -26,10 +21,9 @@ fn rust_ext(py: Python, m: &PyModule) -> PyResult<()> {
2621
// wrapper of `axpy`
2722
#[pyfn(m, "axpy")]
2823
fn axpy_py(py: Python, a: f64, x: &PyArray<f64>, y: &PyArray<f64>) -> PyResult<PyArray<f64>> {
29-
let np = PyArrayModule::import(py)?;
3024
let x = x.as_array().into_pyresult("x must be f64 array")?;
3125
let y = y.as_array().into_pyresult("y must be f64 array")?;
32-
Ok(axpy(a, x, y).into_pyarray(py, &np))
26+
Ok(axpy(a, x, y).into_pyarray(py).to_owned(py))
3327
}
3428

3529
// wrapper of `mult`

0 commit comments

Comments
 (0)