2018-08-19 18:42:17 +00:00
|
|
|
#![feature(specialization)]
|
2018-05-02 18:49:40 +00:00
|
|
|
|
|
|
|
extern crate pyo3;
|
|
|
|
|
2018-06-15 19:21:12 +00:00
|
|
|
use pyo3::ffi;
|
2018-05-02 18:49:40 +00:00
|
|
|
use pyo3::prelude::*;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2018-06-15 19:21:12 +00:00
|
|
|
use std::sync::Arc;
|
2018-05-02 18:49:40 +00:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
mod common;
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass(freelist = 2)]
|
2018-06-15 19:21:12 +00:00
|
|
|
struct ClassWithFreelist {
|
|
|
|
token: PyToken,
|
|
|
|
}
|
2018-05-02 18:49:40 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn class_with_freelist() {
|
|
|
|
let ptr;
|
|
|
|
{
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2018-06-15 19:21:12 +00:00
|
|
|
let inst = Py::new(py, |t| ClassWithFreelist { token: t }).unwrap();
|
|
|
|
let _inst2 = Py::new(py, |t| ClassWithFreelist { token: t }).unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
ptr = inst.as_ptr();
|
|
|
|
drop(inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2018-06-15 19:21:12 +00:00
|
|
|
let inst3 = Py::new(py, |t| ClassWithFreelist { token: t }).unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
assert_eq!(ptr, inst3.as_ptr());
|
|
|
|
|
2018-06-15 19:21:12 +00:00
|
|
|
let inst4 = Py::new(py, |t| ClassWithFreelist { token: t }).unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
assert_ne!(ptr, inst4.as_ptr())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TestDropCall {
|
2018-06-15 19:21:12 +00:00
|
|
|
drop_called: Arc<AtomicBool>,
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
impl Drop for TestDropCall {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.drop_called.store(true, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-05-02 18:49:40 +00:00
|
|
|
struct DataIsDropped {
|
|
|
|
member1: TestDropCall,
|
|
|
|
member2: TestDropCall,
|
|
|
|
token: PyToken,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn data_is_dropped() {
|
|
|
|
let drop_called1 = Arc::new(AtomicBool::new(false));
|
|
|
|
let drop_called2 = Arc::new(AtomicBool::new(false));
|
|
|
|
|
|
|
|
{
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2018-07-30 21:01:46 +00:00
|
|
|
let inst = py
|
|
|
|
.init(|t| DataIsDropped {
|
2018-06-15 19:21:12 +00:00
|
|
|
member1: TestDropCall {
|
|
|
|
drop_called: Arc::clone(&drop_called1),
|
|
|
|
},
|
|
|
|
member2: TestDropCall {
|
|
|
|
drop_called: Arc::clone(&drop_called2),
|
|
|
|
},
|
|
|
|
token: t,
|
|
|
|
}).unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
assert!(!drop_called1.load(Ordering::Relaxed));
|
|
|
|
assert!(!drop_called2.load(Ordering::Relaxed));
|
|
|
|
drop(inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(drop_called1.load(Ordering::Relaxed));
|
|
|
|
assert!(drop_called2.load(Ordering::Relaxed));
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-05-02 18:49:40 +00:00
|
|
|
struct ClassWithDrop {
|
|
|
|
token: PyToken,
|
|
|
|
}
|
|
|
|
impl Drop for ClassWithDrop {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
let py = Python::assume_gil_acquired();
|
|
|
|
|
|
|
|
let _empty1 = PyTuple::empty(py);
|
|
|
|
let _empty2: PyObject = PyTuple::empty(py).into();
|
|
|
|
let _empty3: &PyObjectRef = py.from_owned_ptr(ffi::PyTuple_New(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test behavior of pythonrun::register_pointers + typeob::dealloc
|
|
|
|
#[test]
|
|
|
|
fn create_pointers_in_drop() {
|
|
|
|
let _gil = Python::acquire_gil();
|
|
|
|
|
|
|
|
let ptr;
|
|
|
|
let cnt;
|
|
|
|
{
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let empty = PyTuple::empty(py);
|
|
|
|
ptr = empty.as_ptr();
|
|
|
|
cnt = empty.get_refcnt() - 1;
|
2018-06-15 19:21:12 +00:00
|
|
|
let inst = py.init(|t| ClassWithDrop { token: t }).unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
drop(inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
// empty1 and empty2 are still alive (stored in pointers list)
|
|
|
|
{
|
|
|
|
let _gil = Python::acquire_gil();
|
2018-06-15 19:21:12 +00:00
|
|
|
assert_eq!(cnt + 2, unsafe { ffi::Py_REFCNT(ptr) });
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// empty1 and empty2 should be released
|
|
|
|
{
|
|
|
|
let _gil = Python::acquire_gil();
|
2018-06-15 19:21:12 +00:00
|
|
|
assert_eq!(cnt, unsafe { ffi::Py_REFCNT(ptr) });
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-05-02 18:49:40 +00:00
|
|
|
struct GCIntegration {
|
|
|
|
self_ref: RefCell<PyObject>,
|
|
|
|
dropped: TestDropCall,
|
|
|
|
token: PyToken,
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyproto]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl PyGCProtocol for GCIntegration {
|
|
|
|
fn __traverse__(&self, visit: PyVisit) -> Result<(), PyTraverseError> {
|
|
|
|
visit.call(&*self.self_ref.borrow())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __clear__(&mut self) {
|
|
|
|
*self.self_ref.borrow_mut() = self.py().None();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn gc_integration() {
|
|
|
|
let drop_called = Arc::new(AtomicBool::new(false));
|
|
|
|
|
|
|
|
{
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2018-06-15 19:21:12 +00:00
|
|
|
let inst = Py::new_ref(py, |t| GCIntegration {
|
2018-05-02 18:49:40 +00:00
|
|
|
self_ref: RefCell::new(py.None()),
|
2018-06-15 19:21:12 +00:00
|
|
|
dropped: TestDropCall {
|
|
|
|
drop_called: Arc::clone(&drop_called),
|
|
|
|
},
|
|
|
|
token: t,
|
|
|
|
}).unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
|
|
|
|
*inst.self_ref.borrow_mut() = inst.into();
|
|
|
|
}
|
|
|
|
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
py.run("import gc; gc.collect()", None, None).unwrap();
|
|
|
|
assert!(drop_called.load(Ordering::Relaxed));
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass(gc)]
|
2018-05-02 18:49:40 +00:00
|
|
|
struct GCIntegration2 {
|
|
|
|
token: PyToken,
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn gc_integration2() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2018-06-15 19:21:12 +00:00
|
|
|
let inst = Py::new_ref(py, |t| GCIntegration2 { token: t }).unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
py_run!(py, inst, "import gc; assert inst in gc.get_objects()");
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass(weakref)]
|
2018-05-02 18:49:40 +00:00
|
|
|
struct WeakRefSupport {
|
|
|
|
token: PyToken,
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn weakref_support() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2018-06-15 19:21:12 +00:00
|
|
|
let inst = Py::new_ref(py, |t| WeakRefSupport { token: t }).unwrap();
|
|
|
|
py_run!(
|
|
|
|
py,
|
|
|
|
inst,
|
|
|
|
"import weakref; assert weakref.ref(inst)() is inst"
|
|
|
|
);
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-05-02 18:49:40 +00:00
|
|
|
struct BaseClassWithDrop {
|
|
|
|
token: PyToken,
|
|
|
|
data: Option<Arc<AtomicBool>>,
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pymethods]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl BaseClassWithDrop {
|
|
|
|
#[new]
|
|
|
|
fn __new__(obj: &PyRawObject) -> PyResult<()> {
|
2018-06-15 19:21:12 +00:00
|
|
|
obj.init(|t| BaseClassWithDrop {
|
|
|
|
token: t,
|
|
|
|
data: None,
|
|
|
|
})
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for BaseClassWithDrop {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Some(ref mut data) = self.data {
|
|
|
|
data.store(true, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-15 09:58:33 +00:00
|
|
|
#[pyclass(extends=BaseClassWithDrop)]
|
2018-05-02 18:49:40 +00:00
|
|
|
struct SubClassWithDrop {
|
|
|
|
token: PyToken,
|
|
|
|
data: Option<Arc<AtomicBool>>,
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pymethods]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl SubClassWithDrop {
|
|
|
|
#[new]
|
|
|
|
fn __new__(obj: &PyRawObject) -> PyResult<()> {
|
2018-06-15 19:21:12 +00:00
|
|
|
obj.init(|t| SubClassWithDrop {
|
|
|
|
token: t,
|
|
|
|
data: None,
|
|
|
|
})?;
|
2018-05-02 18:49:40 +00:00
|
|
|
BaseClassWithDrop::__new__(obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for SubClassWithDrop {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Some(ref mut data) = self.data {
|
|
|
|
data.store(true, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inheritance_with_new_methods_with_drop() {
|
|
|
|
let drop_called1 = Arc::new(AtomicBool::new(false));
|
|
|
|
let drop_called2 = Arc::new(AtomicBool::new(false));
|
|
|
|
|
|
|
|
{
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let _typebase = py.get_type::<BaseClassWithDrop>();
|
|
|
|
let typeobj = py.get_type::<SubClassWithDrop>();
|
2018-08-25 18:47:30 +00:00
|
|
|
let inst = typeobj.call(NoArgs, None).unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
|
|
|
|
let obj = SubClassWithDrop::try_from_mut(inst).unwrap();
|
|
|
|
obj.data = Some(Arc::clone(&drop_called1));
|
|
|
|
|
|
|
|
let base = obj.get_mut_base();
|
|
|
|
base.data = Some(Arc::clone(&drop_called2));
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(drop_called1.load(Ordering::Relaxed));
|
|
|
|
assert!(drop_called2.load(Ordering::Relaxed));
|
|
|
|
}
|