diff --git a/tests/test_pickling.cpp b/tests/test_pickling.cpp index 9787bd300..e154bc483 100644 --- a/tests/test_pickling.cpp +++ b/tests/test_pickling.cpp @@ -61,6 +61,8 @@ void wrap(py::module m) { } // namespace exercise_trampoline TEST_SUBMODULE(pickling, m) { + m.def("simple_callable", []() { return 20220426; }); + // test_roundtrip class Pickleable { public: diff --git a/tests/test_pickling.py b/tests/test_pickling.py index 52802ace8..12361a661 100644 --- a/tests/test_pickling.py +++ b/tests/test_pickling.py @@ -1,4 +1,5 @@ import pickle +import re import pytest @@ -6,6 +7,20 @@ import env from pybind11_tests import pickling as m +def test_pickle_simple_callable(): + assert m.simple_callable() == 20220426 + if env.PYPY: + serialized = pickle.dumps(m.simple_callable) + deserialized = pickle.loads(serialized) + assert deserialized() == 20220426 + else: + # To document broken behavior: currently it fails universally with + # all C Python versions. + with pytest.raises(TypeError) as excinfo: + pickle.dumps(m.simple_callable) + assert re.search("can.*t pickle .*PyCapsule.* object", str(excinfo.value)) + + @pytest.mark.parametrize("cls_name", ["Pickleable", "PickleableNew"]) def test_roundtrip(cls_name): cls = getattr(m, cls_name)