Skip to content

Commit 287e4f2

Browse files
authored
Test pickling a simple callable (does not work). (#3906)
* Test pickling a simple callable (does not work). Currently only documents that it does not work. Starting point for future fix. * Use re.search to accommodate variations of the TypeError message. * PyPy: exercise full dumps/loads cycle. * Adding explicit "broken" comment.
1 parent f0b9f75 commit 287e4f2

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

tests/test_pickling.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ void wrap(py::module m) {
6161
} // namespace exercise_trampoline
6262

6363
TEST_SUBMODULE(pickling, m) {
64+
m.def("simple_callable", []() { return 20220426; });
65+
6466
// test_roundtrip
6567
class Pickleable {
6668
public:

tests/test_pickling.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
import pickle
2+
import re
23

34
import pytest
45

56
import env
67
from pybind11_tests import pickling as m
78

89

10+
def test_pickle_simple_callable():
11+
assert m.simple_callable() == 20220426
12+
if env.PYPY:
13+
serialized = pickle.dumps(m.simple_callable)
14+
deserialized = pickle.loads(serialized)
15+
assert deserialized() == 20220426
16+
else:
17+
# To document broken behavior: currently it fails universally with
18+
# all C Python versions.
19+
with pytest.raises(TypeError) as excinfo:
20+
pickle.dumps(m.simple_callable)
21+
assert re.search("can.*t pickle .*PyCapsule.* object", str(excinfo.value))
22+
23+
924
@pytest.mark.parametrize("cls_name", ["Pickleable", "PickleableNew"])
1025
def test_roundtrip(cls_name):
1126
cls = getattr(m, cls_name)

0 commit comments

Comments
 (0)