test_pickling.py revision 11986
1try:
2    import cPickle as pickle  # Use cPickle on Python 2.7
3except ImportError:
4    import pickle
5
6
7def test_roundtrip():
8    from pybind11_tests import Pickleable
9
10    p = Pickleable("test_value")
11    p.setExtra1(15)
12    p.setExtra2(48)
13
14    data = pickle.dumps(p, 2)  # Must use pickle protocol >= 2
15    p2 = pickle.loads(data)
16    assert p2.value() == p.value()
17    assert p2.extra1() == p.extra1()
18    assert p2.extra2() == p.extra2()
19
20
21def test_roundtrip_with_dict():
22    from pybind11_tests import PickleableWithDict
23
24    p = PickleableWithDict("test_value")
25    p.extra = 15
26    p.dynamic = "Attribute"
27
28    data = pickle.dumps(p, pickle.HIGHEST_PROTOCOL)
29    p2 = pickle.loads(data)
30    assert p2.value == p.value
31    assert p2.extra == p.extra
32    assert p2.dynamic == p.dynamic
33