Skip to content

Commit

Permalink
Merge pull request #252 from kngwyu/compile-hack
Browse files Browse the repository at this point in the history
Temporary hack to compile with latest nightly
  • Loading branch information
konstin committed Oct 29, 2018
2 parents d598cf4 + 56b82c2 commit 74d9557
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/freelist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ffi;
use python::Python;
use std::mem;
use std::os::raw::c_void;
use typeob::{PyObjectAlloc, PyTypeInfo};
use typeob::{pytype_drop, PyObjectAlloc, PyTypeInfo};

/// Implementing this trait for custom class adds free allocation list to class.
/// The performance improvement applies to types that are often created and deleted in a row,
Expand Down Expand Up @@ -85,7 +85,7 @@ where

#[cfg(Py_3)]
unsafe fn dealloc(py: Python, obj: *mut ffi::PyObject) {
Self::drop(py, obj);
pytype_drop::<T>(py, obj);

if ffi::PyObject_CallFinalizerFromDealloc(obj) < 0 {
return;
Expand Down Expand Up @@ -114,7 +114,7 @@ where

#[cfg(not(Py_3))]
unsafe fn dealloc(py: Python, obj: *mut ffi::PyObject) {
Self::drop(py, obj);
pytype_drop::<T>(py, obj);

if let Some(obj) = <T as PyObjectWithFreeList>::get_free_list().insert(obj) {
match (*T::type_object()).tp_free {
Expand Down
15 changes: 9 additions & 6 deletions src/typeob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ impl PyObjectWithToken for PyRawObject {
}
}

pub(crate) unsafe fn pytype_drop<T: PyTypeInfo>(py: Python, obj: *mut ffi::PyObject) {
if T::OFFSET != 0 {
let ptr = (obj as *mut u8).offset(T::OFFSET) as *mut T;
std::ptr::drop_in_place(ptr);
pytype_drop::<T::BaseType>(py, obj);
}
}

/// A Python object allocator that is usable as a base type for `#[pyclass]`
pub trait PyObjectAlloc<T> {
/// Allocates a new object (usually by calling ty->tp_alloc),
Expand All @@ -207,12 +215,7 @@ where
#[allow(unconditional_recursion)]
/// Calls the rust destructor for the object.
default unsafe fn drop(py: Python, obj: *mut ffi::PyObject) {
if T::OFFSET != 0 {
let ptr = (obj as *mut u8).offset(T::OFFSET) as *mut T;
std::ptr::drop_in_place(ptr);

T::BaseType::drop(py, obj);
}
pytype_drop::<T>(py, obj);
}

default unsafe fn alloc(_py: Python) -> PyResult<*mut ffi::PyObject> {
Expand Down

0 comments on commit 74d9557

Please sign in to comment.