Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test pickling a simple callable (does not work). #3906

Merged
merged 4 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to test the broken behavior of CPython or just wrap this test with a non-strict xfail? At the very least, we should add a comment that this is a CPython bug not a PyPy bug and that PyPY is doing the preferred behavior for once.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment to be explicit.

The test as-is gives unambiguous information about the where & what, in 3 lines.

strict xfail would lose where & what exactly the failure is.

non-strict xfail would lose almost all information about the where & what.

unless it's tracked in a comment, which is about as many characters, only not executable code, therefore leaving room for doubts and misunderstandings.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwgk You can add a raises arg to xfail, but I see what you mean.

# 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