Skip to content

Commit

Permalink
bump to 0.6.0 (broken)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-k committed Mar 30, 2019
1 parent 99676d3 commit 70ae250
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ libc = "0.2.44"
num-complex = "0.2.1"
num-traits = "0.2.6"
ndarray = "0.12"
pyo3 = "0.5.3"
pyo3 = "0.6.0"

[features]
# In default setting, python version is automatically detected
Expand Down
14 changes: 7 additions & 7 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use ndarray::*;
use npyffi::{self, npy_intp, NPY_ORDER, PY_ARRAY_API};
use num_traits::AsPrimitive;
use pyo3::{ffi, prelude::*, types::PyObjectRef};
use pyo3::{PyDowncastError, PyObjectWithToken, ToPyPointer};
use pyo3::{ffi, prelude::*, types::PyAny};
use pyo3::{AsPyPointer, PyDowncastError, PyNativeType};
use std::iter::ExactSizeIterator;
use std::marker::PhantomData;
use std::mem;
Expand Down Expand Up @@ -37,7 +37,7 @@ use types::{NpyDataType, TypeNum};
/// So you can neither retrieve it nor deallocate it manually.
///
/// # Reference
///
///Object
/// Like [`new`](#method.new), all constractor methods of `PyArray` returns `&PyArray`.
///
/// This design follows
Expand Down Expand Up @@ -109,9 +109,9 @@ pyobject_native_type_convert!(

pyobject_native_type_named!(PyArray<T, D>, T, D);

impl<'a, T, D> ::std::convert::From<&'a PyArray<T, D>> for &'a PyObjectRef {
impl<'a, T, D> ::std::convert::From<&'a PyArray<T, D>> for &'a PyAny {
fn from(ob: &'a PyArray<T, D>) -> Self {
unsafe { &*(ob as *const PyArray<T, D> as *const PyObjectRef) }
unsafe { &*(ob as *const PyArray<T, D> as *const PyAny) }
}
}

Expand All @@ -120,12 +120,12 @@ impl<'a, T: TypeNum, D: Dimension> FromPyObject<'a> for &'a PyArray<T, D> {
// 1. Checks if the object is PyArray
// 2. Checks if the data type of the array is T
// 3. Checks if the dimension is same as D
fn extract(ob: &'a PyObjectRef) -> PyResult<Self> {
fn extract(ob: &'a PyAny) -> PyResult<Self> {
let array = unsafe {
if npyffi::PyArray_Check(ob.as_ptr()) == 0 {
return Err(PyDowncastError.into());
}
&*(ob as *const PyObjectRef as *const PyArray<T, D>)
&*(ob as *const PyAny as *const PyArray<T, D>)
};
array
.type_check()
Expand Down
50 changes: 28 additions & 22 deletions src/slice_box.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::types::TypeNum;
use pyo3::{ffi, typeob, types::PyObjectRef, PyObjectAlloc, Python, ToPyPointer};
use pyo3::class::methods::PyMethodsProtocol;
use pyo3::{ffi, type_object, types::PyAny, AsPyPointer, PyObjectAlloc, Python};
use std::os::raw::c_void;
use std::ptr::NonNull;

/// It's a memory store for IntoPyArray.
/// See IntoPyArray's doc for what concretely this type is for.
Expand All @@ -12,8 +14,8 @@ pub(crate) struct SliceBox<T> {

impl<T> SliceBox<T> {
pub(crate) unsafe fn new<'a>(box_: Box<[T]>) -> &'a Self {
<Self as typeob::PyTypeObject>::init_type();
let type_ob = <Self as typeob::PyTypeInfo>::type_object() as *mut _;
// <Self as type_object::PyTypeObject>::init_type();
let type_ob = <Self as type_object::PyTypeInfo>::type_object() as *mut _;
let base = ffi::_PyObject_New(type_ob);
*base = ffi::PyObject_HEAD_INIT;
(*base).ob_type = type_ob;
Expand All @@ -26,9 +28,9 @@ impl<T> SliceBox<T> {
}
}

impl<T> typeob::PyTypeInfo for SliceBox<T> {
impl<T> type_object::PyTypeInfo for SliceBox<T> {
type Type = ();
type BaseType = PyObjectRef;
type BaseType = PyAny;
const NAME: &'static str = "SliceBox";
const DESCRIPTION: &'static str = "Memory store for PyArray using rust's Box<[T]>.";
const FLAGS: usize = 0;
Expand All @@ -41,38 +43,42 @@ impl<T> typeob::PyTypeInfo for SliceBox<T> {
}
}

impl<T: TypeNum> typeob::PyTypeCreate for SliceBox<T> {
impl<T: TypeNum> type_object::PyTypeObject for SliceBox<T>
where
SliceBox<T>: PyMethodsProtocol,
{
#[inline(always)]
fn init_type() {
static START: std::sync::Once = std::sync::ONCE_INIT;
START.call_once(|| {
let ty = unsafe { <Self as typeob::PyTypeInfo>::type_object() };
if (ty.tp_flags & ffi::Py_TPFLAGS_READY) == 0 {
let gil = Python::acquire_gil();
let py = gil.python();
let mod_name = format!("rust_numpy.{:?}", T::npy_data_type());
typeob::initialize_type::<Self>(py, Some(&mod_name))
.map_err(|e| e.print(py))
.expect("Failed to initialize SliceBox");
}
});
fn init_type() -> NonNull<ffi::PyTypeObject> {
// static START: std::sync::Once = std::sync::ONCE_INIT;
// START.call_once(|| -> NonNull<ffi::PyTypeObject> {
let ty = unsafe { <Self as type_object::PyTypeInfo>::type_object() };
if (ty.tp_flags & ffi::Py_TPFLAGS_READY) == 0 {
let gil = Python::acquire_gil();
let py = gil.python();
// let mod_name = format!("rust_numpy.{:?}", T::npy_data_type());
type_object::initialize_type::<Self>(py)
.map_err(|e| e.print(py))
.expect("Failed to initialize SliceBox");
}
unsafe { NonNull::new_unchecked(ty) }
// })
}
}

impl<T> ToPyPointer for SliceBox<T> {
impl<T> AsPyPointer for SliceBox<T> {
#[inline]
fn as_ptr(&self) -> *mut ffi::PyObject {
&self.ob_base as *const _ as *mut _
}
}

impl<T> PyObjectAlloc<SliceBox<T>> for SliceBox<T> {
impl<T> PyObjectAlloc for SliceBox<T> {
/// Calls the rust destructor for the object.
unsafe fn drop(py: Python, obj: *mut ffi::PyObject) {
let data = (*(obj as *mut SliceBox<T>)).inner;
let boxed_slice = Box::from_raw(data);
drop(boxed_slice);
<Self as typeob::PyTypeInfo>::BaseType::drop(py, obj);
<Self as type_object::PyTypeInfo>::BaseType::drop(py, obj);
}
unsafe fn dealloc(py: Python, obj: *mut ffi::PyObject) {
Self::drop(py, obj);
Expand Down
2 changes: 1 addition & 1 deletion tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate pyo3;

use ndarray::*;
use numpy::*;
use pyo3::{prelude::*, types::PyDict, types::PyList, ToPyPointer};
use pyo3::{prelude::*, types::PyDict, types::PyList, AsPyPointer};

#[test]
fn new_c_order() {
Expand Down

0 comments on commit 70ae250

Please sign in to comment.