Commit 287e4f23 by Ralf W. Grosse-Kunstleve Committed by GitHub

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.
parent f0b9f755
...@@ -61,6 +61,8 @@ void wrap(py::module m) { ...@@ -61,6 +61,8 @@ void wrap(py::module m) {
} // namespace exercise_trampoline } // namespace exercise_trampoline
TEST_SUBMODULE(pickling, m) { TEST_SUBMODULE(pickling, m) {
m.def("simple_callable", []() { return 20220426; });
// test_roundtrip // test_roundtrip
class Pickleable { class Pickleable {
public: public:
......
import pickle import pickle
import re
import pytest import pytest
...@@ -6,6 +7,20 @@ import env ...@@ -6,6 +7,20 @@ import env
from pybind11_tests import pickling as m 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"]) @pytest.mark.parametrize("cls_name", ["Pickleable", "PickleableNew"])
def test_roundtrip(cls_name): def test_roundtrip(cls_name):
cls = getattr(m, cls_name) cls = getattr(m, cls_name)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment