Skip to content

Commit 5309f8d

Browse files
committed
ToPyObject::with_borrowed_ptr(): use explicit release_ref() call.
Unlike the implicit Drop call, an explicit release_ref() call does not need to check whether the GIL is acquired.
1 parent 1bdca22 commit 5309f8d

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/conversion.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
use std;
2020
use ffi;
21-
use python::{Python, PythonObject, PythonObjectWithCheckedDowncast, PyClone};
21+
use python::{Python, PythonObject, PythonObjectWithCheckedDowncast, PyDrop, PyClone};
2222
use objects::PyObject;
2323
use err::PyResult;
2424

@@ -50,7 +50,9 @@ pub trait ToPyObject {
5050
where F: FnOnce(*mut ffi::PyObject) -> R
5151
{
5252
let obj = self.to_py_object(py).into_object();
53-
f(obj.as_ptr())
53+
let res = f(obj.as_ptr());
54+
obj.release_ref(py);
55+
res
5456
}
5557

5658
// FFI functions that accept a borrowed reference will use:
@@ -60,7 +62,7 @@ pub trait ToPyObject {
6062
// 2) input is PyObject
6163
// -> with_borrowed_ptr() just forwards to the closure
6264
// 3) input is &str, int, ...
63-
// -> to_py_object() allocates new Python object; FFI call happens; PyObject::drop() calls Py_DECREF()
65+
// -> to_py_object() allocates new Python object; FFI call happens; release_ref() calls Py_DECREF()
6466

6567
// FFI functions that steal a reference will use:
6668
// let input = try!(input.into_py_object()); ffi::Call(input.steal_ptr())

0 commit comments

Comments
 (0)