Skip to content

Commit 81e0975

Browse files
committed
clarified pickle version requirements (fixes #186)
1 parent 8edfa0c commit 81e0975

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

docs/advanced.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,13 +1340,15 @@ An instance can now be pickled as follows:
13401340
13411341
p = Pickleable("test_value")
13421342
p.setExtra(15)
1343-
data = pickle.dumps(p, -1)
1344-
1345-
Note that only the cPickle module is supported on Python 2.7. It is also
1346-
important to request usage of the highest protocol version using the ``-1``
1347-
argument to ``dumps``. Failure to follow these two steps will lead to important
1348-
pybind11 memory allocation routines to be skipped during unpickling, which will
1349-
likely cause memory corruption and/or segmentation faults.
1343+
data = pickle.dumps(p, 2)
1344+
1345+
Note that only the cPickle module is supported on Python 2.7. The second
1346+
argument to ``dumps`` is also crucial: it selects the pickle protocol version
1347+
2, since the older version 1 is not supported. Newer versions are also fine—for
1348+
instance, specify ``-1`` to always use the latest available version. Beware:
1349+
failure to follow these instructions will cause important pybind11 memory
1350+
allocation routines to be skipped during unpickling, which will likely lead to
1351+
memory corruption and/or segmentation faults.
13501352

13511353
.. seealso::
13521354

example/example15.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
p.setExtra1(15)
1515
p.setExtra2(48)
1616

17-
data = pickle.dumps(p, -1) # -1 is important (use highest protocol version)
17+
data = pickle.dumps(p, 2) # Must use pickle protocol >= 2
1818
print("%s %i %i" % (p.value(), p.extra1(), p.extra2()))
1919

2020
p2 = pickle.loads(data)

0 commit comments

Comments
 (0)