Skip to content
This repository was archived by the owner on Nov 11, 2025. It is now read-only.

Commit 4ec6ff7

Browse files
committed
Expand PyCLIF pickle compatibility test coverage (NO production code changes).
These test were added while working on google/pybind11clif#30099. Before google/pybind11clif#30099, the `SimpleCallable` pickle tests were failing with PyCLIF-pybind11. PiperOrigin-RevId: 608694227
1 parent 054ee5e commit 4ec6ff7

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

clif/testing/pickle_compatibility.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222

2323
namespace clif_testing {
2424

25+
inline int SimpleCallable() { return 234; }
26+
27+
struct SimpleStruct {
28+
int value = -987;
29+
int SimpleMethod() { return value; }
30+
};
31+
2532
class StoreTwoUsingPostproc {
2633
public:
2734
StoreTwoUsingPostproc(int v0, int v1) : values{v0, v1} {}

clif/testing/python/pickle_compatibility.clif

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ from clif.testing.python.pickle_compatibility_helper import ReduceStoreTwo
1616

1717
from "clif/testing/pickle_compatibility.h":
1818
namespace `clif_testing`:
19+
def SimpleCallable() -> int
20+
21+
class SimpleStruct:
22+
def SimpleMethod(self) -> int
23+
1924
class StoreTwoUsingPostproc:
2025
def __init__(self, v0: int, v1: int)
2126
def Get(self, i: int) -> int

clif/testing/python/pickle_compatibility_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,29 @@
4040

4141
class PickleCompatibilityTest(unittest.TestCase):
4242

43+
def testAssumptions(self):
44+
self.assertGreaterEqual(pickle.HIGHEST_PROTOCOL, 0)
45+
46+
@parameterized.parameterized.expand(range(pickle.HIGHEST_PROTOCOL + 1))
47+
def testSimpleCallable(self, protocol):
48+
self.assertEqual(pickle_compatibility.SimpleCallable(), 234)
49+
serialized = pickle.dumps(
50+
pickle_compatibility.SimpleCallable, protocol=protocol
51+
)
52+
self.assertIn(b'clif.testing.python._pickle_compatibility', serialized)
53+
self.assertIn(b'SimpleCallable', serialized)
54+
deserialized = pickle.loads(serialized)
55+
self.assertEqual(deserialized(), 234)
56+
self.assertIs(deserialized, pickle_compatibility.SimpleCallable)
57+
58+
def testSimpleMethod(self):
59+
obj = pickle_compatibility.SimpleStruct()
60+
self.assertEqual(obj.SimpleMethod(), -987)
61+
with self.assertRaisesRegex(
62+
TypeError, 'missing __getinitargs__ and/or __getstate__'
63+
):
64+
pickle.dumps(obj.SimpleMethod)
65+
4366
@parameterized.parameterized.expand(zip(EMPTY_TYPES))
4467
def testUnpicklable(self, empty_type):
4568
obj = empty_type()

0 commit comments

Comments
 (0)