Skip to content

Commit

Permalink
_wrapped_simple_callable proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Feb 9, 2024
1 parent 9f1d6a0 commit 04e16a2
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions tests/test_pickling.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,26 @@
import copy
import pickle
import re

import pytest

import env
from pybind11_tests import pickling as m

_orig_simple_callable = m.simple_callable

def test_simple_callable_repr():
if env.PYPY:
expected = "<builtin_function_or_method object at 0x"
else:
expected = "<built-in method simple_callable of PyCapsule object at 0x"
assert repr(m.simple_callable).startswith(expected)

def _wrapped_simple_callable(*args, **kwargs):
return _orig_simple_callable(*args, **kwargs)

def test_Pickleable_value_repr():
cap = "" if env.PYPY else "PyCapsule."
expected = (
f"<bound method {cap}value of <pybind11_tests.pickling.Pickleable object at 0x"
)
assert repr(m.Pickleable("").value).startswith(expected)

m.simple_callable = _wrapped_simple_callable


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))
serialized = pickle.dumps(m.simple_callable)
deserialized = pickle.loads(serialized)
assert deserialized() == 20220426


@pytest.mark.parametrize("cls_name", ["Pickleable", "PickleableNew"])
Expand Down

0 comments on commit 04e16a2

Please sign in to comment.