Use Python::with_gil and py_assert

Co-authored-by: Yuji Kanagawa <yuji.kngw.80s.revive@gmail.com>
This commit is contained in:
messense 2021-03-18 21:09:46 +08:00
parent 2cec240b0e
commit 1c57294214
3 changed files with 28 additions and 28 deletions

View File

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

View File

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

View File

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