From a90d8c241fe688c44a17b08eb186db5433c0e061 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 4 Jul 2017 10:24:04 +0600 Subject: [PATCH] fix ref counter for get_kwargs --- Cargo.toml | 2 +- src/argparse.rs | 2 +- src/objects/list.rs | 3 +-- src/python.rs | 5 ----- tests/compile_tests.rs | 2 +- tests/test_class.rs | 5 ++++- 6 files changed, 8 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ab750a23..f0727e1b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ pyo3cls = { path = "pyo3cls" } # backtrace = "0.3" [dev-dependencies] -compiletest_rs = "*" +#compiletest_rs = "*" [build-dependencies] regex = "0.2" diff --git a/src/argparse.rs b/src/argparse.rs index 5a43e8e6..6a2d25ed 100644 --- a/src/argparse.rs +++ b/src/argparse.rs @@ -100,6 +100,6 @@ pub unsafe fn get_kwargs<'p>(py: Python<'p>, ptr: *mut ffi::PyObject) -> Option< if ptr.is_null() { None } else { - Some(py.cast_from_ptr::(ptr)) + Some(py.cast_from_borrowed_ptr::(ptr)) } } diff --git a/src/objects/list.rs b/src/objects/list.rs index 0e405874..73e9c6a6 100644 --- a/src/objects/list.rs +++ b/src/objects/list.rs @@ -51,8 +51,7 @@ impl PyList { pub fn get_item(&self, index: isize) -> &PyObjectRef { unsafe { let ptr = ffi::PyList_GetItem(self.as_ptr(), index as Py_ssize_t); - let ob = PyObject::from_borrowed_ptr(self.token(), ptr); - self.token().track_object(ob) + self.token().cast_from_borrowed_ptr(ptr) } } diff --git a/src/python.rs b/src/python.rs index 509d3af2..0fed0226 100644 --- a/src/python.rs +++ b/src/python.rs @@ -391,11 +391,6 @@ impl<'p> Python<'p> { ::unchecked_mut_downcast_from(p) } - pub fn track_object(self, obj: PyObject) -> &'p PyObjectRef - { - unsafe { pythonrun::register_owned(self, obj.into_ptr()) } - } - /// Release PyObject reference. #[inline] pub fn release(self, ob: T) where T: IntoPyPointer { diff --git a/tests/compile_tests.rs b/tests/compile_tests.rs index aace008b..c490b332 100644 --- a/tests/compile_tests.rs +++ b/tests/compile_tests.rs @@ -1,4 +1,4 @@ -extern crate compiletest_rs as compiletest; +//extern crate compiletest_rs as compiletest; //use std::path::PathBuf; //use std::env::var; diff --git a/tests/test_class.rs b/tests/test_class.rs index 51073195..234c1955 100644 --- a/tests/test_class.rs +++ b/tests/test_class.rs @@ -172,7 +172,7 @@ fn new_with_two_args() { assert_eq!(obj._data2, 20); } -#[py::class(freelist=10)] +#[py::class(freelist=2)] struct ClassWithFreelist{token: PyToken} #[test] @@ -187,6 +187,9 @@ fn class_with_freelist() { let inst3 = Py::new(py, |t| ClassWithFreelist{token: t}).unwrap(); assert_eq!(ptr, inst3.as_ptr()); + + let inst4 = Py::new(py, |t| ClassWithFreelist{token: t}).unwrap(); + assert_ne!(ptr, inst4.as_ptr()) } struct TestDropCall {