Skip to content

Commit

Permalink
Merge pull request #1297 from davidhewitt/pyobject-from-py
Browse files Browse the repository at this point in the history
py: fix reference count bug in From(Py<T>) for PyObject
  • Loading branch information
kngwyu authored Nov 28, 2020
2 parents 884f9b2 + f86e6d3 commit 1ee3961
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,9 @@ impl<T> std::convert::From<Py<T>> for PyObject
where
T: AsRef<PyAny>,
{
#[inline]
fn from(other: Py<T>) -> Self {
let Py(ptr, _) = other;
Py(ptr, PhantomData)
unsafe { Self::from_non_null(other.into_non_null()) }
}
}

Expand Down Expand Up @@ -647,4 +647,14 @@ mod test {
};
assert_eq!(unsafe { ffi::Py_REFCNT(dict.as_ptr()) }, 1);
}

#[test]
fn pyobject_from_py() {
Python::with_gil(|py| {
let dict: Py<PyDict> = PyDict::new(py).into();
let cnt = dict.get_refcnt(py);
let p: PyObject = dict.into();
assert_eq!(p.get_refcnt(py), cnt);
});
}
}

0 comments on commit 1ee3961

Please sign in to comment.