test_pickling.py
Go to the documentation of this file.
1 from __future__ import annotations
2 
3 import pickle
4 import re
5 
6 import pytest
7 
8 import env
9 from pybind11_tests import pickling as m
10 
11 
13  assert m.simple_callable() == 20220426
14  if env.PYPY:
15  serialized = pickle.dumps(m.simple_callable)
16  deserialized = pickle.loads(serialized)
17  assert deserialized() == 20220426
18  else:
19  # To document broken behavior: currently it fails universally with
20  # all C Python versions.
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))
24 
25 
26 @pytest.mark.parametrize("cls_name", ["Pickleable", "PickleableNew"])
27 def test_roundtrip(cls_name):
28  cls = getattr(m, cls_name)
29  p = cls("test_value")
30  p.setExtra1(15)
31  p.setExtra2(48)
32 
33  data = pickle.dumps(p, 2) # Must use pickle protocol >= 2
34  p2 = pickle.loads(data)
35  assert p2.value() == p.value()
36  assert p2.extra1() == p.extra1()
37  assert p2.extra2() == p.extra2()
38 
39 
40 @pytest.mark.xfail("env.PYPY")
41 @pytest.mark.parametrize("cls_name", ["PickleableWithDict", "PickleableWithDictNew"])
43  cls = getattr(m, cls_name)
44  p = cls("test_value")
45  p.extra = 15
46  p.dynamic = "Attribute"
47 
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
53 
54 
56  from pybind11_tests import enums as e
57 
58  data = pickle.dumps(e.EOne, 2)
59  assert e.EOne == pickle.loads(data)
60 
61 
62 #
63 # exercise_trampoline
64 #
65 class SimplePyDerived(m.SimpleBase):
66  pass
67 
68 
70  p = SimplePyDerived()
71  p.num = 202
72  p.stored_in_dict = 303
73  data = pickle.dumps(p, pickle.HIGHEST_PROTOCOL)
74  p2 = pickle.loads(data)
75  assert isinstance(p2, SimplePyDerived)
76  assert p2.num == 202
77  assert p2.stored_in_dict == 303
78 
79 
81  p = m.make_SimpleCppDerivedAsBase()
82  assert m.check_dynamic_cast_SimpleCppDerived(p)
83  p.num = 404
84  if not env.PYPY:
85  # To ensure that this unit test is not accidentally invalidated.
86  with pytest.raises(AttributeError):
87  # Mimics the `setstate` C++ implementation.
88  setattr(p, "__dict__", {}) # noqa: B010
89  data = pickle.dumps(p, pickle.HIGHEST_PROTOCOL)
90  p2 = pickle.loads(data)
91  assert isinstance(p2, m.SimpleBase)
92  assert p2.num == 404
93  # Issue #3062: pickleable base C++ classes can incur object slicing
94  # if derived typeid is not registered with pybind11
95  assert not m.check_dynamic_cast_SimpleCppDerived(p2)
setattr
void setattr(handle obj, handle name, handle value)
Definition: pytypes.h:922
test_pickling.test_enum_pickle
def test_enum_pickle()
Definition: test_pickling.py:55
test_pickling.test_roundtrip_simple_py_derived
def test_roundtrip_simple_py_derived()
Definition: test_pickling.py:69
test_pickling.test_pickle_simple_callable
def test_pickle_simple_callable()
Definition: test_pickling.py:12
getattr
object getattr(handle obj, handle name)
Definition: pytypes.h:890
isinstance
bool isinstance(handle obj)
Definition: pytypes.h:842
str
Definition: pytypes.h:1558
test_pickling.test_roundtrip_with_dict
def test_roundtrip_with_dict(cls_name)
Definition: test_pickling.py:42
test_pickling.test_roundtrip_simple_cpp_derived
def test_roundtrip_simple_cpp_derived()
Definition: test_pickling.py:80
test_pickling.test_roundtrip
def test_roundtrip(cls_name)
Definition: test_pickling.py:27
test_pickling.SimplePyDerived
Definition: test_pickling.py:65


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:07:05