2021-12-03 00:03:32 +00:00
|
|
|
#![cfg(feature = "macros")]
|
|
|
|
|
2020-07-20 17:09:43 +00:00
|
|
|
use pyo3::prelude::*;
|
|
|
|
use pyo3::py_run;
|
|
|
|
|
|
|
|
#[pyclass(dict, unsendable)]
|
|
|
|
struct UnsendableDictClass {}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl UnsendableDictClass {
|
|
|
|
#[new]
|
|
|
|
fn new() -> Self {
|
|
|
|
UnsendableDictClass {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-12-27 17:56:52 +00:00
|
|
|
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_10)), ignore)]
|
2020-07-20 17:09:43 +00:00
|
|
|
fn test_unsendable_dict() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
let inst = Py::new(py, UnsendableDictClass {}).unwrap();
|
|
|
|
py_run!(py, inst, "assert inst.__dict__ == {}");
|
|
|
|
});
|
2020-07-20 17:09:43 +00:00
|
|
|
}
|
2020-07-21 08:41:37 +00:00
|
|
|
|
|
|
|
#[pyclass(dict, unsendable, weakref)]
|
|
|
|
struct UnsendableDictClassWithWeakRef {}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl UnsendableDictClassWithWeakRef {
|
|
|
|
#[new]
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-12-27 17:56:52 +00:00
|
|
|
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_10)), ignore)]
|
2020-07-21 08:41:37 +00:00
|
|
|
fn test_unsendable_dict_with_weakref() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
let inst = Py::new(py, UnsendableDictClassWithWeakRef {}).unwrap();
|
|
|
|
py_run!(py, inst, "assert inst.__dict__ == {}");
|
|
|
|
py_run!(
|
|
|
|
py,
|
|
|
|
inst,
|
|
|
|
"import weakref; assert weakref.ref(inst)() is inst; inst.a = 1; assert inst.a == 1"
|
|
|
|
);
|
|
|
|
});
|
2020-07-21 08:41:37 +00:00
|
|
|
}
|