You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
834 B

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