Skip to content

Commit

Permalink
Test pickling a simple callable (does not work). (pybind#3906)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Ralf W. Grosse-Kunstleve authored May 2, 2022
1 parent f0b9f75 commit 287e4f2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/test_pickling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_pickling.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import pickle
import re

import pytest

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)
Expand Down

0 comments on commit 287e4f2

Please sign in to comment.