Skip to content

Commit 27198a1

Browse files
committed
Minor documentation tweaks on pickling.
1 parent b623a25 commit 27198a1

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

doc/sphinx/source/pickle.rst

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
:maxdepth: 2
66

77
====================================
8-
Pickling and C Extension Types
8+
Pickling C Extension Types
99
====================================
1010

1111
If you need to provide support for pickling your specialised types from your C extension then you need to implement some special functions.
1212

13-
This example shows you how to provided pickle support for for the ``custom2`` type described in the C extension tutorial in the
13+
This example shows you how to provided pickle support for for the ``custom2.Custom`` type described in the C extension tutorial in the
1414
`Python documentation <https://docs.python.org/3/extending/newtypes_tutorial.html#adding-data-and-methods-to-the-basic-example>`_.
1515

1616
Pickle Version Control
@@ -73,6 +73,9 @@ We are being passed an arbitrary Python object and need to check:
7373
Note that our ``__new__`` method (``Custom_new()``) has already been called on ``self``.
7474
Before setting any member value we need to de-allocate the existing value set by ``Custom_new()`` otherwise we will have a memory leak.
7575

76+
Error Checking
77+
^^^^^^^^^^^^^^^^^^^^^^^^
78+
7679
.. code-block:: c
7780
7881
/* Un-pickle the object */
@@ -101,7 +104,8 @@ Before setting any member value we need to de-allocate the existing value set by
101104
return NULL;
102105
}
103106
104-
Set ``first``:
107+
Set the ``first`` Member
108+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
105109

106110
.. code-block:: c
107111
@@ -117,7 +121,8 @@ Set ``first``:
117121
/* Increment the borrowed reference for our instance of it. */
118122
Py_INCREF(self->first);
119123
120-
Set ``last``:
124+
Set the ``last`` Member
125+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
121126

122127
.. code-block:: c
123128
@@ -131,7 +136,10 @@ Set ``last``:
131136
}
132137
Py_INCREF(self->last);
133138
134-
Set ``number``, this is a C fundamental type:
139+
Set the ``number`` Member
140+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
141+
142+
This is a C fundamental type so the code is slightly different:
135143

136144
.. code-block:: c
137145
@@ -151,7 +159,8 @@ And we are done.
151159
Py_RETURN_NONE;
152160
}
153161
154-
The complete code is:
162+
``__setstate__`` in Full
163+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
155164

156165
.. code-block:: c
157166
@@ -215,8 +224,6 @@ The complete code is:
215224
Py_RETURN_NONE;
216225
}
217226
218-
219-
220227
Add the Special Methods
221228
---------------------------------
222229

@@ -225,22 +232,22 @@ Now we need to add these two special methods to the methods table which now look
225232
.. code-block:: c
226233
227234
static PyMethodDef Custom_methods[] = {
228-
{"name", (PyCFunction) Custom_name, METH_NOARGS,
229-
"Return the name, combining the first and last name"
230-
},
231-
{"__getstate__", (PyCFunction) Custom___getstate__, METH_NOARGS,
232-
"Pickle the Custom object"
233-
},
234-
{"__setstate__", (PyCFunction) Custom___setstate__, METH_O,
235-
"Un-pickle the Custom object"
236-
},
237-
{NULL} /* Sentinel */
235+
{"name", (PyCFunction) Custom_name, METH_NOARGS,
236+
"Return the name, combining the first and last name"
237+
},
238+
{"__getstate__", (PyCFunction) Custom___getstate__, METH_NOARGS,
239+
"Pickle the Custom object"
240+
},
241+
{"__setstate__", (PyCFunction) Custom___setstate__, METH_O,
242+
"Un-pickle the Custom object"
243+
},
244+
{NULL} /* Sentinel */
238245
};
239246
240-
Example of Using ``custom2.Custom``
247+
Pickling a ``custom2.Custom`` Object
241248
-------------------------------------
242249

243-
We can test this with code like this that pickles one object then creates another object from that pickle.
250+
We can test this with code like this that pickles one ``custom2.Custom`` object then creates another ``custom2.Custom`` object from that pickle.
244251
Here is some Python code that exercises our module:
245252

246253
.. code-block:: python

0 commit comments

Comments
 (0)