From 05a1a097a913984734f2bff91f8fd5f90b452578 Mon Sep 17 00:00:00 2001 From: kngwyu Date: Sat, 14 Sep 2019 15:42:42 +0900 Subject: [PATCH] Remove mem::forget from PyBuffer::release --- .../rustapi_module/tests/test_buf_and_str.py | 7 +++-- src/buffer.rs | 1 - tests/test_buffer_protocol.rs | 27 +------------------ 3 files changed, 4 insertions(+), 31 deletions(-) diff --git a/examples/rustapi_module/tests/test_buf_and_str.py b/examples/rustapi_module/tests/test_buf_and_str.py index 78ad2965..681257a5 100644 --- a/examples/rustapi_module/tests/test_buf_and_str.py +++ b/examples/rustapi_module/tests/test_buf_and_str.py @@ -4,7 +4,7 @@ from rustapi_module.buf_and_str import BytesExtractor def test_pybuffer_doesnot_leak_memory(): - N = int(1e5) + N = 1000 extractor = BytesExtractor() process = psutil.Process(os.getpid()) @@ -25,6 +25,5 @@ def test_pybuffer_doesnot_leak_memory(): for i in range(N): extractor.to_str(message_s) - mv = memory_diff(to_vec) - ms = memory_diff(to_str) - assert abs(mv - ms) < 1000 + memory_diff(to_vec) == 0 + memory_diff(to_str) == 0 diff --git a/src/buffer.rs b/src/buffer.rs index a3eca6c2..91c317f6 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -582,7 +582,6 @@ impl PyBuffer { let ptr = &*self.0 as *const ffi::Py_buffer as *mut ffi::Py_buffer; ffi::PyBuffer_Release(ptr) }; - mem::forget(self); } } diff --git a/tests/test_buffer_protocol.rs b/tests/test_buffer_protocol.rs index 944dc959..64aa4b94 100644 --- a/tests/test_buffer_protocol.rs +++ b/tests/test_buffer_protocol.rs @@ -2,7 +2,7 @@ use pyo3::class::PyBufferProtocol; use pyo3::exceptions::BufferError; use pyo3::ffi; use pyo3::prelude::*; -use pyo3::types::{IntoPyDict, PyAny}; +use pyo3::types::IntoPyDict; use std::ffi::CStr; use std::os::raw::{c_int, c_void}; use std::ptr; @@ -76,28 +76,3 @@ fn test_buffer() { let d = [("ob", t)].into_py_dict(py); py.run("assert bytes(ob) == b' 23'", None, Some(d)).unwrap(); } - -fn byte_to_vec(py_byte_string: &PyAny) -> PyResult> { - py_byte_string.extract() -} - -fn byte_to_str(py_byte_string: &PyAny) -> PyResult { - py_byte_string.extract() -} - -#[test] -fn test_byte_conversion() { - let gil = Python::acquire_gil(); - let py = gil.python(); - let bytes = py.eval("b'I AM BYTE-STRING'", None, None).unwrap(); - let answer = "I AM BYTE-STRING"; - assert_eq!(byte_to_vec(bytes).unwrap(), answer.as_bytes().to_vec()); - assert_eq!( - byte_to_str(bytes) - .map_err(|err| { - err.print_and_set_sys_last_vars(py); - }) - .unwrap(), - answer.to_string() - ); -}