Skip to content

Commit

Permalink
update tests of refcounting to use a non-immortal object
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Jul 30, 2023
1 parent 5564d61 commit ecfddd5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,14 +591,14 @@ mod tests {
fn test_set_item_refcnt() {
Python::with_gil(|py| {
let cnt;
let obj = py.eval("object()", None, None).unwrap();
{
let _pool = unsafe { crate::GILPool::new() };
let none = py.None();
cnt = none.get_refcnt(py);
let _dict = [(10, none)].into_py_dict(py);
cnt = obj.get_refcnt();
let _dict = [(10, obj)].into_py_dict(py);
}
{
assert_eq!(cnt, py.None().get_refcnt(py));
assert_eq!(cnt, obj.get_refcnt());
}
});
}
Expand Down
25 changes: 12 additions & 13 deletions src/types/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,31 +155,30 @@ mod tests {
#[test]
fn iter_item_refcnt() {
Python::with_gil(|py| {
let obj;
let none;
let count;
{
let obj = py.eval("object()", None, None).unwrap();
let list = {
let _pool = unsafe { GILPool::new() };
let l = PyList::empty(py);
none = py.None();
l.append(10).unwrap();
l.append(&none).unwrap();
count = none.get_refcnt(py);
obj = l.to_object(py);
}
let list = PyList::empty(py);
list.append(10).unwrap();
list.append(obj).unwrap();
count = obj.get_refcnt();
list.to_object(py)
};

{
let _pool = unsafe { GILPool::new() };
let inst = obj.as_ref(py);
let inst = list.as_ref(py);
let mut it = inst.iter().unwrap();

assert_eq!(
10_i32,
it.next().unwrap().unwrap().extract::<'_, i32>().unwrap()
);
assert!(it.next().unwrap().unwrap().is_none());
assert!(it.next().unwrap().unwrap().is(obj));
assert!(it.next().is_none());
}
assert_eq!(count, none.get_refcnt(py));
assert_eq!(count, obj.get_refcnt());
});
}

Expand Down
24 changes: 12 additions & 12 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,18 +395,18 @@ mod tests {
#[test]
fn test_set_item_refcnt() {
Python::with_gil(|py| {
let obj = py.eval("object()", None, None).unwrap();
let cnt;
{
let _pool = unsafe { crate::GILPool::new() };
let v = vec![2];
let ob = v.to_object(py);
let list: &PyList = ob.downcast(py).unwrap();
let none = py.None();
cnt = none.get_refcnt(py);
list.set_item(0, none).unwrap();
cnt = obj.get_refcnt();
list.set_item(0, obj).unwrap();
}

assert_eq!(cnt, py.None().get_refcnt(py));
assert_eq!(cnt, obj.get_refcnt());
});
}

Expand All @@ -431,15 +431,15 @@ mod tests {
fn test_insert_refcnt() {
Python::with_gil(|py| {
let cnt;
let obj = py.eval("object()", None, None).unwrap();
{
let _pool = unsafe { crate::GILPool::new() };
let list = PyList::empty(py);
let none = py.None();
cnt = none.get_refcnt(py);
list.insert(0, none).unwrap();
cnt = obj.get_refcnt();
list.insert(0, obj).unwrap();
}

assert_eq!(cnt, py.None().get_refcnt(py));
assert_eq!(cnt, obj.get_refcnt());
});
}

Expand All @@ -457,14 +457,14 @@ mod tests {
fn test_append_refcnt() {
Python::with_gil(|py| {
let cnt;
let obj = py.eval("object()", None, None).unwrap();
{
let _pool = unsafe { crate::GILPool::new() };
let list = PyList::empty(py);
let none = py.None();
cnt = none.get_refcnt(py);
list.append(none).unwrap();
cnt = obj.get_refcnt();
list.append(obj).unwrap();
}
assert_eq!(cnt, py.None().get_refcnt(py));
assert_eq!(cnt, obj.get_refcnt());
});
}

Expand Down

0 comments on commit ecfddd5

Please sign in to comment.