Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 1 | import pytest |
| 2 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 3 | try: |
| 4 | import cPickle as pickle # Use cPickle on Python 2.7 |
| 5 | except ImportError: |
| 6 | import pickle |
| 7 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 8 | |
| 9 | def test_roundtrip(): |
Dean Moldovan | 6fccf69 | 2016-10-11 01:12:48 +0200 | [diff] [blame] | 10 | from pybind11_tests import Pickleable |
| 11 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 12 | p = Pickleable("test_value") |
| 13 | p.setExtra1(15) |
| 14 | p.setExtra2(48) |
| 15 | |
| 16 | data = pickle.dumps(p, 2) # Must use pickle protocol >= 2 |
| 17 | p2 = pickle.loads(data) |
| 18 | assert p2.value() == p.value() |
| 19 | assert p2.extra1() == p.extra1() |
| 20 | assert p2.extra2() == p.extra2() |
Dean Moldovan | 6fccf69 | 2016-10-11 01:12:48 +0200 | [diff] [blame] | 21 | |
| 22 | |
Wenzel Jakob | 1d1f81b | 2016-12-16 15:00:46 +0100 | [diff] [blame] | 23 | @pytest.unsupported_on_pypy |
Dean Moldovan | 6fccf69 | 2016-10-11 01:12:48 +0200 | [diff] [blame] | 24 | def test_roundtrip_with_dict(): |
| 25 | from pybind11_tests import PickleableWithDict |
| 26 | |
| 27 | p = PickleableWithDict("test_value") |
| 28 | p.extra = 15 |
| 29 | p.dynamic = "Attribute" |
| 30 | |
| 31 | data = pickle.dumps(p, pickle.HIGHEST_PROTOCOL) |
| 32 | p2 = pickle.loads(data) |
| 33 | assert p2.value == p.value |
| 34 | assert p2.extra == p.extra |
| 35 | assert p2.dynamic == p.dynamic |