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.
This commit is contained in:
Ralf W. Grosse-Kunstleve
2022-05-02 12:39:36 -07:00
committed by GitHub
parent f0b9f755e4
commit 287e4f233d
2 changed files with 17 additions and 0 deletions

View File

@@ -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)