1 from __future__
import annotations
9 from pybind11_tests
import pickling
as m
13 assert m.simple_callable() == 20220426
15 serialized = pickle.dumps(m.simple_callable)
16 deserialized = pickle.loads(serialized)
17 assert deserialized() == 20220426
21 with pytest.raises(TypeError)
as excinfo:
22 pickle.dumps(m.simple_callable)
23 assert re.search(
"can.*t pickle .*PyCapsule.* object",
str(excinfo.value))
26 @pytest.mark.parametrize(
"cls_name", [
"Pickleable",
"PickleableNew"])
33 data = pickle.dumps(p, 2)
34 p2 = pickle.loads(data)
35 assert p2.value() == p.value()
36 assert p2.extra1() == p.extra1()
37 assert p2.extra2() == p.extra2()
40 @pytest.mark.xfail(
"env.PYPY")
41 @pytest.mark.parametrize(
"cls_name", [
"PickleableWithDict",
"PickleableWithDictNew"])
46 p.dynamic =
"Attribute"
48 data = pickle.dumps(p, pickle.HIGHEST_PROTOCOL)
49 p2 = pickle.loads(data)
50 assert p2.value == p.value
51 assert p2.extra == p.extra
52 assert p2.dynamic == p.dynamic
56 from pybind11_tests
import enums
as e
58 data = pickle.dumps(e.EOne, 2)
59 assert e.EOne == pickle.loads(data)
72 p.stored_in_dict = 303
73 data = pickle.dumps(p, pickle.HIGHEST_PROTOCOL)
74 p2 = pickle.loads(data)
77 assert p2.stored_in_dict == 303
81 p = m.make_SimpleCppDerivedAsBase()
82 assert m.check_dynamic_cast_SimpleCppDerived(p)
86 with pytest.raises(AttributeError):
89 data = pickle.dumps(p, pickle.HIGHEST_PROTOCOL)
90 p2 = pickle.loads(data)
95 assert not m.check_dynamic_cast_SimpleCppDerived(p2)