Remove mem::forget from PyBuffer::release

This commit is contained in:
kngwyu 2019-09-14 15:42:42 +09:00
parent e911828b79
commit 05a1a097a9
3 changed files with 4 additions and 31 deletions

View File

@ -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

View File

@ -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);
}
}

View File

@ -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<Vec<u8>> {
py_byte_string.extract()
}
fn byte_to_str(py_byte_string: &PyAny) -> PyResult<String> {
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()
);
}