Skip to content

Commit

Permalink
Use Python::with_gil and py_assert
Browse files Browse the repository at this point in the history
Co-authored-by: Yuji Kanagawa <[email protected]>
  • Loading branch information
messense and kngwyu committed Mar 19, 2021
1 parent 2cec240 commit 1c57294
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
22 changes: 11 additions & 11 deletions tests/test_class_basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ struct UnitClass;

#[test]
fn unit_class() {
let gil = Python::acquire_gil();
let py = gil.python();
let typeobj = py.get_type::<UnitClass>();
// By default, don't allow creating instances from python.
assert!(typeobj.call((), None).is_err());
Python::with_gil(|py| {
let typeobj = py.get_type::<UnitClass>();
// By default, don't allow creating instances from python.
assert!(typeobj.call((), None).is_err());

py_assert!(py, typeobj, "typeobj.__name__ == 'UnitClass'");
py_assert!(py, typeobj, "typeobj.__name__ == 'UnitClass'");
});
}

/// Line1
Expand Down Expand Up @@ -309,10 +309,10 @@ struct TupleClass(i32);

#[test]
fn test_tuple_struct_class() {
let gil = Python::acquire_gil();
let py = gil.python();
let typeobj = py.get_type::<TupleClass>();
assert!(typeobj.call((), None).is_err());
Python::with_gil(|py| {
let typeobj = py.get_type::<TupleClass>();
assert!(typeobj.call((), None).is_err());

py_assert!(py, typeobj, "typeobj.__name__ == 'TupleClass'");
py_assert!(py, typeobj, "typeobj.__name__ == 'TupleClass'");
});
}
30 changes: 15 additions & 15 deletions tests/test_class_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ impl UnitClassWithNew {

#[test]
fn unit_class_with_new() {
let gil = Python::acquire_gil();
let py = gil.python();
let typeobj = py.get_type::<UnitClassWithNew>();
assert!(typeobj
.call((), None)
.unwrap()
.cast_as::<PyCell<UnitClassWithNew>>()
.is_ok());
Python::with_gil(|py| {
let typeobj = py.get_type::<UnitClassWithNew>();
assert!(typeobj
.call((), None)
.unwrap()
.cast_as::<PyCell<UnitClassWithNew>>()
.is_ok());
});
}

#[pyclass]
Expand All @@ -60,13 +60,13 @@ impl TupleClassWithNew {

#[test]
fn tuple_class_with_new() {
let gil = Python::acquire_gil();
let py = gil.python();
let typeobj = py.get_type::<TupleClassWithNew>();
let wrp = typeobj.call((42,), None).unwrap();
let obj = wrp.cast_as::<PyCell<TupleClassWithNew>>().unwrap();
let obj_ref = obj.borrow();
assert_eq!(obj_ref.0, 42);
Python::with_gil(|py| {
let typeobj = py.get_type::<TupleClassWithNew>();
let wrp = typeobj.call((42,), None).unwrap();
let obj = wrp.cast_as::<PyCell<TupleClassWithNew>>().unwrap();
let obj_ref = obj.borrow();
assert_eq!(obj_ref.0, 42);
});
}

#[pyclass]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_getter_setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn tuple_struct_getter_setter() {

let inst = Py::new(py, TupleClassGetterSetter(10)).unwrap();

py_run!(py, inst, "assert inst.num == 10");
py_assert!(py, inst, "inst.num == 10");
py_run!(py, inst, "inst.num = 20");
py_run!(py, inst, "assert inst.num == 20");
py_assert!(py, inst, "inst.num == 20");
}

0 comments on commit 1c57294

Please sign in to comment.